Thursday , July 29 2021

How to install and use Docker Compose on Ubuntu 18.04

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Use a YAML file to configure the services, networks and application volumes.

Componi can be used for different purposes. Deployments of single host applications, automated testing and local development are the most popular use cases for Docker Compose.

In this tutorial we will 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.


Make sure you have satisfied the following prerequisites before continuing with this tutorial:

Install Docker Compose on Ubuntu

The Docking Compose installation package is available in the official Ubuntu 18.04 repositories but may not always be the latest version. The recommended approach is to install Docker Compose from the Docker GitHub repository.

At the time of writing this article, the latest stable version of Docker Compose is the version 1.22.0. Before downloading the Compose installer script, visit the Compose repository release page on GitHub and see if a new version is available for download.

To install Docker Compose on Ubuntu 18.04, follow these steps:

  1. Download the Docker Compose in the / Usr / local / bin directory with the following curl command:

    sudo curl -L "$ (uname-s)-$ (uname -m)" -o / usr / local / bin / docker-compose
  2. Once the download is complete, provide the permissions that can be performed on the Compose binary:

    sudo chmod + x / usr / local / bin / docker-compose
  3. Verify the installation by typing:

    The output will be similar to this:

    docker version-dial 1.22.0, create f46880fe


Start with Docker Compose

In this section we will show how to use Docker Compose to run a WordPress stack on your Ubuntu 18.04 machine.

Start by creating a directory for the project:

Open your text editor and create a file called mobile window-compose.yml in the directory of your project:

Paste the following content:

version: & # 39; 3.3 & # 39;

    Image: mysql:5.7
    restart: always
      - db_data:/ Var / lib / mysql
      MYSQL_ROOT_PASSWORD: order word
      MYSQL_DATABASE: wordpress

    Image: wordpress
    restart: always
      - ./wp_data:/ Var / www / html
      - "8080: 80"
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_PASSWORD: order word
    depend on:
       - db


We analyze the code line by line.

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

Then we are defining two services, db is wordpress. Each service performs an image and will create a separate container when the compose window is executed.

The db service:

  • Use the mysql: 5.7 Image. In the image is not present on your system Compose will extract it from the public archive Docker Hub.
  • Use restart always policy that will instruct the container to always restart.
  • Create a volume named db_data to keep the database.
  • Defines the environment variables for the mysql: 5.7 Image.

The wordpress service:

  • Use the wordpress Image. In the image is not present on your system Compose will extract it from the public archive Docker Hub.
  • Use restart always policy that will instruct the container to always restart.
  • Mount the wp_data directory on the host a / Var / lib / mysql inside the container.
  • Forward the exposed port 80 on the enclosure to port 8080 on the host machine.
  • Defines the environment variables for the wordpress Image.
  • The depend on The instruction defines the dependence between the two services. In this example, db will be started first wordpress.

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

The output should be similar to this:

Starting my_app_wordpress_1 ... completed
Linking to my_app_db_1, my_app_wordpress_1
db_1 | 2018-09-23T22: 31: 42.790836Z 0 [Warning] TIMESTAMP with implicit value DEFAULT is deprecated. Please use the --explicit_defaults_for_timestamp option on your server (see the documentation for more details).
db_1 | 2018-09-23T22: 31: 42.791694Z 0 [Note] mysqld (mysqld 5.7.23) which starts as process 1 ...
wordpress_1 | [Sun Sep 23 22:31:43.499055 2018] [mpm_prefork:notice] [pid 1]    AH00163: configured in Apache / 2.4.25 (Debian) PHP / 7.2.10 - resuming normal operations
wordpress_1 | [Sun Sep 23 22:31:43.499796 2018] [core:notice] [pid 1]    AH00094: Command line: & # 39; apache2 -D FOREGROUND & # 39;

Compose will extract both images, start two containers and create the wp_data directory in the project directory.

Enter in your browser and you will see the WordPress installation screen.

From here you can start developing your WordPress theme or plugin.

To stop Compose, press CTRL + C.

You can also start Compose in independent mode by passing the -d flag.

To check the services in progress use the ps option:

                            Command State Ports name
-------------------------------------------------- --------------------------------
my_app_db_1 mysqld About 3306 / tcp, 33060 / tcp
my_app_wordpress_1 apach ... Up>80/tcp

When Compose is running independently to stop the services, use:

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

Passing the --volumes switch will also remove data volumes:

docker-compose down --volumes

Uninstalling Docker Compose

If for any reason you want to uninstall Docker Compose you can simply delete the binary by typing:

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


You learned how to install and use Docker Compose on Ubuntu 18.04. If you have any questions, please leave a comment below.

Source link

Leave a Reply

Your email address will not be published.