Sharing Build Environments With Docker Registries

Creating a Docker image that contains everything you need to build and develop your firmware project is a great first step to a robust and portable build environment. The next step is using a Docker Registry to easily share that image. A Docker Registry is a service that allows users to share Docker images using the {% c-line %}docker push{% c-line-end %} and {% c-line %}docker pull{% c-line-end %} commands.

A number of companies offer a Docker Registry as a hosted service, including Docker, GitHub, AWS, and Microsoft. In this example we'll demonstrate using Docker Hub as our registry.

Before diving in, if you're not familiar with Docker, or why it's such a powerful tool for embedded engineers check out this post on  Docker for embedded engineering:

https://www.lagerdata.com/articles/an-introduction-to-docker-for-managing-build-environments

We'll start by locally building a build environment for the nRF52 family of MCUs from Nordic using this publicly hosted DockerFile:

{% c-block language="console" %}
% git clone https://github.com/lagerdata/devenv-cortexm-nrf52.git
% cd devenv-cortexm-nrf52
% docker build -f Dockerfile.nrf52 . -t devenv-cortexm-nrf52:version-1.3
...
=> => writing image sha256:2cf17e9fd029650b8e68113d90ccf6e9ee08224409b178d22b48627239a875a8
%
{% c-block-end %}

Now if we run {% c-line %}docker image ls{% c-line-end %} we should see the following

{% c-block language="console" %}
% docker image ls
REPOSITORY                               TAG                   IMAGE ID       CREATED        SIZE
devenv-cortexm-nrf52                   version-1.3           6ebb1b51a8eb   1 minute ago     2.82GB
%
{% c-block-end %}

You'll notice the image was tagged with {% c-line %}version 1.3{% c-line-end %}. We chose this value because that aligns with the {% c-line %}git tag{% c-line-end %} version of the Dockerfile used to build this image.

{% c-block language="console" %}
% git log --oneline
244d0a4 (HEAD -> main, tag: v1.3, origin/main, origin/HEAD) Combine nanopb RUN steps
%
{% c-block-end %}

Now go to hub.docker.com and create an account.

There are 4 tiers {% c-line %}Free{% c-line-end %}, {% c-line %}Pro{% c-line-end %}, {% c-line %}Team{% c-line-end %}, and {% c-line %}Large{% c-line-end %}.

https://www.docker.com/pricing

The {% c-line %}Free{% c-line-end %} plan offers unlimited public repositories (and 1 private repository) which is good enough to get started.

Use the credentials you just created for you Docker Hub account to login to the Docker Hub registry. In this case, the username is {% c-line %}adhanali{% c-line-end %}.

{% c-block language="console" %}
% docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: adhanali
Password:
Login Succeeded
%
{% c-block-end %}

Next you'll need to re-tag your docker image by pre-fixing it with your user account name. In this case we want to retag the image from {% c-line %}devenv-cortexm-nrf52{% c-line-end %} to {% c-line %}adhanali/devenv-cortexm-nrf52{% c-line-end %}.

{% c-block language="console" %}
% docker tag devenv-cortexm-nrf52:version-1.3 adhanali/devenv-cortexm-nrf52:version-1.3
{% c-block-end %}

The last step is to {% c-line %}push{% c-line-end %} your image to the registry

{% c-block language="console" %}
% docker push adhanali/devenv-cortexm-nrf52:version-1.3
{% c-block-end %}

Now, if you go back to your Docker Hub account you should see something like

Image In Docker Registry

If someone else on your team wants to use this image to start developing a project they simply need to run {% c-line %}docker pull adhanali/devenv-cortexm-nrf52:version-1.3{% c-line-end %} and that's it!

Another really nice feature of Docker Hub is that it allows you to link your DockerFile in GitHub (or BitBucket, GitLab, etc) to an image in your Docker Hub account. Once linked, every time you push a change to your Dockerfile, Docker Hub will automatically trigger a re-build of your image. You can even set tagging rules so that Docker Hub will auto-tag your image based the rules you give it. We'll go over how to link your Docker File to your Docker Hub account and trigger automatic builds in a later post.

Want to stay up to date on the future of firmware? Join our mailing list.

Section
Chapter
Published