In my previous entry I discussed how to create a docker image using a minimal Dockerfile. There are a few benefits to this method, the main one being is that you can eliminate a lot less baggage layers resulting from the Dockerfile build process. This is good because the resulting image is much smaller and you’ll have much shorter transfer times when moving these images around. The downside to this method is you need to copy your files into the container from the docker host using scp which adds a bunch of unwanted complexity.
I realized recently “docker run” could be used instead of “docker build” to build docker images. This is what that looks like:
docker run -t -v $(pwd)/fs:/fs --name builder docker.psidox.com/base /bin/bash /fs/build.sh docker commit builder docker.psidox.com/image docker rm builder
So whats going here..
- Mount our build directory as a volume eliminating the need to scp to copy files in
- Label our container as “builder” so we can commit it later to image using this name
- Execute our build script from the volume we just mounted to setup the container how we want it
- Once the build script has finished it will then run the “docker commit” command, committing our “builder” container to a docker image called “docker.psidox.com/image”
- Remove our temporary builder container
So there you have it, a simple image building process that will result in no extra garbage layers. Personally I think this is all a symptom of “docker build” needing some improvement, a single Dockerfile build should not result in multiple image layers and more importantly should be bash scriptable.