Ubuntu 26.04

Docker : Use Docker Compose2026/05/08

 

To Install Docker Compose, it's easy to configure and run multiple containers as a Docker application.

[1] Install Docker Compose.
root@dlp:~#
apt -y install docker-compose-v2
root@dlp:~#
ln -s /usr/libexec/docker/cli-plugins/docker-compose docker-compose

root@dlp:~#
docker-compose --version

Docker Compose version 2.40.3+ds1-0ubuntu1
[2] For example, Configure an application that has Web and DB services with Docker Compose.
# define Web service container

root@dlp:~#
vi Dockerfile
FROM ubuntu
LABEL Maintainer="ServerWorld <admin@srv.world>"

ENV ubuntu_FRONTEND=noninteractive

RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get -y install apache2

EXPOSE 80
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

# define application configuration

root@dlp:~#
vi docker-compose.yml
services:
  db:
    image: docker.io/library/mariadb
    volumes:
      - /var/lib/docker/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: ubuntu
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: ubuntu_db
    user: 0:0
    privileged: true
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/var/www/html
    privileged: true

# build and run

root@dlp:~#
docker-compose up -d

[+] Running 11/11
 ✓ db Pulled                                                                                                               7.5s
   ✓ 4f12367291a4 Pull complete                                                                                            0.6s
   ✓ d5c8079c2cfb Pull complete                                                                                            0.8s
   ✓ b40150c1c271 Pull complete                                                                                            1.9s
   ✓ 70e28d1970b2 Pull complete                                                                                            0.8s
   ✓ be8de6e97fbd Pull complete                                                                                            0.8s
   ✓ 9728cf7b6840 Pull complete                                                                                            4.4s
   ✓ 79b59e88f775 Pull complete                                                                                            2.0s
   ✓ 3a08fbfd5bee Pull complete                                                                                            0.8s
   ✓ 7cc06ba3b186 Download complete                                                                                        0.0s
   ✓ 8955ebb9cc1f Download complete                                                                                        0.1s
[+] Building 23.2s (10/10) FINISHED
 => [internal] load local bake definitions                                                                                 0.0s
 => => reading from stdin 445B                                                                                             0.0s
 => [internal] load build definition from Dockerfile                                                                       0.0s
 => => transferring dockerfile: 276B                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                           0.0s
 => [internal] load .dockerignore                                                                                          0.0s
 => => transferring context: 2B                                                                                            0.0s
 => CACHED [1/4] FROM docker.io/library/ubuntu:latest@sha256:f3d28607ddd78734bb7f71f117f3c6706c666b8b76cbff7c9ff6e5718d46  0.0s
 => => resolve docker.io/library/ubuntu:latest@sha256:f3d28607ddd78734bb7f71f117f3c6706c666b8b76cbff7c9ff6e5718d46ff64     0.0s
 => [2/4] RUN apt-get update                                                                                               5.0s
 => [3/4] RUN apt-get -y install tzdata                                                                                    2.9s
 => [4/4] RUN apt-get -y install apache2                                                                                  11.2s
 => exporting to image                                                                                                     3.8s
 => => exporting layers                                                                                                    2.9s
 => => exporting manifest sha256:ce7277ffef46dee06d20e68bd2fe4470f72f0e88ffabbb1bc9a68d4bb8cb9e7e                          0.0s
 => => exporting config sha256:1194f8c0f821463e992202c0b5ddaf331f7acb52eae1c93ca223b37a10aa2ea2                            0.0s
 => => exporting attestation manifest sha256:655cc7d1ab5a6fb445aebad15853ff32fbf9b7f7a25355b671a28146b31752c7              0.0s
 => => exporting manifest list sha256:094ea18e9ca05b8c4ed2176f49276e10234fc3fc169b6bac649fd49742f92272                     0.0s
 => => naming to docker.io/library/root-web:latest                                                                         0.0s
 => => unpacking to docker.io/library/root-web:latest                                                                      0.8s
 => resolving provenance for metadata file                                                                                 0.0s
[+] Running 4/4
 ✓ root-web              Built                                                                                             0.0s
 ✓ Network root_default  Created                                                                                           0.0s
 ✓ Container root-web-1  Started                                                                                           0.3s
 ✓ Container root-db-1   Started                                                     

root@dlp:~#
docker ps

CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                         NAMES
69aa06551219   mariadb    "docker-entrypoint.s…"   50 seconds ago   Up 50 seconds   0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp   root-db-1
df272a9c8a6c   root-web   "/usr/sbin/apachectl…"   50 seconds ago   Up 50 seconds   0.0.0.0:80->80/tcp, [::]:80->80/tcp           root-web-1

# verify accesses

root@dlp:~#
mariadb -h 127.0.0.1 -u root -p -e "show variables like 'hostname';"

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 69aa06551219 |
+---------------+--------------+

root@dlp:~#
mariadb -h 127.0.0.1 -u ubuntu -p -e "show databases;"

Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ubuntu_db          |
+--------------------+

root@dlp:~#
echo "Hello Docker Compose World" > /var/lib/docker/disk02/index.html

root@dlp:~#
curl 127.0.0.1

Hello Docker Compose World
[3] Other basic operations of Docker Compose are follows.
# verify state of containers

root@dlp:~#
docker-compose ps

NAME         IMAGE                       COMMAND                  SERVICE   CREATED         STATUS         PORTS
root-db-1    docker.io/library/mariadb   "docker-entrypoint.s…"   db        2 minutes ago   Up 2 minutes   0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp
root-web-1   root-web                    "/usr/sbin/apachectl…"   web       2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp, [::]:80->80/tcp

# show logs of containers

root@dlp:~#
docker-compose logs

web-1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
db-1   | 2026-05-08 00:37:38+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:12.2.2+maria~ubu2404 started.
db-1   | 2026-05-08 00:37:39+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-1   | 2026-05-08 00:37:39+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:12.2.2+maria~ubu2404 started.
db-1   | 2026-05-08 00:37:39+00:00 [Note] [Entrypoint]: Initializing database files
db-1   | 2026-05-08 00:37:40+00:00 [Note] [Entrypoint]: Database files initialized
db-1   | 2026-05-08 00:37:40+00:00 [Note] [Entrypoint]: Starting temporary server
db-1   | 2026-05-08 00:37:40+00:00 [Note] [Entrypoint]: Waiting for server startup
.....
.....

# run any commands inside a container
# container name is just the one set in [docker-compose.yml]

root@dlp:~#
docker-compose exec db /bin/bash

root@69aa06551219:/# exit

# stop application and also shutdown all containers

root@dlp:~#
docker-compose stop

[+] Stopping 2/2
 ✓ Container root-db-1   Stopped                0.3s
 ✓ Container root-web-1  Stopped               10.2s

# start a service alone in application
# if set dependency, other container starts

root@dlp:~#
docker-compose up -d web

[+] Running 1/1
 ✓ Container root-web-1  Started                0.2s

root@dlp:~#
docker-compose ps

NAME         IMAGE      COMMAND                  SERVICE   CREATED         STATUS         PORTS
root-web-1   root-web   "/usr/sbin/apachectl…"   web       6 minutes ago   Up 8 seconds   0.0.0.0:80->80/tcp, [::]:80->80/tcp

# remove all containers in application
# if a container is running, it won't be removed

root@dlp:~#
docker-compose rm

? Going to remove root-db-1 Yes
[+] Removing 1/0e root-db-1 (y/N) y
 ✓ Container root-db-1  Removed                0.0s
Matched Content