How to install and use Docker Compose on Ubuntu 20.04

Introduction: Docker Compose is a command-line tool through which you can define and arrange multi container Docker applications. This article will explain how to install the latest version of Docker Compose on Ubuntu 20.04.

For image download, domain name resolution and time synchronization, please click Alibaba open source mirror station

Docker Compose is a command-line tool through which you can define and orchestrate multi container docker applications. It uses YAML files to configure application servers, networks and data volumes. Compose is usually used for local development, stand-alone application deployment, and automatic testing. With compose, you can define a portable application environment that can run on any system. This article explains how to install the latest version of Docker Compose on Ubuntu 20.04.

1, Preconditions

2, Install Docker Compose on Ubuntu

Docker Compose is a binary file. The installation is very simple and direct. We will download the file to a directory, add it to the PATH environment variable of the system, and set the file to executable.

Docker Compose  The software package is in the official Ubuntu 20.04 Available in the source repository, but may not be the latest version.

At the time of writing this article, the latest version of Docker Compose is 1.25.5.
Before downloading Compose, browse Publish page And check whether there is a new version to download.
Use curl to download the Compose file to the / 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 downloading, set the file to executable:

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

Run the following command to verify the successful installation and check the version of Compose:

docker-compose --version

The output interface is as follows:

docker-compose version 1.25.5, build b02f1306

3, Introduction to Docker Compose

Next, we will use Docker Compose to build a multi container WordPress application.

  1. Create a project directory:
mkdir my_app
cd my_app
  1. Open your text editor and create a file called docker - compose YML files are placed in the project directory:
nano docker-compose.yml
  1. Paste the following:
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:

docker-compose. The first line of the YML file specifies  Compose file Version of. There are different versions of Compose, and each version supports the specified Docker distribution.

  1. Configure the server, db# and wordpress.

When docker compose runs, each server runs an image and creates an independent container.
The server can use images available on DockerHub or images built locally from Dockerfile files. In addition, you can also specify some settings, such as exposure port, data volume, environment variable, dependency, and other Docker commands.
Run the following command in the project directory to start the WordPress application:

docker-compose up

Compose pulls the image, starts the container, and creates wp_data directory.
Enter in your browser[ http://0.0.0.0:8080/](http://0.0.0.0:8080/ ), you will see the WordPress installation screen. At this point, the WordPress application has been started and running, and you can start installing themes or plug-ins. You can press CTRL+C to stop Compose.
You can also start Compose in background mode by adding the - d option after Compose:

docker-compose up -d

Using the ps option, check the running services:

docker-compose ps

The output is as follows:

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

Run the following command to stop the service:

docker-compose stop

You can also use the down command to stop and remove the application container and network

docker-compose down

4, Uninstall Docker Compose

To uninstall Docker Compose, simply delete the binary file and enter the following command:

sudo rm /usr/local/bin/docker-compose

5, Summary

So far, we have shown you how to install Docker Compose on Ubuntu 20.04. Using Docker Compose can significantly improve your workflow and improve your work efficiency. You can use Docker Compose to define a development environment and share it with project partners.

This article is transferred from: How to install and use Docker Compose - Alibaba cloud developer community on Ubuntu 20.04

Keywords: Linux Docker Ubuntu

Added by SpaceLincoln on Tue, 15 Feb 2022 16:00:16 +0200