Docker : Use Docker Compose2025/08/08 |
|
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.37.3/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.37.3 |
| [2] | For example, Configure an application that has Web and DB services with Docker Compose. |
FROM quay.io/centos/centos:stream10 LABEL maintainer="ServerWorld <admin@srv.world>" RUN dnf -y install nginx EXPOSE 80 CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
services:
db:
image: docker.io/library/mariadb
volumes:
- /var/lib/containers/disk01:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: cent
MYSQL_PASSWORD: password
MYSQL_DATABASE: cent_db
user: 0:0
privileged: true
ports:
- "3306:3306"
web:
build: .
ports:
- "80:80"
volumes:
- /var/lib/containers/disk02:/usr/share/nginx/html
privileged: true
# build and run [root@dlp ~]# docker-compose up -d ..... ..... Container root-web-1 Starting Container root-db-1 Starting [+] Running 4/4br-2b65fd497827: port 1(veth4676f8d) entered blocking state ✓ web Built ✓ Network root_default Created ✓ Container root-web-1 Started ✓ Container root-db-1 Started[root@dlp ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9af087938a78 mariadb "docker-entrypoint.s..." About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp root-db-1 0c7ca7b2dd0f root-web "/usr/sbin/nginx -g ..." About a minute ago Up About a minute 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 | 9af087938a78 | +---------------+--------------+[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/containers/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 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp root-web-1 root-web "/usr/sbin/nginx -g ..." web 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp # show logs of containers [root@dlp ~]# docker-compose logs ..... ..... db-1 | 2025-08-08 5:56:53 0 [Note] InnoDB: Buffer pool(s) load completed at 250808 5:56:53 db-1 | 2025-08-08 5:56:56 0 [Note] Server socket created on IP: '0.0.0.0'. db-1 | 2025-08-08 5:56:56 0 [Note] Server socket created on IP: '::'. db-1 | 2025-08-08 5:56:56 0 [Note] mariadbd: Event Scheduler: Loaded 0 events db-1 | 2025-08-08 5:56:56 0 [Note] mariadbd: ready for connections. db-1 | Version: '11.8.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 ~]# docker-compose exec db /bin/bash
root@9af087938a78:/#
root@9af087938a78:/# exit
exit
# stop application and also shutdown all containers [root@dlp ~]# docker-compose stop [+] Stopping 0/2 Container root-db-1 Stopping Container root-web-1 Stopping [+] Stopping 1/2eth4676f8d (unregistering): left allmulticast mode Container root-db-1 Stopping [+] Stopping 1/2t-web-1 Stopped Container root-db-1 Stopping ✓ Container root-web-1 Stopped [+] Stopping 2/2ethc7c2012 (unregistering): left allmulticast mode ✓ Container root-db-1 Stopped ✓ Container root-web-1 Stopped # 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 [+] Running 1/1br-2b65fd497827: port 1(veth900258e) entered blocking state ✓ Container root-web-1 Started[root@dlp ~]# docker-compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS root-web-1 root-web "/usr/sbin/nginx -g ..." web 38 seconds ago Up 37 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/1 ✓ Container root-db-1 Removed |
| Sponsored Link |
|
|