Use Docker to configure, compile, and run.
Dockerfile will build a Docker image that has CMake and gcc installed.
The resulting container can then execute
The source and build directories are bind-mounted to this container, which allows your changes to be detected by the container without rebuilding the image. It also allows build results to persist on the host (with the permissions that you specify).
- Docker v17.05+
Before you can run a Docker container, you need a Docker image. Either pull an image from DockerHub or build your own.
Pull Docker Image
Pull a pre-built Docker image from DockerHub:
$ docker pull kevinwmatthews/gcc-cmake:8-3.13.1
I like to tag the image with a name that is easy to remember:
$ docker tag kevinwmatthews/gcc-cmake:8-3.13.1 cmake
Build Docker Image
Build the Docker image that is the basis for containers:
# docker build --tag <image_name> <relative/path/to/dockerfile/dir> $ docker build --tag gcc-cmake:8-3.13.1 Dockerfiles/
I like to tag the image for ease fo use at the command line:
$ docker tag gcc-cmake:8-3.13.1 cmake
For more information, see
docker build --help.
The base docker-gcc image is fairly large, but all subsequent builds can reuse this image.
Run Docker Container
A docker image can be run simply using
docker run <image_name>, but we want
to customize several aspects of this container.
This can be done by hand each time the container is run, or use the provided helper script with an environment file.
$ cp example.env .env
.env to be appropriate to your system.
Note that Docker requires absolute paths.
Run the container using:
# ENV_FILE=<env_file> ./docker_run.sh <image_name> $ ENV_FILE=.env ./docker_run.sh cmake
Any options that are passed to
docker_run.sh are passed directly to
Compile and Run
Once within the container, compile the source as usual.
Configure the project’s build system using:
# cmake <path/to/source> $ cmake ../hello_world
Compile the project using:
Run the resulting executable using:
# ./bin/<your_executable> $ ./bin/hello_world Hello, World!
When finished, stop the container using: