How to use Docker Compose

Docker Compose is one of the most useful tools for developers and sysadmins. In fact, most IT jobs require knowledge of Docker and Docker Compose. Knowing how to use these technologies will undoubtedly be an advantage in continuing your career.

What is Docker compose?

In a nutshell, Docker Compose is a tool that allows you to manage multiple Docker containers. Do you remember micro-services, i.e. the concept of dividing a web application into different services? Well, these services run in individual containers that must be managed and must be configured to communicate with each other. Docker compose allows you to do just that.

Imagine having an application with several separate services, such as authentication, registration, etc.

Following a micro-services architecture, each element will run in a separate container. Docker Compose makes it easy to manage all of these containers instead of managing them individually.

Installing Docker compose

Docker Compose is an official Docker tool, but it does not come with the Docker installation by default. As such, you need to install it as a separate package. The Docker Compose installation process for Windows and Mac is available on the official Docker site.

For installation on Ubuntu, you just need to launch the installation from the command line:

$ sudo apt-get install docker-compose

The docker-compose.yaml file

It wouldn’t be wrong to say that a Docker Compose file is to Docker Compose what a Dockerfile is to Docker. Inside the Docker Compose file, you will find all the instructions that Docker Compose executes when managing containers. The services that have to be launched in the containers are defined here. The networks and volumes on which the services depend are also defined.

The Docker Compose file uses YAML syntax and must be saved as docker-compose.yml. For example, you may have backend, frontend, database and message queue services in a web app. These services will need specific dependencies, such as networks, ports and memory for optimal functioning. You must always bear in mind that everything needed for the entire application will be defined in the Docker Compose file.

For a concrete example of drafting a Docker compose file you can refer to this article.

Docker compose commands

After creating a docker-compose.yml file, you need to run certain commands for it to work.

docker-compose up

This Docker-compose command helps build the image, after which it creates and launches the Docker containers. The containers come from the services specified in the configuration file. If the containers are already running and you run docker-compose up, the container is recreated.

docker-compose start

This Docker compose command starts Docker containers, but does not create images or create containers – it only starts containers if they have been created previously.

docker-compose stop

Containers often need to be stopped after they are created and started. This is where Docker’s shutdown command comes in handy. This command basically stops any services running while the installation containers and networks remain intact.

docker-compose down

The docker-compose down command also stops Docker containers, as does the “stop” command. Not only that, it doesn’t just stop the containers, it also removes them. This also applies to Docker networks, volumes and images, which can be removed if certain arguments are used.

docker-compose down –volumes

Removes all volumes.

docker-compose down –rmi all

Removes all images.

In conclusion

You don’t need to be a Docker expert to use Docker Compose. If you are a beginner, it is best to try writing a docker-compose.yaml to launch two or more containers.

In this article, you learned the basics of Docker Compose. Now you know what it’s for, how to set up a Docker Compose configuration file and the main commands. That means it’s time to get down to business and try Docker Compose in practice!

Share: Facebook Twitter Linkedin

Comments