How to Install and use Docker Compose on Debian 12

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.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Output:

root@server:~# curl -L "https://github.com/docker/compose/releases/download/v2.18.1/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  31.1M      0  0:00:01  0:00:01 --:--:-- 62.8M

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@server:~# docker-compose --version
Docker Compose version v2.18.1

(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@server:~# 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).
curl is already the newest version (7.88.1-10).
The following additional packages will be installed:
  appstream dirmngr gir1.2-glib-2.0 gir1.2-packagekitglib-1.0 gnupg gnupg-l10n
  gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server gpgconf gpgsm
  libappstream4 libassuan0 libduktape207 libdw1 libgirepository-1.0-1
  libglib2.0-bin libgstreamer1.0-0 libksba8 libnpth0 libpackagekit-glib2-18
  libpolkit-agent-1-0 libpolkit-gobject-1-0 libstemmer0d libunwind8 libxmlb2
  libyaml-0-2 packagekit packagekit-tools pinentry-curses polkitd
  python3-blinker python3-cffi-backend python3-cryptography python3-dbus
  python3-distro python3-gi python3-jwt python3-lazr.restfulclient
  python3-lazr.uri python3-oauthlib python3-software-properties
  python3-wadllib sgml-base xml-core
Suggested packages:
  apt-config-icons pinentry-gnome3 tor parcimonie xloadimage scdaemon
  gstreamer1.0-tools pinentry-doc polkitd-pkla python-blinker-doc
  python-cryptography-doc python3-cryptography-vectors python-dbus-doc
  python3-crypto sgml-base-doc debhelper
The following NEW packages will be installed:
  appstream apt-transport-https dirmngr gir1.2-glib-2.0
  gir1.2-packagekitglib-1.0 gnupg gnupg-agent gnupg-l10n gnupg-utils gpg
  gpg-agent gpg-wks-client gpg-wks-server gpgconf gpgsm libappstream4
  libassuan0 libduktape207 libdw1 libgirepository-1.0-1 libglib2.0-bin
  libgstreamer1.0-0 libksba8 libnpth0 libpackagekit-glib2-18
  libpolkit-agent-1-0 libpolkit-gobject-1-0 libstemmer0d libunwind8 libxmlb2
  libyaml-0-2 packagekit packagekit-tools pinentry-curses polkitd
  python3-blinker python3-cffi-backend python3-cryptography python3-dbus
  python3-distro python3-gi python3-jwt python3-lazr.restfulclient
  python3-lazr.uri python3-oauthlib python3-software-properties
  python3-wadllib sgml-base software-properties-common xml-core
0 upgraded, 50 newly installed, 0 to remove and 0 not upgraded.
Need to get 13.6 MB of archives.
After this operation, 39.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian bookworm/main amd64 sgml-base all 1.31 [15.4kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libstemmer0d amd64 2.2.0-2 [118 kB]

Import docker repository GPG key.

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

Output:

root@server:~# curl -fsSL https://download.docker.com/linux/debian/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@server:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
Repository: 'deb [arch=amd64] https://download.docker.com/linux/debian bookworm stable'
Description:
Archive for codename: bookworm components: stable
More info: https://download.docker.com/linux/debian
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_debian-bookworm.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_debian-bookworm.list
Hit:1 http://deb.debian.org/debian bookworm InRelease
Hit:2 http://deb.debian.org/debian bookworm-updates InRelease
Hit:3 http://security.debian.org/debian-security bookworm-security InRelease
Get:4 https://download.docker.com/linux/debian bookworm InRelease [43.3 kB]
Ign:5 https://download.docker.com/linux/ubuntu bookworm InRelease
Err:6 https://download.docker.com/linux/ubuntu bookworm Release
  404  Not Found [IP: 54.230.21.60 443]
Get:7 https://download.docker.com/linux/debian bookworm/stable amd64 Packages [9,661 B]
Reading package lists... Done
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://download.docker.com/linux/debian/dists/bookworm/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@server:~# 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 git git-man iptables liberror-perl libip6tc2 libltdl7
  libnetfilter-conntrack3 libnfnetlink0 libslirp0 patch pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
  firewalld ed diffutils-doc
The following NEW packages will be installed:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin git git-man iptables liberror-perl libip6tc2
  libltdl7 libnetfilter-conntrack3 libnfnetlink0 libslirp0 patch pigz slirp4netns
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 121 MB of archives.
After this operation, 452 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian bookworm/main amd64 pigz amd64 2.6-1 [64.0 kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libip6tc2 amd64 1.8.9-2 [19.4 kB]
Get:3 http://deb.debian.org/debian bookworm/main amd64 libnfnetlink0 amd64 1.0.2-2 [15.1 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 libnetfilter-conntrack3 amd64 1.0.9-3 [40.7 kB]

To check status.

systemctl status docker

Output:

root@server:~# 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-06-19 14:26:09 EDT; 19s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 4183 (dockerd)
      Tasks: 10
     Memory: 30.3M
        CPU: 1.101s
     CGroup: /system.slice/docker.service
             └─4183 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 19 14:26:08 server systemd[1]: Starting docker.service - Docker Application Container Engine...

(Optionally) Setting 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@server:~/my_app# docker-compose up
  [+] Running 34/34
 ✔ wordpress 21 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                  35.9s
 ✔ 759700526b78 Pull complete                                                                                                                        13.4s
 ✔ 014a8e7424b0 Pull complete                                                                                                                        13.4s
 ✔ dbf764694663 Pull complete                                                                                                                        24.5s
 ✔ f50c4f9b9271 Pull complete                                                                                                                        24.5s
 ✔ aded95647031 Pull complete                                                                                                                        26.4s
 ✔ 4ab82896a769 Pull complete                                                                                                                        26.5s
 ✔ 86ad5aec4044 Pull complete                                                                                                                        26.5s
 ✔ d924242ab77a Pull complete                                                                                                                        26.8s
 ✔ 8de92f91d55d Pull complete                                                                                                                        26.8s
 ✔ c56c7ab9fd5e 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@server:~/my_app# docker-compose up -d
[+] Running 2/2
✔ Container my_app-db-1         Started                                                                                                                0.6s
✔ Container my_app-wordpress-1  Started                                                                                                                1.2s
root@server:~/my_app#

To check the running services.

docker-compose ps

Output:

root@server:~/my_app# docker-compose ps
NAME                 IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
my_app-db-1          mysql:5.7           "docker-entrypoint.s…"   db                  3 minutes ago       Up 42 seconds       3306/tcp, 33060/tcp
my_app-wordpress-1   wordpress           "docker-entrypoint.s…"   wordpress           3 minutes ago       Up 42 seconds       0.0.0.0:8080->80/tcp, :::8080->80/tcp

To stop the services only.

docker-compose stop

To stop and remove containers and networks.

docker-compose down