Featured image of post How to Install and Use Docker Compose

How to Install and Use Docker Compose

Docker Compose is a tool for defining and running multiple Docker containers, allowing you to easily manage multiple container applications on a single host...

Docker Compose is a tool for defining and running multiple Docker containers, enabling you to easily manage multiple container applications on a single host. In this article, I will guide you through the installation of Docker Compose and how to use it to manage multiple Docker containers.

First, you need to install Docker. Depending on your operating system, you can choose different installation methods. On Windows and macOS, you can use Docker Desktop to install Docker. On Linux, you can make use of package managers like apt-get or yum.

Next, here are the steps to install Docker Compose:

Method 1: Install Using pip

  1. Open your terminal or command line window.
  2. Check if your computer has Python and pip installed, as these two tools are prerequisites for installing Docker Compose. You can verify this with the following commands:
1
2
$ python --version
$ pip --version
  1. If Python and pip are installed, use the following command to install Docker Compose:
1
$ sudo pip install docker-compose

If you encounter permission issues, prepend sudo to the command. 4. After the installation is complete, you can check if Docker Compose is installed correctly with the following command:

1
$ docker-compose --version

Method 2: Install Using the Binary Package

First, you need to download the Docker Compose binary package for Linux from the official Docker website. You can visit the page at https://github.com/docker/compose/releases, find the appropriate Compose version for your system, and download it.

  1. Run the following command to download the current stable version of Docker Compose:
1
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

To install a different version of Compose, replace 1.25.0 with the version you wish to install.

If you encounter issues using curl, please refer to the β€œAlternative Installation Options” section above. 2. Apply executable permissions to the binary file:

1
sudo chmod +x /usr/local/bin/docker-compose

Note: If the docker-compose command fails after installation, please check your PATH. You can also create a symbolic link pointing to /usr/bin or any other directory in your PATH.

For example:

1
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  1. (Optional) Install command completion for bash and zsh shells by following the command completion instructions.
  2. Test the installation:
1
$ docker-compose --version

You should see output similar to:

1
docker-compose version 1.25.0, build 1110ad01

Now that you have successfully installed Docker Compose, let’s explore how to use Docker Compose to define and run multiple Docker containers.

  1. Create a Docker Compose file. Docker Compose uses YAML files to define and configure multiple containers. You can create a file named docker-compose.yml in the directory where you want to manage multiple containers. Here’s an example of a docker-compose.yml file:
1
2
3
4
5
6
7
8
version: '3.8'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this example, we defined two services: web and redis. The web service builds an image using the Dockerfile in the current directory and maps port 5000 on the host to port 5000 in the container. The redis service uses the official Redis image.

  1. Start the containers with the following command:
1
$ docker-compose up

If you only want to start a specific service, you can use:

1
$ docker-compose up <service-name>

Before starting, Docker Compose will automatically build any missing images.

  1. Stop the containers. You can stop all running containers using:
1
$ docker-compose down

If you only want to stop a specific service, you can run:

1
$ docker-compose down <service-name>

That’s the complete process of using Docker Compose to define and run multiple Docker containers. I hope this information helps you!

Licensed under CC BY-NC-SA 4.0