This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

AKS入門講座 DockerComposeハンズオン

Last updated at Posted at 2020-05-22

Docker Composeのインストール

1.jqのインストール

コピー&ペースト用

コマンド
apt -y install jq

コマンド結果

コマンド
# apt -y install jq
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  grub-pc-bin linux-headers-4.15.0-99
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libjq1 libonig4
The following NEW packages will be installed:
  jq libjq1 libonig4
0 upgraded, 3 newly installed, 0 to remove and 22 not upgraded.
Need to get 276 kB of archives.
After this operation, 930 kB of additional disk space will be used.
Get:1 http://azure.archive.ubuntu.com/ubuntu bionic/universe amd64 libonig4 amd64 6.7.0-1 [119 kB]
Get:2 http://azure.archive.ubuntu.com/ubuntu bionic/universe amd64 libjq1 amd64 1.5+dfsg-2 [111 kB]
Get:3 http://azure.archive.ubuntu.com/ubuntu bionic/universe amd64 jq amd64 1.5+dfsg-2 [45.6 kB]
Fetched 276 kB in 1s (315 kB/s)
Selecting previously unselected package libonig4:amd64.
(Reading database ... 76808 files and directories currently installed.)
Preparing to unpack .../libonig4_6.7.0-1_amd64.deb ...
Unpacking libonig4:amd64 (6.7.0-1) ...
Selecting previously unselected package libjq1:amd64.
Preparing to unpack .../libjq1_1.5+dfsg-2_amd64.deb ...
Unpacking libjq1:amd64 (1.5+dfsg-2) ...
Selecting previously unselected package jq.
Preparing to unpack .../jq_1.5+dfsg-2_amd64.deb ...
Unpacking jq (1.5+dfsg-2) ...
Setting up libonig4:amd64 (6.7.0-1) ...
Setting up libjq1:amd64 (1.5+dfsg-2) ...
Setting up jq (1.5+dfsg-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

2.Docker Composeインストールスクリプトの作成

コピー&ペースト用

コマンド
vim docker-compose-install.sh
コマンド
#!/bin/bash
compose_version=$(curl https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)
output='/usr/local/bin/docker-compose'
curl -L https://github.com/docker/compose/releases/download/$compose_version/docker-compose-$(uname -s)-$(uname -m) -o $output
chmod +x $output
echo $(docker-compose --version)

作業全体

コマンド
# vim docker-compose-install.sh
#!/bin/bash
compose_version=$(curl https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)
output='/usr/local/bin/docker-compose'
curl -L https://github.com/docker/compose/releases/download/$compose_version/docker-compose-$(uname -s)-$(uname -m) -o $output
chmod +x $output
echo $(docker-compose --version)

3.権限付与

コピー&ペースト用

コマンド
chmod +x docker-compose-install.sh

コマンド結果

コマンド結果は表示されません。

コマンド
# chmod +x docker-compose-install.sh

4.スクリプト実行

コピー&ペースト用

コマンド
sh docker-compose-install.sh

コマンド結果

コマンド
# sh docker-compose-install.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18174  100 18174    0     0  55920      0 --:--:-- --:--:-- --:--:-- 56092
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   651  100   651    0     0   2206      0 --:--:-- --:--:-- --:--:--  2199
100 11.6M  100 11.6M    0     0  4181k      0  0:00:02  0:00:02 --:--:-- 7409k
docker-compose version 1.27.4, build 40524192

docker-compose.ymlの作成

1.docker-compose.ymlの作成

コピー&ペースト用

コマンド
vim docker-compose.yml
コマンド
version: '3.7'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     env_file: .env

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
     env_file: .env

volumes:
    db_data:
     driver: local

作業全体

コマンド
# vim docker-compose.yml
version: '3.7'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     env_file: .env

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
     env_file: .env

volumes:
    db_data:
     driver: local

.envファイルの作成

1..envファイルの作成

コピー&ペースト用

コマンド
vim .env
コマンド
MYSQL_ROOT_PASSWORD=somewordpress
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=wordpress

作業全体

コマンド
# vim .env
MYSQL_ROOT_PASSWORD=somewordpress
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=wordpress

WordPressコンテナーの一括起動

1.コンテナー一括起動

コピー&ペースト用

コマンド
docker-compose up -d

コマンド結果

コマンド
# docker-compose up -d
Creating network "build_wordpress_default" with the default driver
Creating volume "build_wordpress_db_data" with local driver
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
afb6ec6fdc1c: Pull complete
0bdc5971ba40: Pull complete
97ae94a2c729: Pull complete
f777521d340e: Pull complete
1393ff7fc871: Pull complete
a499b89994d9: Pull complete
7ebe8eefbafe: Pull complete
4eec965ae405: Pull complete
a531a782d709: Pull complete
10e94c02b508: Pull complete
799a94b968ef: Pull complete
Digest: sha256:5c9fd7949bc0f076429fa2c40d0e7406e095bdb5216a923257b31972a6f3ae4f
Status: Downloaded newer image for mysql:5.7
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
afb6ec6fdc1c: Already exists
3d895574014b: Pull complete
c309fdad6410: Pull complete
c201f6a5d6f9: Pull complete
e87f853892aa: Pull complete
998b2113b400: Pull complete
b3c0b4710d3b: Pull complete
c79fb2b38801: Pull complete
30aa6f0dd423: Pull complete
8af9a337c36d: Pull complete
64ec85e06910: Pull complete
606f88b4f608: Pull complete
845e768a44c5: Pull complete
232824f4bf64: Pull complete
c14e31cd46f2: Pull complete
df59d99840f5: Pull complete
0f3a9380af13: Pull complete
348bafbbcb22: Pull complete
d96f27aa3b63: Pull complete
58f35ab6fddc: Pull complete
0c1a0d01788e: Pull complete
Digest: sha256:1c4b01a38f37fee0e3aee5266bef13cbb3bb2ff705803377a35687fc441f6da8
Status: Downloaded newer image for wordpress:latest
Creating build_wordpress_db_1 ... done
Creating build_wordpress_wordpress_1 ... done

2.ブラウザアクセス

「http://グローバルIPアドレス:8080/」

Volumeについて

1.volume名の確認

コピー&ペースト用

コマンド
docker volume ls

コマンド結果

コマンド
# docker volume ls
DRIVER              VOLUME NAME
local               1b13aec9ad265d4813a5fd2adc2091b14db5c4cd7f5b6761aa8935f54efa69d1
local               build_wordpress_db_data

2.Linuxホストの以下ディレクトリでtestという空ファイルを作成

コピー&ペースト用

コマンド
touch /var/lib/docker/volumes/build_wordpress_db_data/_data/test

コマンド結果

コマンド結果は表示されません。

コマンド
# touch /var/lib/docker/volumes/build_wordpress_db_data/_data/test

コピー&ペースト用

コマンド
ls /var/lib/docker/volumes/build_wordpress_db_data/_data/

コマンド結果

コマンド
# ls /var/lib/docker/volumes/build_wordpress_db_data/_data/
auto.cnf  ca-key.pem  ca.pem  client-cert.pem  client-key.pem  ib_buffer_pool  ib_logfile0  ib_logfile1  ibdata1  ibtmp1  mysql  performance_schema  private_key.pem  public_key.pem  server-cert.pem  server-key.pem  sys  test  wordpress

3. MySQLのコンテナー名を確認

コピー&ペースト用

コマンド
docker container ls

コマンド結果

コマンド
# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
f3e2e47c8949        wordpress:latest    "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp   build_wordpress_wordpress_1
f5f271ee395a        mysql:5.7           "docker-entrypoint.s…"   3 minutes ago       Up 2 minutes        3306/tcp, 33060/tcp    build_wordpress_db_1

4.MySQLコンテナーの中からも確認

コピー&ペースト用

コマンド
docker container exec -it build_wordpress_db_1 /bin/bash

コマンド結果

コマンド
# docker container exec -it build_wordpress_db_1 /bin/bash
root@f5f271ee395a:/# ls /var/lib/mysql
auto.cnf         client-key.pem  ibdata1             private_key.pem  sys
ca-key.pem       ib_buffer_pool  ibtmp1              public_key.pem   test
ca.pem           ib_logfile0     mysql               server-cert.pem  wordpress
client-cert.pem  ib_logfile1     performance_schema  server-key.pem

5.コンテナーでもtest2という空ファイルを作成して、Linuxホストから確認

コピー&ペースト用

コマンド
touch /var/lib/mysql/test2

コマンド結果

コマンド結果は表示されません

コマンド
root@f5f271ee395a:/# touch /var/lib/mysql/test2

コピー&ペースト用

コマンド
ls /var/lib/mysql

コマンド結果

コマンド
root@f5f271ee395a:/# ls /var/lib/mysql
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  mysql               private_key.pem  server-cert.pem  sys   test2
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   performance_schema  public_key.pem   server-key.pem   test  wordpress

コピー&ペースト用

コマンド
exit

コマンド結果

コマンド
root@f5f271ee395a:/# exit
exit

6.Linuxホストで確認

コピー&ペースト用

コマンド
ls /var/lib/docker/volumes/build_wordpress_db_data/_data/

コマンド結果

コマンド
# ls /var/lib/docker/volumes/build_wordpress_db_data/_data/
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  mysql               private_key.pem  server-cert.pem  sys   test2
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   performance_schema  public_key.pem   server-key.pem   test  wordpress

7.rootからログアウト

コピー&ペースト用

コマンド
exit

コマンド結果

コマンド
# exit
logout

8.Linuxホストからログアウト

コピー&ペースト用

コマンド
exit

コマンド結果

コマンド
# exit
logout
Connection to 40.115.143.98 closed.

9.仮想マシンの削除

コピー&ペースト用

コマンド
az vm delete --resource-group training --name docker --yes

コマンド結果

コマンド結果は表示されません。

コマンド
# az vm delete --resource-group training --name docker --yes
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up