Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
In this tutorial, we’ll show you how to install the latest version of Docker Compose on CentOS/ RHEL or Ubuntu 16.04/ Ubuntu 18.04 and explore the basic Docker Compose concepts and commands.
Make sure that you have met the following prerequisites before continuing with this tutorial:
- Logged in as a user with sudo privileges.
- Docker Engine installed. Follow our previous tutorial about How To Install and Use Docker CE on Linux
Step 1 — Installing Docker Compose
In order to get the latest release of Docker Compose, go to the Docker’s GitHub repository site.
01- Start by downloading the Docker Compose binary into the
/usr/local/bin directory using the following
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
02- Next, set the permissions to make the binary executable:
$ sudo chmod +x /usr/local/bin/docker-compose
03- Then, verify that the installation was successful by checking the version:
$ sudo docker-compose --version docker-compose version 1.25.4, build 4667896b
04- Furthermore, to run Compose as a non-root user, add your user to the
docker group as below.
$ sudo usermod -aG docker $USER
Step 2 – Getting started with Docker Compose
In this section, we’ll show how to use Docker Compose to run a Nginx container and expose it on your host machine.
01- Create a new directory named
nginx and move into it:
$ mkdir ~/nginx && cd ~/nginx
02- Next, create
docker-compose.yml file in your new directory and add the following lines:
$ vi docker-compose.yml version: '3' services: web: image: nginx:latest ports: - "8000:80"
– The first line specifies the Compose file version. There are several different versions of the Compose file format with support for specific Docker releases. For full details, check Compose and Docker compatibility matrix.
In the second section of services, we are are going to define a service called web. When
docker-compose is run, will create a separate container based on the latest official Nginx image and it will expose the port 80 on the container to port 8000 on the host machine.
03- After saving the file, start the Nginx container as a background process with the following command:
$ docker-compose up -d Creating network "nginx_default" with the default driver Pulling web (nginx:latest)... latest: Pulling from library/nginx fc7181108d40: Pull complete d2e987ca2267: Pull complete 0b760b431b11: Pull complete Digest: sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223a Status: Downloaded newer image for nginx:latest Creating nginx_web_1 ... done
– Now, to check if the container is runnung and up, use the below command:
$ docker-compose ps Name Command State Ports ----------------------------------------------------------------- nginx_web_1 nginx -g daemon off; Up 0.0.0.0:8000->80/tcp
04- Finaly, open the url
http:IP_Docker:8000 and you should see the below screen:
Uninstalling Docker Compose
If for any reason you want to uninstall Docker Compose you can simply delete the binary directory by typing the below command:
$ sudo rm /usr/local/bin/docker-compose
And that’s a wrap! You have successfully learnt how to install Docker Compose on Linux System and how to define and run your first container easily using a YAML file. Next, we will explore some basic Docker Compose commands.
What’s Next? You might want to check the following guides: