Both Docker Compose and the Compose CLI are built using Go. Compose can be run on the three major operating systems: Linux, Windows, and macOS. Since Compose is about managing multi-container Docker applications, the prerequisite is to have Docker installed.
Docker Desktop
On Mac and Windows, Docker Desktop is an installation option. Docker Desktop handles the complexity of setting up Docker on your local machine. It will create a Linux Virtual Machine (VM) on your host and facilitate container interactions with the OS such as access to the filesystem and networking. This one-click installation comes with the necessary tools such as the Docker CLI. One of the tools that is included is also Docker Compose. Therefore, installing Docker Desktop makes it sufficient to interact with Docker Engine using Compose on our workstation.
Installing Docker
To install the correct Docker distribution for the workstation of our choice, we will navigate to the corresponding section of the official Docker page:
On macOS
Apple provides workstations with two different types of processors: an Intel processor and an Apple processor. Docker has an installation option for both. Once the download is complete, by clicking on the installer, you can drag and drop the Docker application, as shown in the following screenshot:
Figure 1.1 – Installing Docker on Mac
Once Docker has been installed, we can run a hello world
command check:
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
93288797bd35: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea 0fecc38e597b2209f49a
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
..
Additionally, we have to check whether Compose has been installed:
$ docker compose version
Docker Compose version v2.2.3
Now, let’s look at how to install Docker Desktop on Windows.
On Windows
Similar to Mac, Docker Desktop is installed seamlessly onto your OS ready to be used.
Once you download the EXE installation file and click on it, Docker will be installed along with its utilities. Once this is done, some extra configurations will need to be applied to enable virtualization for Windows.
Whether the backend that’s being used is WSL 2 backend or Hyper-V, you have to set up your machine BIOS to enable virtualization, as shown in the following screenshot:
Figure 1.2 – Enabling virtualization on Windows via BIOS
Once you have logged in to Windows, you will need to enable the corresponding virtualization features.
For WSL 2, you should enable the Virtual Machine Platform feature and the Windows Subsystem for Linux feature:
Figure 1.3 – Enabling virtualization for WSL 2
For Hyper-V you should enable Hyper-V:
Figure 1.4 – Enabling virtualization for Hyper-V
Before you get started, make sure that your user account is added to the docker-users
group. Once done, log out from Windows and log in again. You can start Docker, and then you can execute your first Docker command on PowerShell, as follows:
PS C:\Users\my-user> docker run -d -p 80:80 docker/getting-started
Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
59bf1c3509f3: Pull complete 8d6ba530f648: Pull complete 5288d7ad7a7f: Pull complete 39e51c61c033: Pull complete ee6f71c6f4a8: Pull complete f2303c6c8865: Pull complete 0645fddcff40: Pull complete d05ee95f5d2f: Pull complete Digest: sha256:aa945bdff163395d3293834697fa91fd4c725f47093ec499 f27bc032dc1bdd16
Status: Downloaded newer image for docker/getting-started:latest
852371fcb34fddfe900bddc669af3a7aaab8743f8555fbb9952904bd2516a e7a
PS C:\Users\my-user>
Let’s also check whether Docker Compose has been installed:
PS C:\Users\my-user> docker compose version
Docker Compose version v2.2.3
Next, we will look at how to install Docker Desktop on Linux.
On Linux
At the time of writing, a Docker Desktop installation for Linux is not available, but it’s on the roadmap, and it’s just a matter of time before it’ll be available for Linux. However, Docker Engine is sufficient in order to use Docker Compose.
The most common method of installation is to add the Docker repositories to your Linux workstation and then install Docker Community Edition using the corresponding package manager of the distribution used.
If you have an older version of Docker, you should remove and install the new docker-ce
and docker-ce-cli
versions. We will assume that this is the first Docker installation on the workstation we are currently using.
Since Red Hat-based Linux distributions are very popular for both workstations and production usage, we will install Docker on Fedora, which is a Red Hat-based distribution.
First, install the dnf-plugins-core
package since it contains tools that can assist us with the management of the dnf
repositories:
$ sudo dnf -y install dnf-plugins-core
Then, add the docker-ce
repo to access the binaries provided by Docker:
$ sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
Now that the repo has been set up, we can add the packages:
$ sudo dnf install docker-ce docker-ce-cli containerd.io -y
Docker is a daemon that will run as a service to our machine. Therefore, the systemctl
commands apply to Docker running as a device:
$ sudo systemctl start docker
Let’s run a hello-world
example:
$ sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
…
As you can see, we had to use sudo
in almost every command. This can be fixed by having a group called docker
, in which users will have the permission to interact with Docker Engine. On the installation of Docker Engine, this group will be created:
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ docker run hello-world
Once installed, everything is set up to install Compose on Linux.
We will proceed with the installation link at https://docs.docker.com/compose/install/#install-compose-on-linux-systems:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose —version
docker-compose version 1.29.2, build 5becea4c
Here, we can observe that this is an older version of Compose compared to the ones that we saw earlier. There isn’t a standard way to install Compose V2 on Linux, for instance, by installing Docker Desktop on Mac and Windows. However, since it’s feasible to install Compose V2 on Linux, we will proceed in doing so, allowing us to focus on Compose V2.
We will follow the guidelines from the official documentation at https://docs.docker.com/compose/cli-command/#install-on-linux:
$ mkdir -p ~/.docker/cli-plugins/
$ curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
$ chmod +x ~/.docker/cli-plugins/docker-compose
$ docker compose version
Docker Compose version v2.2.3
docker compose versus docker-compose
One observation to be made by navigating to the installation instructions for Linux is that a Python version of docker compose
has been installed.
Also, this same version can be found on a Windows installation if you try to use the docker-compose
command on Windows:
PS C:\Users\my-user> docker-compose-v1.exe version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1g 21 Apr 2020
PS C:\Users\my-user>
The initial Docker Compose was built in Python; therefore, the installation instructions referenced the installation of pip
packages.
Note that for new installations of Docker Desktop, the docker-compose
command is an alias to docker compose
.
The initial version of Compose’s docker-compose
is still supported and maintained. In the case of Compose applications built and run using docker-compose
, there are supporting tools available such as Compose Switch (https://docs.docker.com/compose/cli-command/#compose-switch) for a smooth migration.
By installing Compose Switch, the old docker-compose
command will be replaced by the compose-switch
command.
Compose Switch will interpret the command that should have been passed to docker-compose
. Then, it will translate it into a command that can be executed by Compose V2. Then, it will invoke Compose V2 using that command.
In this book, we shall focus on Compose V2 since it's part of docker-cli
. This is the default on Docker Desktop, has the latest features, and comes with extra commands.
By now, you should have Docker and Docker Compose installed on your workstation and know how to execute some basic commands. You should also understand the previous Compose version and how you can transition to the latest version. Next, we’re going to take a deeper dive into how Compose works and how it interacts with Docker Engine.