Skip to content

Commit

Permalink
Run stage0 as root during Docker build
Browse files Browse the repository at this point in the history
Fixes sbt#1195

Since the user of the fabric8 image is already non-root, stage0 fails with

```
chmod: changing permissions of '/opt/docker': Operation not permitted
```

This switches the stage0 user to root explicitly so we can run `chmod` in there. In the actual image the user is set back to 1001.
  • Loading branch information
eed3si9n committed Jan 25, 2019
1 parent a2baeb2 commit 991d0df
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ object DockerPlugin extends AutoPlugin {
Seq(
makeFromAs(base, stage0name),
makeWorkdir(dockerBaseDirectory),
makeUserAdd(user, uid, gid),
makeCopy(dockerBaseDirectory),
makeUser("root"),
makeChmod(dockerChmodType.value, Seq(dockerBaseDirectory)),
DockerStageBreak
)
Expand Down Expand Up @@ -337,6 +337,13 @@ object DockerPlugin extends AutoPlugin {
daemonUser
)

/**
* @param daemonUser
* @return USER docker command
*/
private final def makeUser(daemonUser: String): CmdLike =
Cmd("USER", daemonUser)

/**
* @param userId userId of the daemon user
* @return USER docker command
Expand Down
16 changes: 8 additions & 8 deletions src/sbt-test/docker/file-permission/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ lazy val root = (project in file("."))
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM openjdk:8 as stage0
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|WORKDIR /opt/docker
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|COPY opt /opt
|USER root
|RUN ["chmod", "-R", "u=rX,g=rX", "/opt/docker"]
|
|FROM openjdk:8
|FROM fabric8/java-centos-openjdk8-jdk
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|WORKDIR /opt/docker
|COPY --from=stage0 --chown=daemon:root /opt/docker /opt/docker
Expand All @@ -32,7 +32,7 @@ lazy val root = (project in file("."))
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM openjdk:8
"""FROM fabric8/java-centos-openjdk8-jdk
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|WORKDIR /opt/docker
|COPY opt /opt
Expand All @@ -59,7 +59,7 @@ lazy val root = (project in file("."))
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM openjdk:8
"""FROM fabric8/java-centos-openjdk8-jdk
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|WORKDIR /opt/docker
|COPY --chown=daemon:root opt /opt
Expand All @@ -72,13 +72,13 @@ lazy val root = (project in file("."))
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val lines = dockerfile.linesIterator.toList
assertEquals(lines,
"""FROM openjdk:8 as stage0
"""FROM fabric8/java-centos-openjdk8-jdk as stage0
|WORKDIR /opt/docker
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|COPY opt /opt
|USER root
|RUN ["chmod", "-R", "u=rwX,g=rwX", "/opt/docker"]
|
|FROM openjdk:8
|FROM fabric8/java-centos-openjdk8-jdk
|RUN id -u daemon || useradd --system --create-home --uid 1001 --gid 0 daemon
|WORKDIR /opt/docker
|COPY --from=stage0 --chown=daemon:root /opt/docker /opt/docker
Expand Down
1 change: 1 addition & 0 deletions src/sbt-test/docker/file-permission/change.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dockerBaseImage := "fabric8/java-centos-openjdk8-jdk"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.typesafe.sbt.packager.docker._

dockerPermissionStrategy := DockerPermissionStrategy.CopyChown
dockerBaseImage := "fabric8/java-centos-openjdk8-jdk"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.typesafe.sbt.packager.docker._

dockerPermissionStrategy := DockerPermissionStrategy.None
dockerBaseImage := "fabric8/java-centos-openjdk8-jdk"
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import com.typesafe.sbt.packager.docker._

dockerPermissionStrategy := DockerPermissionStrategy.MultiStage
dockerChmodType := DockerChmodType.UserGroupWriteExecute
dockerBaseImage := "fabric8/java-centos-openjdk8-jdk"

0 comments on commit 991d0df

Please sign in to comment.