How to Install and use Docker Compose on Ubuntu 23.04

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services. Then, using a single command, you create and start all the services from your configuration.

Installing Docker Compose on Ubuntu

Use curl to download the Compose file into the /usr/local/bin directory.

curl -L "https://github.com/docker/compose/releases/download/v2.17.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Output:

root@ubuntu:~# curl -L "https://github.com/docker/compose/releases/download/v2.17.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 51.9M  100 51.9M    0     0  26.6M      0  0:00:01  0:00:01 --:--:-- 39.5M

Next, set the correct permissions so that the docker-compose command is executable.

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

To verify that the installation was successful, you can run following command.

docker-compose --version

Output:

root@ubuntu:~# docker-compose --version
Docker Compose version v2.17.3

(Optionally) Installing Docker on Ubuntu

Check for system updates and install it.

apt update

apt upgrade

Install basic dependencies.

apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Output:

root@ubuntu:~# apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20230311).
ca-certificates set to manually installed.
curl is already the newest version (7.88.1-8ubuntu1).
curl set to manually installed.
software-properties-common is already the newest version (0.99.35).
software-properties-common set to manually installed.
The following NEW packages will be installed:
  apt-transport-https gnupg-agent
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 8,348 B of archives.
After this operation, 58.4 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu lunar/universe amd64 apt-transport-https all 2.6.0 [3,966 B]
Get:2 http://us.archive.ubuntu.com/ubuntu lunar/universe amd64 gnupg-agent all 2.2.40-1.1ubuntu1 [4,382 B]

Import docker repository GPG key.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Output:

root@ubuntu:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

Add Docker CE repository.

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Output:


root@ubuntu:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Repository: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu lunar stable'
Description:
Archive for codename: lunar components: stable
More info: https://download.docker.com/linux/ubuntu
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-lunar.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-lunar.list
Get:1 https://download.docker.com/linux/ubuntu lunar InRelease [43.3 kB]
Hit:2 http://us.archive.ubuntu.com/ubuntu lunar InRelease
Hit:3 http://us.archive.ubuntu.com/ubuntu lunar-updates InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu lunar-backports InRelease
Hit:5 http://us.archive.ubuntu.com/ubuntu lunar-security InRelease
Get:6 https://download.docker.com/linux/ubuntu lunar/stable amd64 Packages [3,617 B]
Fetched 47.0 kB in 2s (26.4 kB/s)
Reading package lists... Done
W: https://download.docker.com/linux/ubuntu/dists/lunar/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

Installing Docker CE.

apt install docker-ce

Output:

root@ubuntu:~# apt install docker-ce
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  containerd.io docker-buildx-plugin docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libltdl7 libslirp0 pigz
  slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libltdl7
  libslirp0 pigz slirp4netns
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 109 MB of archives.
After this operation, 396 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 https://download.docker.com/linux/ubuntu lunar/stable amd64 containerd.io amd64 1.6.20-1 [28.3 MB]
Get:2 http://us.archive.ubuntu.com/ubuntu lunar/universe amd64 pigz amd64 2.6-1 [63.6 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu lunar/main amd64 libltdl7 amd64 2.4.7-5 [40.3 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu lunar/main amd64 libslirp0 amd64 4.7.0-1 [62.7 kB]

To check status.

systemctl status docker

Output:

root@ubuntu:~# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Mon 2023-04-24 19:04:34 UTC; 1min 21s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 3805 (dockerd)
      Tasks: 10
     Memory: 26.7M
        CPU: 1.028s
     CGroup: /system.slice/docker.service
             └─3805 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

 root@ubuntu:~# 

(Optionally) Editing Up a docker-compose.yml File

In this section, we’ll use Docker Compose to construct a multi-container WordPress utility.

creating a new directory in your home folder, and then moving into it.

mkdir my_app

cd my_app

Next, create the docker-compose.yml file.

nano docker-compose.yml

Paste the following content on your docker-compose.yml file.

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:

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

Start up the WordPress application by running the following command.

docker-compose up

Output:

root@vps:~# docker-compose up
Creating network "root_default" with the default driver
Creating volume "root_db_data" with default driver
Creating volume "root_wp_data" with default driver
Pulling db (mysql:5.7.42-1.el7 )...
5.7.42-1.el7 : Pulling from library/mysql
f7ec5a41d630: Pull complete
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete

Navigate to your browser. http://yourserver-ip-address:8080 and you will see the Wordpress installation screen.

images

Start the Compose in a detached mode by following command.

docker-compose up -d

Output:

root@ubuntu:~/my_app# docker-compose up -d
[+] Running 2/2
Container my_app-db-1         Started                                                                                1.3s
Container my_app-wordpress-1  Started                                                                                2.5s

To check the running services.

docker-compose ps

Output:

root@ubuntu:~/my_app# docker-compose ps
NAME                 IMAGE         COMMAND                 PORTS             SERVICE           STATUS    

my_app-db-1        mysql:5.7    "docker-entrypoint.s…"   3306/tcp, 33060/tcp   db        Up About a minute   

my_app-wordpress-1  wordpress   "docker-entrypoint.s…"  0.0.0.0:8080->80/tcp, :::8080->80/tcp  wordpress Up About aminute   

To stop the services only.

docker-compose stop

To stop and remove containers and networks.

docker-compose down