Docker : Use Docker Compose2022/07/29 |
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.7.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
[root@dlp ~]#
[root@dlp ~]# chmod 755 /usr/local/bin/docker-compose
docker-compose --version Docker Compose version v2.7.0 |
[2] | For example, Configure an application that has Web and DB services with Docker Compose. |
FROM quay.io/centos/centos:stream9 MAINTAINER ServerWorld <admin@srv.world> RUN dnf -y install nginx EXPOSE 80 CMD ["/usr/sbin/nginx", "-g", "daemon off;"] version: '3' services: db: image: mariadb volumes: - /var/lib/docker/disk01:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_USER: cent MYSQL_PASSWORD: password MYSQL_DATABASE: cent_db ports: - "3306:3306" web: build: . ports: - "80:80" volumes: - /var/lib/docker/disk02:/usr/share/nginx/html # build and run [root@dlp ~]# docker-compose up -d [+] Running 12/12 [+] Building 0.1s (4/5) => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 257B 0.0s ..... ..... [+] Running 3/3eth0: renamed from vethd22d159 Network root_default Created 0.2sCHANGE): veth34f9937: link becomes ready Container root-db-1 Started 0.5ss(veth34f9937) entered blocking state Container root-web-1 Started 0.5ss(veth34f9937) entered forwarding state[root@dlp ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e9bdc02dd9a mariadb "docker-entrypoint.s…" 18 seconds ago Up 16 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp root-db-1 29dcae4b111e root_web "/usr/sbin/nginx -g …" 18 seconds ago Up 17 seconds 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 | 4e9bdc02dd9a | +---------------+--------------+[root@dlp ~]# mysql -h 127.0.0.1 -u cent -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | cent_db | | information_schema | +--------------------+[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 SERVICE STATUS PORTS root-db-1 "docker-entrypoint.s…" db running 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp root-web-1 "/usr/sbin/nginx -g …" web running 0.0.0.0:80->80/tcp, :::80->80/tcp # show logs of containers [root@dlp ~]# docker-compose logs root-db-1 | 2022-07-29 04:57:23+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started. root-db-1 | 2022-07-29 04:57:23+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' root-db-1 | 2022-07-29 04:57:23+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started. ..... ..... root-db-1 | 2022-07-29 4:57:29 0 [Note] Server socket created on IP: '::'. root-db-1 | 2022-07-29 4:57:29 0 [Note] mariadbd: ready for connections. root-db-1 | Version: '10.8.3-MariaDB-1:10.8.3+maria~jammy' 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 ~]# docker-compose exec db /bin/bash root@4e9bdc02dd9a:/# # stop application and also shutdown all containers [root@dlp ~]# docker-compose stop [+] Running 0/0 Container root-web-1 Stopping 0.1s Container root-db-1 Stopping 0.1s [+] Running 1/2br-f172cf624e51: port 2(vethd5bd225) entered disabled state Container root-web-1 Stopped 0.2ss promiscuous mode Container root-db-1 Stopping 0.3s(vethd5bd225) entered disabled state [+] Running 2/2br-f172cf624e51: port 1(veth34f9937) entered disabled state Container root-web-1 Stopped 0.2st promiscuous mode Container root-db-1 Stopped 0.4ss(veth34f9937) entered disabled state # start a service alone in application # if set dependency, other container starts [root@dlp ~]# docker-compose up -d web [+] Running 0/1 Container root-web-1 Starting 0.3s [+] Running 1/1br-f172cf624e51: port 1(veth2985af3) entered blocking state Container root-web-1 Started 0.4ss(veth2985af3) entered forwarding state[root@dlp ~]# docker-compose ps NAME COMMAND SERVICE STATUS PORTS root-db-1 "docker-entrypoint.s…" db exited (0) root-web-1 "/usr/sbin/nginx -g …" web running 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
[+] Running 1/0ve root-db-1 (y/N) y
Container root-db-1 Removed 0.0s
|
Sponsored Link |
|