Podman : Docker Compose を利用する2024/11/06 |
Docker Compose をインストールすると、複数コンテナーの定義や実行が容易に可能となります。 |
|
[1] | |
[2] | Docker Compose をインストールします。 |
[root@dlp ~]# dnf -y install docker-compose
|
[3] | 例として、Nginx が稼働するコンテナーと MariaDB が稼働するコンテナーを同時に起動する場合の設定です。 |
FROM fedora 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: fedora MYSQL_PASSWORD: password MYSQL_DATABASE: fedora_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 # アプリケーションをビルド/実行 [root@dlp ~]# docker-compose up -d Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg. >>>> Executing external compose provider "/usr/libexec/docker/cli-plugins/docker-compose". Please see podman-compose(1) for how to disable this message. <<<< Sending build context to Docker daemon 3.472kB STEP 1/6: FROM fedora STEP 2/6: MAINTAINER ServerWorld <admin@srv.world> --> 09e660430cb7 STEP 3/6: RUN dnf -y install nginx ..... ..... ✓ Network root_default Created 0.0s ✓ Container root-db-1 Started 0.3s ✓ Container root-web-1 Started 0.3s[root@dlp ~]# podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 97ee105b152d docker.io/library/root-web:latest /usr/sbin/nginx -... 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, 80/tcp root-web-1 89fce851a278 docker.io/library/mariadb:latest mariadbd 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 3306/tcp root-db-1 # アクセス確認 [root@dlp ~]# mysql -h 127.0.0.1 -u root -p -e "show variables like 'hostname';" Enter password: +---------------+--------------+ | Variable_name | Value | +---------------+--------------+ | hostname | 89fce851a278 | +---------------+--------------+[root@dlp ~]# mysql -h 127.0.0.1 -u fedora -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | fedora_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 |
[4] | Docker Compose その他の基本的な操作です。 |
# アプリケーションを構成するコンテナーの状態確認 [root@dlp ~]# docker-compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS root-db-1 docker.io/library/mariadb:latest "mariadbd" db 6 minutes ago Up 6 minutes 3306/tcp root-web-1 docker.io/library/root-web:latest "/usr/sbin/nginx -g " web 6 minutes ago Up 6 minutes 80/tcp # アプリケーションを構成するコンテナーのログ確認 [root@dlp ~]# docker-compose logs db-1 | 2024-11-06 00:45:29+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.5.2+maria~ubu2404 started. db-1 | 2024-11-06 00:45:30+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' db-1 | 2024-11-06 00:45:30+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.5.2+maria~ubu2404 started. db-1 | 2024-11-06 00:45:30+00:00 [Note] [Entrypoint]: MariaDB upgrade not required db-1 | 2024-11-06 0:45:30 0 [Note] Starting MariaDB 11.5.2-MariaDB-ubu2404 source revision ea75a0b6009b0251e83193cd38c3974e36b65e06 server_uid TpZ76ucH4nV+AyKFiAG+J41iNaU= as process 1 ..... ..... db-1 | 2024-11-06 0:45:31 0 [Note] mariadbd: Event Scheduler: Loaded 0 events db-1 | 2024-11-06 0:45:31 0 [Note] mariadbd: ready for connections. db-1 | Version: '11.5.2-MariaDB-ubu2404' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution # アプリケーションを構成するコンテナーでコマンド実行 # コンテナー名は [docker-compose.yml] で指定した名称 [root@dlp ~]# docker-compose exec db /bin/bash Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg. root@89fce851a278:/# # アプリケーションを終了し、構成するコンテナーを全て停止 [root@dlp ~]# docker-compose stop [+] Stopping 1/2 ✓ Container root-web-1 Stopped 0.2s Container root-db-1 Stopping 0.2s [+] Stopping 2/2eth1 (unregistering): left allmulticast mode ✓ Container root-web-1 Stopped 0.2s ✓ Container root-db-1 Stopped # アプリケーションに定義したサービスを個別に起動 # 依存関係を設定している場合は依存コンテナーも同時に起動 [root@dlp ~]# docker-compose up -d web [+] Running 1/1 ✓ Container root-web-1 Started 0.1s[root@dlp ~]# docker-compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS root-web-1 docker.io/library/root-web:latest "/usr/sbin/nginx -g " web 10 minutes ago Up 30 seconds 80/tcp # アプリケーションに関連するコンテナーを全て削除 # 起動中のコンテナーは削除しない [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
|
Sponsored Link |