One of the key advantages of using Docker is the ability to easily manage and scale multiple containers. This is where Docker Compose comes in. Docker Compose is a tool that allows you to define and manage multiple containers as a single unit, using a simple YAML file.

In this blog post, we will go over the steps to use Docker Compose for the first time, using the popular Watchtower as a demonstration.

Watchtower is a container-based solution for automating Docker container base image updates.

1: Install Docker

The first step is to install the Docker engine on your machine. Docker is available for various operating systems, including Windows, macOS, and Linux. You can visit the Docker website to download the appropriate installer for your system.

Once the installation is complete, you can verify that Docker is running by running the following command in your terminal:

docker -v

This should display the version of Docker that is installed on your machine.

2: Install Docker Compose

Next, you will need to install Docker Compose on your machine. Docker Compose is included in the Docker installation on Windows and macOS, but you will need to install it separately on Linux. You can visit the Docker Compose documentation to learn more about the installation process.

I personally use a Linux based OS for my containers. If you are using Linux as well you can use this command:

sudo apt install docker-compose

Once Docker Compose is installed, you can verify that it is working by running the following command in your terminal:

docker-compose -v

This should display the version of Docker Compose that is installed on your machine.

3: Create a Docker Compose file

The next step is to create a docker-compose.yml file that defines the different containers and their dependencies. This file will be used by Docker Compose to spin up and manage the containers.

A basic Docker Compose YAML might look something like this:

version: "3.3"
services:
watchtower:
container_name: watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
image: containrrr/watchtower

 

4: Run Docker Compose

Once you have created your Docker Compose file, you can use it to spin up and manage your containers. This is done using the docker-compose command, followed by the desired action.

To spin up the containers defined in your Docker Compose file, you can run the following command in the same directory as your docker-compose.yml file:

docker-compose up -d

This will start the Nginx container and run it in the foreground. You can add the -d flag to run the container in detached mode, which will allow you to continue using the terminal while the container is running.

5. Basic Docker Commands

To see if your Docker container is running, you can use the following commands:

  1. docker ps – This command lists all running containers, including their names, IDs, and the image they were created from.
  2. docker container ls – This command is similar to docker ps, but it also shows stopped containers.
  3. docker inspect <container name> – This command allows you to view detailed information about a specific container, including its state, configuration, and runtime information.
  4. docker logs <container name> – This command allows you to view the logs for a specific container, which can be useful for troubleshooting issues.
  5. docker top <container name> – This command shows the processes running inside a specific container, including their CPU and memory usage.

Using one or more of these commands can help you determine if your Docker container is running, and if not, you can use the output of these commands to troubleshoot and fix any issues that may be preventing your container from starting.

6. Stop and remove the containers

When you are done using the containers, you can use the docker-compose down command to stop and remove them. This will remove the containers and their associated networks, volumes, and images.

docker-compose down

– I hope this was a helpful introduction to Docker Compose.