How to install and use Docker Compose on Ubuntu 18.04

s2 0

Docker Compose
is a tool that allows you to define and manage Docker applications with multiple containers. It uses a YAML file to configure the application’s services, networks, and volumes.

Compose can be used for a variety of purposes. Single host application deployments, automated testing, and on-site development are the most popular use cases for Docker Compose.

In this tutorial, we are going to show you how to install the latest version of Docker Compose on Ubuntu 18.04 and explore the basic concepts and commands of Docker Compose.

The same instructions apply to Ubuntu 16.04 and any other Debian-based distribution, including Debian, Linux Mint, and Elementary OS.

requirements

Make sure you meet the following requirements before proceeding with this tutorial:

  • Logged in as a user with sudo permissions.
  • Have Docker installed according to the instructions in How to Install and Use Docker on Ubuntu 18.04.

Install Docker Compose on Ubuntu

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 Docker’s GitHub repository.

At the time of this writing, the latest stable version of Docker is Compose version 1.23.1. Before downloading the Compose binary, visit the Create a repository release page on GitHub
and check if a new version is available for download.

To install Docker Compose on Ubuntu 18.04, do the following:

  1. Load the Docker Compose binary into the /usr/local/bin Directory with the following curl
    Command:

    sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. After the download is complete, 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 shows the Compose version:

    docker-compose --version

    The output looks something like this:

    docker-compose version 1.23.1, build b02f1306

Get started with Docker Compose

In this section we show how to use Docker Compose to set up a multi-container WordPress application on Ubuntu 18.04.

First create a project directory and navigate into it:

mkdir my_appcd my_app

Start 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.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 code line by line.

In the first line we give the Create file version
. There are several different versions of the Compose file format with support for specific Docker versions.

Next we define two services, db and wordpress. Each service runs an image and creates a separate container when docker-compose is run.

the db Service:

  • Uses the mysql:5.7

    Image. If the image does not exist on the system, it is pulled from the Docker Hub public repository.

  • Uses the restart always Policy that tells 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 does not exist on your system, Compose will pull it from the Docker Hub public repository.

  • Uses the restart always Policy that tells the container to always restart.
  • Assembles the wp_data Directory on the host /var/lib/mysql inside the container.
  • Forward exposed port 80 on the container to port 8080 on the host computer.
  • Defines the environment variables for the wordpress Image.
  • the depends_on Statement defines the dependency between the two services. In this example, db is started beforehand wordpress.

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

docker-compose up

The output should look something like this:

...
wordpress_1  | [Sun Sep 23 22:31:43.499055 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations
wordpress_1  | [Sun Sep 23 22:31:43.499796 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

Compose pulls both images, starts two containers and creates the wp_data Directory in your project 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 up and running 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 Flag.

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 using the services:

docker-compose stop

If you want to remove the containers completely, use the down Opportunity:

docker-compose down

Existence of --volumes switch also removes the data volumes:

docker-compose down --volumes

Uninstall Docker Compose

If for any reason you’d like to uninstall Docker Compose, you can simply remove the binary by typing:

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

diploma

You learned how to install and use Docker Compose on Ubuntu 18.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.