Docker and compiled code
Developing compiled code in a synchronized, isolated, version-controlled build environment with Docker
Interpreted Code
Most examples I’ve come across involving Docker demonstrate how to build and run web applications. Since web apps are often written in interpreted languages (e.g. Ruby, Python, JavaScript), there is no traditional “build” step to compile any code. The code you write is the code you run. You don’t need to worry about development environments, because they are inherently the same as the production environment.
Web app development with Docker
- Write app code (js, py, rb).
- Build an image by copying code into a pre-made base image.
- Add other missing pieces (e.g. database, web server).
- Ship it and repeat.
Any Dockerfile for this type of project is versioned along side the code. Code merges mean everyone gets the new environment. The development environment is the same thing shipped to production. Everything is gravy.
Compiled Code
Compiled code is different, in that it has an additional build step between the above step 1 and step 2, one that produce binaries from source code. Such an app requires an operating system and a toolchain to compile, but very little beyond the executable binary to run.
This is of course where the Docker web app paradigm breaks down, because the build environment is not the same as the runtime environment.
Go is one such compiled language, and the examples I have seen for building or distributing Go code seem to follow either a web app pattern or a minimalist pattern:
Web app style
- Write app code (go).
- Build an image by copying code into a pre-made base image.
a) Compile code during image build with a RUN command. - Add other missing pieces (e.g. database, web server).
- Ship it and repeat.
The problem here is that the resulting image in production is an entire build environment, including operating system, toolchain, and source code. Does your 1 MB app really need a 500 MB image to run? Or perhaps more importantly, what if your source code is proprietary or distribution is restricted by law? Just run another step to delete it?
Minimalist approach
- Write app code (go).
- Compile app code on host machine.
- Build an image by copying binaries into a bare-bones base image.
- Add other missing pieces (e.g. database, web server).
- Ship it and repeat.
This approach is much more sane, in that we’ve separated the development environment from the runtime environment. Except, the approach totally ignores the host of new problems it introduces:
- Synchronization of dependencies
If the environment is distributed (per developer’s computer), then dependencies need to be kept in sync and versioned. One developer cannot be compiling with Go 1.5 while another uses Go 1.7. You could solve this by making everyone work on the same machine… - Isolation of environments
…but, if the environment is shared, changes to the environment will be propagated to all developers during development before code has been merged, which can lead to conflicts or unexpected build failures. There’s also the potential for a sysadmin hurdle, where developers need to ask the sysadmin to install packages for them. - Environment versions
Even if you manage to synchronize dependencies and isolate development environments, how will the development environment itself be versioned? The environment changes over time just as the code does. Will developers be able to work on old releases quickly and easily to run, debug, and patch bugs, long after the release’s dependencies have changed? Version 1.0 might be built with Go version 1.6 while Version 3.0 is several years down the road and building with an incompatible Go 2.0.
I’ve developed an approach that aims to keep developers in sync with one another, while giving them with the isolation and autonomy needed to rapidly prototype and develop new code, all while version controlling the build environment so that a git checkout version-1.0; build is the only step necessary to leverage the old build environment and compile.
Containerizing your build before your app
In order to isolate environments, each developer will need to compile code within a Docker container, which is based upon a Dockerfile that is version controlled with the source code.
Get Brian’s stories in your inbox
Join Medium for free to get updates from this writer.
Developers are free to add or modify dependencies within their local copy of the environment, even if the host machine is shared. Eventually, those changes will be merged to the the master branch for other developers to use, and as long as developers are launching builds on an image from their their commit point, all development environments are kept in sync.
The tricky part is of course to make it seamless for the developer to run a container from the right image without any effort, no matter the revision (going back or forward in commit history). The basic idea is to:
- Checkout a commit
- Pull or Build a docker image
- Compile
- If changes are made to the Dockerfile environment, restart from 2
Before you compile, hash your Dockerfile, and try to pull an image tagged with the hash of your Dockerfile from the Docker hub.
you@host$ DOCKERFILE_HASH=($(sha256sum dockerfiles/Dockerfile))
you@host$ docker pull myproj:$DOCKERFILE_HASHIf it doesn’t exist on the hub, build the image yourself and tag it with the hash.
you@host$ docker build -t myproj:$DOCKERFILE_HASH dockerfiles/Invoke a container that mounts your source code, sets up a user and permissions, and sets the working directory to the root of your source code. Make sure to have the build artifacts be written to a volume accessible by the host machine as well. Compile.
you@host$ docker run -it \
-u $(id -u):$(id -g) \
-v $GIT_ROOT:/mnt/src \
-w /mnt/src \
foo/first-build \
/bin/bash
I have no name!@1a61b316c4de$ makeIf the compilation succeeds, your tests pass, and you’re ready to merge back into master, push your image to the hub and merge.
you@host$ docker push myrepo:$DOCKERFILE_HASH
you@host$ git merge feature/fooNow, repeat! (Wrapper scripts can help to automate the hash and pull/build steps, while the push step should be done by an automated CI system like Jenkins).
You should now be compiling your code on an image built by your CI system pulled from the hub. The image will be an identical copy for everyone in the project. If a new package is installed, the CI system should build, test, and push a new image to the hub, and after a git merge, a brand new environment will be available as well for the next checkout.
If you go back in time and checkout the previous commit, by repeating the above steps, you’ll be back to your previous build environment.
From here, you can easily copy the output of your build into a new Docker image and distribute that if you’d like without shipping the whole build environment. Or, just send binaries.
Note: Regardless of whether you’re able to pull images (the golden standard for identical environments) or you end up needing to rebuild the images yourself, you should make every effort possible to keep your build as reproducible as possible. That means…
- Version your packages
- Mirror repositories (debian, pip, etc)
- Mirror source code dependencies if possible (from github, etc)
- Mirror the base Docker image in Docker hub if possible
If you’re working with compiled code and trying to figure out how to leverage Docker with your build, I hope you found this helpful!


![Greenwave/TCP Connected Lightbulb Update [Updated]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*oXQmIOTC-b1AZcbBG0MVyQ.jpeg)






