Docker Compose is a command line tool that lets you define and organize multi-container Docker applications. It uses a YAML file to configure the services, networks and volumes of the application.
With Compose, you can define a portable application environment that can run on any system. Composite environments are isolated from each other, allowing multiple copies of the same environment to run on the same host.
Compose is commonly used for local development, single host deployment, and automated testing.
This article explains how to install the latest Docker Compose on Ubuntu 20.04. We’ll also cover the basic concepts and commands of Docker Compose.
We will assume that you have Docker installed on your Ubuntu machine.
Installing Docker Compose on Ubuntu
Docker Compose is one binary. Installation is simple. We will upload the file to the directory that is in the system PATH and make it executable.
The Docker Compose package is available in the official Ubuntu 20.04 repositories, but it may not always be the latest version.
At the time of this writing, the latest stable version of Docker Compose is 1.25.5. Before downloading the Compose binary, go to the Compose repository release page on GitHub and check if a new version is available for download.
Use curl command to download the Compose file to / usr / local / bin directory:
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
After the download is complete, apply the permissions to the executable:
sudo chmod +x /usr/local/bin/docker-compose
To verify that the installation was successful, enter the following command, which will display the Compose version:
The output will look something like this:
docker-compose version 1.25.5, build b02f1306
All! Docker Compose is installed on your Ubuntu machine and you can start using it.
Getting started with Docker Compose
In this section, we will be using Docker Compose to create a WordPress multi-container CMS.
The first step is to create a project directory:
mkdir my_appcd my_app
Open your text editor and create a file called docker-compose.yml inside your project directory:
Paste in the following content:
version: '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:
Let’s analyze the structure of the docker-compose.yml file.
The first line of the file indicates the version of the Compose file. There are several different versions of the Compose file format with support for specific versions of Docker.
Next, you define services, volumes and networks.
In this example, we have db services, and wordpress. Each service runs a single image and creates a separate container when running docker-compose.
Services can use images available on DockerHub or images generated from a Dockerfile. The service section also includes keys that indicate open ports, volumes, environment variables, dependencies, and other Docker commands.
From your project directory, start your WordPress application by running the following command:
Compose will fetch images, launch containers and create a wp_data directory.
Enter http://0.0.0.0:8080/ into your browser and you will see the WordPress installation screen. At this point, your WordPress application is up and running and you can start working on your theme or plugin.
Press CTRL + C to stop Compose.
You can also start Compose in a separate mode by passing the -d option:
docker-compose up -d
Use the ps option to check running services:
Name Command State Ports ---------------------------------------------------------------------------------- my_app_db_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp my_app_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp
With Compose offline, to stop the services, run:
To stop and remove app and network containers use the down parameter:
Removing Docker Compose
To uninstall Docker Compose, simply delete the binary by typing:
sudo rm /usr/local/bin/docker-compose
We showed you how to install Docker Compose on Ubuntu 20.04. Using Docker Compose can significantly improve your workflow and productivity. You can define your development environment with Docker Compose and share it with the project collaborators.
If you have any questions, please leave a comment below.