How to install Docker Compose on Ubuntu 20.04

s2 0

Docker Compose
is a command line tool that you can use to define and orchestrate multi-container Docker applications. It uses a YAML file to configure the application’s services, networks, and volumes.

With Compose, you can define a portable application environment that you can run on any system. Compose environments are isolated from one another, so you can run multiple copies of the same environment on a single host.

Compose is typically used for local development, single-host application deployment, and automated testing.

This article explains how to install the latest version of Docker Compose on Ubuntu 20.04. We’ll also explore the basic Docker Compose concepts and commands.

requirements

We assume that Docker is installed on your Ubuntu machine.

Install Docker Compose on Ubuntu

Docker Compose is a single binary file. Installation is straightforward. We download the file to a directory in the PATH system 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 is Compose 1.25.5. Before downloading the Compose binary, visit the Create a repository release page on GitHub
and check if a new version is available for download.

Use curl
to download the compose file into 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 the download is complete, apply the executable permissions to the file:

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

To verify that the installation was successful, run the following command that prints the Compose version:

docker-compose --version

The output looks something like this:

docker-compose version 1.25.5, build b02f1306

That’s it! Docker Compose has been installed on your Ubuntu computer and you are ready to use it.

Get started with Docker Compose

In this section we will use Docker Compose to build a multi-container WordPress application

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 in the project directory:

nano docker-compose.yml

Paste the following content:

docker-compose.yml
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 gives the version of the Create file
. There are several different versions of the Compose file format with support for specific Docker versions.

Next you define services, volumes and networks.

In this example we have services db, and wordpress. Each service runs an image and creates a separate container when docker-compose is run.

Services can use images available on DockerHub or images created from the Dockerfile. The services section also contains keys that identify exposed ports, volumes, environment variables, dependencies, and other Docker commands

From the project directory, start the WordPress application by running the following command:

docker-compose up

Compose loads the images, starts the containers and creates the wp_data Directory.

Enter https://0.0.0.0:8080/ in your browser and you will see the WordPress installation screen. At this point the WordPress application is ready to use and you can start working on your theme or plugin.

To stop Compose, press CTRL+C.

You can also start Compose in a disconnected mode by running the -d Opportunity:

docker-compose up -d

To check the running services, use the ps Opportunity:

docker-compose ps
       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

If Compose is running in disconnected mode to stop the services, do the following:

docker-compose stop

To stop and remove the application containers and networks, use the down Opportunity:

docker-compose down

Uninstall Docker Compose

To uninstall Docker Compose, simply remove the binary by typing:

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

diploma

We showed you how to install Docker Compose on Ubuntu 20.04. Using Docker Compose can greatly improve your workflow and productivity. You can define your development environment with Docker Compose and share it with the project team.

If you have any questions please leave a comment below.

Tags: