Building an image with an artifact inside it on a Mac

Prior to Docker 1.3, you couldn’t mount your Mac directories into your container. This is because Docker itself ran in a VirtualBox environment (boot2docker started this VM), so attempts to mount directories would be referencing the VM. Sure, there were tricks with SSHFS, but I couldn’t get SSHFS to install (I think it conflicted with Google Drive). So I used the Dockerfile ability to supply an archive to the build process from which it took it’s content.


First, copy the artefacts needed to a subfolder of the build directory

$ rm -rf content
$ mkdir content
$ cp ../target/backend-1.0.3-SNAPSHOT.jar content
$ cp ../configuration/dev.yml content

Then create the Dockerfile (using one of the new Language Stack Base Images)

$ cat > DOCKERFILE << EOF
FROM dockerfile/java:openjdk-7-jre
MAINTAINER Andrew Gorton <[email protected]>
 
RUN mkdir -p /opt/backend
 
ADD content/backend-1.0.3-SNAPSHOT.jar /opt/backend/
ADD content/dev.yml /opt/backend/
 
EXPOSE 8020 8021
 
CMD ["/usr/bin/java", "-jar", "/opt/backend/backend-1.0.3-SNAPSHOT.jar", "server", "/opt/backend/dev.yml"]
EOF

RUN mkdir -p /opt/backend

ADD content/backend-1.0.3-SNAPSHOT.jar /opt/backend/
ADD content/dev.yml /opt/backend/

EXPOSE 8020 8021

CMD ["/usr/bin/java", "-jar", "/opt/backend/backend-1.0.3-SNAPSHOT.jar", "server", "/opt/backend/dev.yml"]
EOF

Note that the ADD commands use the directory names as stored inside the archive.

I then compress the Dockerfile and the content subdirectory, and use this archive to build the image.

$ tar cvfz build.tgz Dockerfile content
$ docker build -t andrewgorton/sdwntier-backend - < build.tgz

No messy upload-artefact-to-a-webserver-and-run-wget-during-build, nor messing around with SSHFS.

This is my personal blog - all views are my own.