Above: Docker loading an image into your computer and preparing it for execution. Image credit Gazouya-japan
If you're not already familiar with Docker, it can be a bit intimidating. What is a Dockerfile? What is an image and how do I get one? What's the difference between an image and a container? (see answers at the end if you don't already know). In this post we'll show you how you can use the Lager CLI to quickly set up a Dockerized build environment for your project. We'll be using libopencm3, an open-source firmware library for Cortex-M microcontrollers. Note that this is an unaffiliated third-party library that has had no special work done to support Lager or Docker - it just works out of the box!
Step 1: Install Docker and the Lager CLI tool, and clone the libopencm3 repo
Step 2: Create a Lager devenv
A Lager devenv is just a thin wrapper around Docker for the common tasks of building embedded projects. Devenvs are a completely optional part of Lager, but they give you a convenient and cross platform way to run commands within your container. They also make it simple to name those commands and share them with other developers who have access to the repository.
This will prompt you with two questions, and for each one you can accept the default by just pressing enter. lagerdata/devenv-cortexm is a Docker image maintained by Lager that contains the arm-none-eabi toolchain, as well as other useful tools for developers working with Cortex M chips. "Source code mount directory" just tells us where within the container to place the code that we'll be building.
Docker image [lagerdata/devenv-cortexm]:
Source code mount directory in docker container [/app]:
Step 3: Build it!
lager exec is the CLI command for running a command in a Docker container. In this case, we'll be running make - since that is how the libopencm3 README tells us to build it. This should kick off the build process within a container. Note that we didn't have to install make or the arm-none-eabi toolchain onto our system, and it works the same regardless of whether we're running on Windows, Linux, or MacOS.
And that's it! After running the make command, the lib subdirectory will contain the build artifacts, the same as if we had run make on our host system.
For more info on how to use Lager Devenvs, see our documentation.
What is a Dockerfile? A set of steps for building a Docker image. Lager maintains a free set of Dockerfiles for building code for some common embedded platforms: https://github.com/lagerdata/devenv-dockerfiles
What is a Docker image? A Docker image is a file that contains all of the runtime dependencies (files, executables, libraries, environment variables, etc) of an application. It is used to create Docker containers
What is a Docker container? A Docker container is a running instance of an application defined by a Docker image. By default it is relatively isolated from the host system and does not have access to other files or processes on the host.