How to Install Docker Compose on Ubuntu 20.04

[ad_1]

Docker Compose
is a command-line tool that allows you 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 each other, allowing you to run multiple copies of the same environment on a single host.

Compose is typically used for local development, single host application deployments, 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.

Prerequisites #

We’re assuming that you have Docker installed
on your Ubuntu machine.

Installing Docker Compose on Ubuntu #

Docker Compose is a single binary file. The installation is straightforward. We’ll download the file to a 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 writing this article, the latest stable version of Docker Compose is 1.25.5. Before downloading the Compose binary, visit the Compose repository release page on GitHub
and check if there is a new version 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

Once the download is complete, apply executable permissions
to the file:

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

To verify that the installation was successful, run the following command which will print the Compose version:

docker-compose --version

The output will look something like this:

docker-compose version 1.25.5, build b02f1306

That’s it! Docker Compose has been installed on your Ubuntu machine, and you can start using it.

Getting Started with Docker Compose #

In this section, we’ll 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 inside 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:

The first line of the file specifies the version of the Compose file
. There are several different versions of the Compose file format with support for specific Docker releases.

Next, you define services, volumes and networks.

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

Services can use images that are available on DockerHub or images built from the Dockerfile. The service section also includes keys specifying exposed ports, volumes, environment variables, dependencies, and other Docker commands

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

docker-compose up

Compose will pull the images, start the containers, and create the wp_data directory.

Enter http://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 the Compose in a detached mode by passing the -d option:

docker-compose up -d

To check the running services use the ps option:

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

When Compose is running in detached mode to stop the services, run:

docker-compose stop

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

docker-compose down

Uninstalling Docker Compose #

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

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

Conclusion #

We’ve shown 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.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.