Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible improvement for #1202 #1235

Merged
merged 2 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,22 @@ object DockerPlugin extends AutoPlugin {
gidOpt: Option[String]): CmdLike =
Cmd(
"RUN",
(List("id", "-u", daemonUser, "2>", "/dev/null", "||") :::
(List("id", "-u", daemonUser, "1>/dev/null", "2>&1", "||") :::
(gidOpt.fold[List[String]](Nil)(
gid => List("((", "getent", "group", gid, "||", "groupadd", "-g", gid, daemonGroup, ")", "&&")
gid =>
List("((", "getent", "group", gid, "1>/dev/null", "2>&1", "||") :::
List("(", "type", "groupadd", "1>/dev/null", "2>&1", "&&") :::
List("groupadd", "-g", gid, daemonGroup, "||") :::
List("addgroup", "-g", gid, "-S", daemonGroup, "))", "&&")
)) :::
List("(", "type", "useradd", "1>/dev/null", "2>&1", "&&") :::
List("useradd", "--system", "--create-home") :::
(uidOpt.fold[List[String]](Nil)(List("--uid", _))) :::
(gidOpt.fold[List[String]](Nil)(List("--gid", _))) :::
List(daemonUser, ")")): _*
List(daemonUser, "||") :::
List("adduser", "-S") :::
(uidOpt.fold[List[String]](Nil)(List("-u", _))) :::
List("-G", daemonGroup, daemonUser, "))")): _*
)

/**
Expand Down
10 changes: 5 additions & 5 deletions src/sbt-test/docker/file-permission/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lazy val root = (project in file("."))
|
|FROM fabric8/java-centos-openjdk8-jdk
|USER root
|RUN id -u demiourgos728 2> /dev/null || (( getent group 0 || groupadd -g 0 root ) && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 )
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
|COPY --from=stage0 --chown=demiourgos728:root /opt/docker /opt/docker
|USER 1001:0
Expand All @@ -37,7 +37,7 @@ lazy val root = (project in file("."))
assertEquals(lines,
"""FROM fabric8/java-centos-openjdk8-jdk
|USER root
|RUN id -u demiourgos728 2> /dev/null || (( getent group 0 || groupadd -g 0 root ) && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 )
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
|COPY opt /opt
|USER 1001:0
Expand All @@ -51,7 +51,7 @@ lazy val root = (project in file("."))
assertEquals(lines,
"""FROM fabric8/java-centos-openjdk8-jdk
|USER root
|RUN id -u demiourgos728 2> /dev/null || (( getent group 5000 || groupadd -g 5000 sbt ) && useradd --system --create-home --uid 1001 --gid 5000 demiourgos728 )
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 5000 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 5000 sbt || addgroup -g 5000 -S sbt )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 5000 demiourgos728 || adduser -S -u 1001 -G sbt demiourgos728 ))
|WORKDIR /opt/docker
|COPY opt /opt
|USER 1001:5000
Expand All @@ -65,7 +65,7 @@ lazy val root = (project in file("."))
assertEquals(lines,
"""FROM openjdk:8
|USER root
|RUN id -u demiourgos728 2> /dev/null || (( getent group 0 || groupadd -g 0 root ) && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 )
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
|COPY opt /opt
|RUN ["chmod", "-R", "u=rX,g=rX", "/opt/docker"]
Expand Down Expand Up @@ -100,7 +100,7 @@ lazy val root = (project in file("."))
|
|FROM fabric8/java-centos-openjdk8-jdk
|USER root
|RUN id -u demiourgos728 2> /dev/null || (( getent group 0 || groupadd -g 0 root ) && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 )
|RUN id -u demiourgos728 1>/dev/null 2>&1 || (( getent group 0 1>/dev/null 2>&1 || ( type groupadd 1>/dev/null 2>&1 && groupadd -g 0 root || addgroup -g 0 -S root )) && ( type useradd 1>/dev/null 2>&1 && useradd --system --create-home --uid 1001 --gid 0 demiourgos728 || adduser -S -u 1001 -G root demiourgos728 ))
|WORKDIR /opt/docker
|COPY --from=stage0 --chown=demiourgos728:root /opt/docker /opt/docker
|USER 1001:0
Expand Down
13 changes: 13 additions & 0 deletions src/sbt-test/docker/test-busybox-create-user/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

enablePlugins(JavaAppPackaging)

name := "test-busybox-create-user"

version := "0.1.0"

maintainer := "Boris Capitanu <[email protected]>"
dockerBaseImage := "anapsix/alpine-java:8"
daemonUserUid in Docker := Some("2000")
daemonUser in Docker := "appuser"
daemonGroupGid in Docker := Some("3000")
daemonGroup in Docker := "appgroup"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import com.sun.security.auth.module.UnixSystem

object Main extends App {
println(System.getProperty("user.name"))
}
3 changes: 3 additions & 0 deletions src/sbt-test/docker/test-busybox-create-user/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generate the Docker image locally
> docker:publishLocal
$ exec bash -c 'docker run test-busybox-create-user:0.1.0 | grep -q "appuser"'