1
votes

Hey everyone I am having a problem using the native packager to create a docker image of a Scalatra web app. The relevant part of my build.sbt file is here:

enablePlugins(JettyPlugin)
enablePlugins(JavaAppPackaging)
enablePlugins(DockerPlugin)

// for Docker, the "." location where it is looking for files is in target/docker/stage. For the COPY command to work,
// the sbt Docker packager has to know this file resides in ../../scala-2.11 so include this mapping
//mappings in Docker += file("../../scala-2.11/direct2-batch-match_2.11-0.1.0-SNAPSHOT.war") -> "direct2-batch-match_2.11-0.1.0-SNAPSHOT.war"
dockerCommands := Seq(Cmd("FROM","tomcat:8.0.20-jre8"), Cmd("LABEL", """vendor="Dun & Bradstreet" """), Cmd("LABEL", """version="0.0" """), Cmd("EXPOSE", "8080"), Cmd("COPY","direct2-batch-match_2.11-0.1.0-SNAPSHOT.war","webapps/d2bm.war"))

As you can see I have commented out mappings in Docker. The reason for this is if I leave it in, it creates an empty directory in target/docker/stage with the same name as my war file (in this case direct2-batch-match_2.11-0.1.0-SNAPSHOT.war). This allows the Dockerfile to work fine for the docker build, but the contents of the war file are not included in the build.

I am running on Mac OS X, I wondered if this was a bug where the plugin was attempting to create a link to the desired file and perhaps the link command for Linux gives the wrong results on Mac OS X Sierra.

Do I need to do the mappings differently? Has anyone else run into this? The empty directory is created when I run docker:publishLocal in sbt, I am using plugin version 1.2.0-M9.

results of running docker:publishLocal in sbt:

docker:publishLocal
[info] Updating {file:/Users/cardm/projects/direct2-batch-match/}direct2-batch-match...
[info] Wrote /Users/cardm/projects/direct2-batch-match/target/scala-2.11/direct2-batch-match_2.11-0.1.0-SNAPSHOT.pom
[info] Resolving org.scalatra.scalate#scalate-precompiler_2.11;1.8.0.1 ...
[info] Done updating.
[info] Compiling Templates in Template Directory: /Users/cardm/projects/direct2-batch-match/src/main/webapp/WEB-INF/templates
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[warn] You have no main class in your project. No start script will be generated.
[warn] You have no main class in your project. No start script will be generated.
[info] Sending build context to Docker daemon 74.45 MB
[info] Step 1/5 : FROM tomcat:8.0.20-jre8
[info] ---> e88a065848be
[info] Step 2/5 : LABEL vendor "Dun & Bradstreet"
[info] ---> Using cache
[info] ---> de03acb09377
[info] Step 3/5 : LABEL version "0.0"
[info] ---> Using cache
[info] ---> f4f3e4accacb
[info] Step 4/5 : EXPOSE 8080
[info] ---> Using cache
[info] ---> f971b388dc2c
[info] Step 5/5 : COPY direct2-batch-match_2.11-0.1.0-SNAPSHOT.war webapps/d2bm.war
[info] ---> 60a95c386e23
[info] Removing intermediate container cf15079616cd
[info] Successfully built 60a95c386e23
[info] Built image DockerAlias(None,None,direct2-batch-match,Some(0.1.0-SNAPSHOT))
[success] Total time: 4 s, completed Apr 28, 2017 6:35:09 PM

contents of target/docker/stage:

Mike-Cards-MacBook-Pro:direct2-batch-match cardm$ ls -la target/docker/stage/
total 8
drwxr-xr-x 5 cardm staff 170 Apr 28 18:35 .
drwxr-xr-x 4 cardm staff 136 Apr 28 18:35 ..
-rw-r--r-- 1 cardm staff 156 Apr 28 18:35 Dockerfile
drwxr-xr-x 2 cardm staff 68 Dec 31 1969 direct2-batch-match_2.11-0.1.0-SNAPSHOT.war
drwxr-xr-x 3 cardm staff 102 Apr 28 18:35 opt

As you can see something is wrong it's useless to put an empty directory in there instead of the file, that's why I'm thinking this is supposed to be a link instead. In my set file if I use "mappings in Universal" instead it produces the same result, an empty directory with the war file's name.

1

1 Answers

1
votes

Hey everyone it turns out the problem was in how my sbt file was set up, in particular the "mappings in Docker" section. The correct setting for my project is as follows:

mappings in Docker += file("target/scala-2.11/direct2-batch-match_2.11-0.1.0-SNAPSHOT.war") -> "direct2-batch-match_2.11-0.1.0-SNAPSHOT.war"

The trick here is that the "source" file (left side of the ->) is relative to the PROJECT ROOT folder, and the destination file (right side of the ->) is relative to the docker staging directory, which is in PROJECT ROOT/target/docker/stage. In my original settings I assumed that both file paths were relative to the Docker staging directory, and while debugging the sbt-native-packager plugin I discovered that these 2 paths are relative to different root locations.

I really think the sbt-native-packager documentation for Docker should be updated to make this clear.