Docker on macOS - Installation and Getting Started with GCC 8 in a Docker container
Posted on March 26, 2019 by Paul
In this article I will show you how to install Docker on macOS. In order to exemplify the use of Docker, we’ll use a container to build and run a simple C or C++ application with GCC 8.
There is also a video version of this tutorial:
Start by downloading Docker Desktop for macOS. If you don’t have a Docker account, sign up for one it is free and it only takes a few minutes to create one. Once you are logged in, you should be able to get the Docker app for macOS. Open the dmg file and drag the installer to your Applications, you can safely accept all the default settings.
After the installation is finished, start the Docker application and wait for the message Docker Desktop is now up and running. Next, open a Terminal and check if the docker command is available:
This is what I see on my machine:
As mentioned in the introduction, I will exemplify the use of a Docker container with a simple C application. Create a new directory for your application and name it myapp:
Next, let’s write a simple C program that will print Hello, World!, save the file as test.c:
In order to make our life easier and to be able to, optionally, add more files to the project we’ll also write a simple Makefile:
At this point, myapp folder should contain two files: test.c and Makefile.
Next, we are going to create a temporary container in which we will build and run the above application. Our container will be based on the official GCC image from Docker Hub.
Make sure you are in myapp folder and run the next command from a Terminal:
The above command will mount the current folder as /myapp in the container, set the working folder to myapp. First time when you will run the command it will download the image from the Docker Hub. Finally make prog will be executed inside the container. Next time when you will run the command it will use the existing image and will be much faster.
This is what I see on my Mac if I run the above command:
If your run the command again, it won’t pull (download) the image, because you already have it. Also, because the C file was not changed it will use the already created executable:
Change the C file, for example, you can print Hello, World from Docker!, save it and run the command again:
This time the app was first recompiled and than executed.
While this approach is slower than using GCC directly on your macOS, it has the advantage that it doesn’t mess with your system and that it can be easily removed if necessary.
If you want to learn more about Docker, I recommend reading Docker: Up & Running by S. P. Kane and K. Matthias: