Introduction
Docker Compose is used to run multi-container Docker applications. This means that each container will run a standalone application which can also communicate with the other containers present in the same host. Docker Compose uses YAML files to configure all of your Docker containers and configurations.
In this tutorial, we’ll show you how to install the latest version of Docker Compose to help you manage multi-container applications.
Before We Begin
To follow this article, you will need an Ubuntu 18.04 server with the following:
- A non-root user with sudo privileges
- Docker installed (If not Follow our tutorials on How to install Docker on ubuntu
Step 1 : Installing Docker Compose
The Docker Compose installation package is available in the official Ubuntu 18.04 repositories but it may not always be the latest version. The recommended approach is to install Docker Compose from the Docker’s GitHub repository.
At the time of writing this article, the latest stable version of Docker Compose is version 1.26.2
Before downloading the Compose binary visit the Compose repository release page on GitHub and check if there is a new version available for download.
To install Docker Compose on Ubuntu 18.04, follow these steps:
1.Download the Docker Compose binary into the /usr/local/bin
directory with the following curl command:
Note :If curl is not installed install using command sudo apt install curl
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This will download Docker Compose version 1.26.2
- The
–L
option tells the system to follow any redirects, in case the file has been moved - If you want a different version of Docker Compose, you may browse the list and substitute your preferred version for /1.26.2/
- The
–o
to specify the output file first rather than redirecting the output, this syntax avoids running into a permission denied error caused when usingsudo
2. Once Download is completed ,apply executable permissions to the Compose binary:
sudo chmod +x /usr/local/bin/docker-compose
3.Verify the installation by running the following command which will display the Compose version:
docker-compose --version
The output will look something like this:
docker-compose version 1.26.2, build a133471
Now that we have Docker Compose installed
Step 2:Getting started with Docker Compose
In this section, we will show how to use Docker Compose to set up a multi-container wordpress application on ubuntu 18.04.
Define the project:
Start by creating a project directory and navigating into it:
mkdir my_wordpress cd my_wordpress
There are two key forms of Docker Compose that you’ll use in your project:
- Docker Compose comes in the form of a command line command that you’ll use to build, ship and run multi-container applications. The command is called
docker-compose
. - Docker Compose provides the Compose file, where you’ll define your application stack and the way components in your stack interact with each other. The Compose file is a text file in the
yml
format.
Adding the Compose file
In order to make the application work we have to add the Compose file to glue all the components together. Let’s create a file called docker-compose.yml
in our project directory.
Note:The name
docker-compose.yml
is the default name of the compose file. Thedocker-compose
command will look for the Compose file by this name.
Use any preferred text editor and add below contents to our docker-compose.yml file.
nano docker-compose.yml
version: '3.3' services: db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: wordpress wordpress: image: wordpress restart: always volumes: - ./wp_data:/var/www/html ports: - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: password depends_on: - db volumes: db_data: wp_data:
This file is in the YAML
format, which is a “human friendly data serialization standard for all programming languages”
Now Let’s analyse the each content in the yaml file:
- In the first line, we are specifying the Compose file version . There are several different versions of the Compose file format with support for specific Docker releases.
Note:
If you don’t specify the version in the Compose file it will be considered
version: 1
.The Compose file uses a two digit versioning notation. The current latest version is 3.8. It is enough to specify the major version (i.e.version: 3
) in your Compose file, but for certain new features you may have to add the exact two digit version number, likeversion:
3.8.
- In services section we are defining two services,
db
andwordpress
. Each service runs one image and it will create a separate container when docker-compose is run.
The db
service:
- Uses the
mysql:5.7
image. If the image is not present on the system it will be pulled it from the Docker Hub public repository. - Uses the restart
always
policy which will instruct the container to always restart. - Creates a named volume
db_data
to make the database persistent. - Defines the environment variables for the
mysql:5.7
image.
The wordpress
service:
- Uses the
wordpress
image. If the image is not present on your system Compose will pull it from the Docker Hub public repository. - Uses the restart
always
policy which will instruct the container to always restart. - Mounts the
wp_data
directory on the host to/var/lib/mysql
inside the container. - Forwards the exposed port 80 on the container to port 8080 on the host machine.
- Defines the environment variables for the
wordpress
image. - The
depends_on
instruction defines the dependency between the two services. In this example,db
will be started beforewordpress
.
Note: One service in a Compose file is supposed to serve one concern of your architecture. You can specify exactly one
image
per service. So, a simple rule of thumb is to create one service for each image in your application.
Run the project :
From the project directory, start up the WordPress application by running the following command:
docker-compose up
You will get output look like :
Compose will pull both images, start two containers and create the wp_data
directory in your project directory.
Check the newly created container status
docker ps
As you can see our both containers are up and running .
Enter http://0.0.0.0:8080/
in your browser and you will see the WordPress installation screen.
At this point the WordPress application is up and running and you can start working on your theme or plugin.
How are the containers connected:
As you can see, the containers are automatically connected when we run the stack with docker-compose up
.Our wordpress container can access MySQL container without any configuration. It’s crucial to understand the mechanism behind this magic.
Docker is creating two containers as expected, but it’s also creating something else; Docker created a default network for our application components.
The two containers are connected to the network named mywordpress_default.This network was created by Docker Compose automatically for our stack.
Note:The network mywordpress_default is isolated from other networks on the host machine, which means that no other containers may access containers on this network without permission.
Containers on this network can refer to each other by service name. This means that if I want to access the MySQL container from my wordpress app, I can use the service name db as the hostname to access the container.
This way you can easily connect containers to each other with Docker Compose. You can also specify custom, more complex network setups beyond the default network.
manage docker-compose
To stop Compose press CTRL+C
or use command docker-compose down
docker-compose down
Note:The command
docker-compose down
removes the containers and default network, but preserves your WordPress database.
You can also start the Compose in a detached mode by passing the -d
flag.
docker-compose up -d
To make sure containers are running services use the ps
option:
docker-compose ps
Note:Only perform docker-compose commands on the location where it is placed,Normally it should be inside of project directory
When compose is running in detached mode,to stop use :
docker-compose stop
If you want to remove the containers entirely use the down
option:
docker-compose down
Passing the --volumes
switch will also remove the data volumes:
docker-compose down --volumes
Uninstalling Docker Compose
If for any reason you want to uninstall Docker Compose you can simply remove the binary by typing:
sudo rm /usr/local/bin/docker-compose
Conclusion
In this tutorial, we learned how to install and use Docker-compose in ubuntu 18.04. We have also found how to manage docker-compose for different containers with a sample example. To learn more about Docker Compose visit Docker compose official documentation