Ubuntu 24.04
Sponsored Link

Docker : Use Docker Compose2024/06/13

 

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

[1] Install Docker Compose.
root@dlp:~#
curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

root@dlp:~#
chmod 755 /usr/local/bin/docker-compose

root@dlp:~#
/usr/local/bin/docker-compose --version

Docker Compose version v2.27.0
[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
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
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/usr/share/nginx/html

# build and run

root@dlp:~#
/usr/local/bin/docker-compose up -d

[+] Running 9/9
 ✓ db Pulled                                                               7.8s
   ✓ 00d679a470c4 Already exists                                           0.0s
   ✓ 381fb89d9026 Pull complete                                            1.5s
   ✓ fd042176e576 Pull complete                                            1.8s
   ✓ 80b21b96f832 Pull complete                                            1.8s
   ✓ 310e39ef046d Pull complete                                            2.1s
   ✓ 8e4136144025 Pull complete                                            4.7s
   ✓ c187e9145a80 Pull complete                                            4.7s
   ✓ 7b8cfc1ced45 Pull complete                                            4.7s
[+] Building 22.0s (8/8) FINISHED                                docker:default
 => [web internal] load .dockerignore                                      0.1s
 => => transferring context: 2B                                            0.0s
 => [web internal] load build definition from Dockerfile                   0.1s
 => => transferring dockerfile: 268B                                       0.0s
 => [web internal] load metadata for docker.io/library/ubuntu:latest       0.0s
 => CACHED [web 1/4] FROM docker.io/library/ubuntu                         0.0s
 => [web 2/4] RUN apt-get update                                           5.1s
 => [web 3/4] RUN apt-get -y install tzdata                                2.7s
 => [web 4/4] RUN apt-get -y install apache2                              13.7s
 => [web] exporting to image                                               0.4s
 => => exporting layers                                                    0.4s
 => => writing image sha256:04dd1f9159cf877a6e105ca74c48d4a5dcd3f9ad1cfb8  0.0s
 => => naming to docker.io/library/root-web                                0.0s
[+] Running 3/3
 ✓ Network root_default  Created                                           0.1s
 ✓ Container root-web-1  Started                                           0.3s
 ✓ Container root-db-1   Started                                           0.4s

root@dlp:~#
docker ps

CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
fba9420393f3   mariadb    "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   root-db-1
adb1800e2691   root-web   "/usr/sbin/apachectl…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp           root-web-1

# verify accesses

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

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 51bcfccf5bbd |
+---------------+--------------+

root@dlp:~#
mysql -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:~#
/usr/local/bin/docker-compose ps

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

# show logs of containers

root@dlp:~#
/usr/local/bin/docker-compose logs

db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.4.2+maria~ubu2404 started.
db-1   | 2024-06-13 01:23:15+00:00 [Warn] [Entrypoint]: /sys/fs/cgroup///memory.pressure not writable, functionality unavailable to MariaDB
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.4.2+maria~ubu2404 started.
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: MariaDB upgrade not required

.....
.....

db-1   | 2024-06-13  1:23:17 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
db-1   | 2024-06-13  1:23:17 0 [Note] mariadbd: ready for connections.
db-1   | Version: '11.4.2-MariaDB-ubu2404'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

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

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

root@fba9420393f3:/#

# stop application and also shutdown all containers

root@dlp:~#
/usr/local/bin/docker-compose stop

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

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

root@dlp:~#
/usr/local/bin/docker-compose up -d web

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

root@dlp:~#
/usr/local/bin/docker-compose ps

NAME         IMAGE      COMMAND                  SERVICE   CREATED         STATUS          PORTS
root-web-1   root-web   "/usr/sbin/apachectl…"   web       5 minutes ago   Up 25 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:~#
/usr/local/bin/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