Ubuntu 23.04
Sponsored Link

Docker : Use Docker Compose2023/04/27

 
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
[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 DEBIAN_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 configration

root@dlp:~#
vi docker-compose.yml
version: '3'
services:
  db:
    image: 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:/var/www/html

# buid and run

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

Building web
Sending build context to Docker daemon  27.65kB
Step 1/8 : FROM ubuntu
 ---> 08d22c0ceb15
Step 2/8 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Using cache
 ---> 943e43d6e7a4

.....
.....

Creating root_db_1  ... done
Creating root_web_1 ... done

root@dlp:~#
docker ps

CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
95cb820e269d   root_web   "/usr/sbin/apachectl…"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp           root_web_1
c6aeab57409d   mariadb    "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   root_db_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      | c6aeab57409d |
+---------------+--------------+

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:~#
docker-compose ps

   Name                Command              State              Ports
--------------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh           Up      0.0.0.0:3306->3306/tcp,:::3306->3306/tcp
root_web_1   /usr/sbin/apachectl -D FOR     Up      0.0.0.0:80->80/tcp,:::80->80/tcp

# show logs of containers

root@dlp:~#
docker-compose logs

Attaching to root_web_1, root_db_1
db_1   | 2023-04-27 01:35:42+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.2+maria~ubu2204 started.
db_1   | 2023-04-27 01:35:42+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2023-04-27 01:35:42+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.2+maria~ubu2204 started.
db_1   | 2023-04-27 01:35:42+00:00 [Note] [Entrypoint]: Initializing database files

.....
.....

db_1   | 2023-04-27  1:35:47 0 [Note] mariadbd: ready for connections.
db_1   | Version: '10.11.2-MariaDB-1:10.11.2+maria~ubu2204'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
web_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message

# 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@c6aeab57409d:/#

# stop application and also shutdown all containers

root@dlp:~#
docker-compose stop

Stopping root_db_1  ... done
Stopping root_web_1 ... done

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

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

Starting root_web_1 ... done
root@dlp:~#
docker-compose ps

   Name                Command              State               Ports
--------------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh           Exit 0
root_web_1   /usr/sbin/apachectl -D FOR     Up       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
Are you sure? [yN] y
Removing root_db_1 ... done
Matched Content