Docker : Docker Compose を利用する2024/06/13 |
Docker Compose をインストールすると、複数コンテナーの定義や実行が容易に可能となります。 |
|
[1] | 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] | 例として、Apache2 が稼働するコンテナーと MariaDB が稼働するコンテナーを同時に起動する場合の設定です。 |
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"] 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 # アプリケーションをビルド/実行 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.4sroot@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 # アクセス確認 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] | Docker Compose その他の基本的な操作です。 |
# アプリケーションを構成するコンテナーの状態確認 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 # アプリケーションを構成するコンテナーのログ確認 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 # アプリケーションを構成するコンテナーでコマンド実行 # コンテナー名は [docker-compose.yml] で指定した名称 root@dlp:~# /usr/local/bin/docker-compose exec db /bin/bash root@fba9420393f3:/# # アプリケーションを終了し、構成するコンテナーを全て停止 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 # アプリケーションに定義したサービスを個別に起動 # 依存関係を設定している場合は依存コンテナーも同時に起動 root@dlp:~# /usr/local/bin/docker-compose up -d web [+] Running 1/1 ✓ Container root-web-1 Started 0.2sroot@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 # アプリケーションに関連するコンテナーを全て削除 # 起動中のコンテナーは削除しない 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
|
Sponsored Link |