2022/10/05 Updated by

Docker

Compose: 複数のコンテナをまとめて起動する


[Up] Japanese English

2つのコンテナが通信する WordPress の例

  1. Docker ネットワークを作成する。
  2. ubuntu@ip-172-30-2-68:~$ docker network create wordpressnet
    efff9f7cc3a08cbdd871e1f10d6a0f25515ba1d39c79077e8f7900fb0029e596
    ubuntu@ip-172-30-2-68:~$ docker network ls
    NETWORK ID     NAME           DRIVER    SCOPE
    d1a42256454b   bridge         bridge    local
    7334f900df0e   host           host      local
    fa6aae724b3d   none           null      local
    efff9f7cc3a0   wordpressnet   bridge    local
    
  3. ボリュームを作成する。
  4. ubuntu@ip-172-30-2-68:~$ docker volume create wordpress_db_volume
    wordpress_db_volume
    ubuntu@ip-172-30-2-68:~$ docker volume ls
    DRIVER    VOLUME NAME
    local     mysqlvolume
    local     wordpress_db_volume
    
  5. MySQL コンテナを起動する
  6. [想定している設定]
        コンテナ名    wordpress_db
    [環境変数とここでの値]
        MYSQL_ROOT_PASSWORD    myrootpassword     
        MYSQL_DATABASE         wordpressdb
        MYSQL_USER             wordpressuser
        MYSQL_PASSWORD         wordpresspass
    
    ubuntu@ip-172-30-2-68:~$ docker run --name wordpress_db -dit -v wordpress_db_volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=myrootpassword -e MYSQL_DATABASE=wordpressdb -e MYSQL_USER=wordpressuser -e MYSQL_PASSWORD=wordpresspass --net wordpressnet mysql:5.7
    be559cba742615cb79e0d06a9c0a33158d53c8b6b35f18c0c202f167e68bd211
    ubuntu@ip-172-30-2-68:~$ docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
    be559cba7426   mysql:5.7   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp   wordpress_db
    
  7. WordPress コンテナを起動する
  8. [環境変数]
        WORDPRESS_DB_HOST        wordpress_db
        WORDPRESS_DB_NAME        wordpressdb
        WORDPRESS_DB_USER        wordpressuser
        WORDPRESS_DB_PASSWORD    wordpresspass
    
    ubuntu@ip-172-30-2-68:~$ docker run --name wordpress_db -dit -v wordpress_db_volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=myrootpassword -e MYSQL_DATABASE=wordpressdb -e MYSQL_USER=wordpressuser -e MYSQL_PASSWORD=wordpresspass --net wordpressnet mysql:5.7
    be559cba742615cb79e0d06a9c0a33158d53c8b6b35f18c0c202f167e68bd211
    ubuntu@ip-172-30-2-68:~$ docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
    be559cba7426   mysql:5.7   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp   wordpress_db
    ubuntu@ip-172-30-2-68:~$ docker run --name wordpress_app -dit -p 8080:80 -e WORDPRESS_DB_HOST=wordpress_db -e WORDPRESS_DB_NAME=word
    pressdb -e WORDPRESS_DB_USER=wordpressuser -e WORDPRESS_DB_PASSWORD=wordpresspass --net wordpressnet wordpress
    ebf1185f0ae11d9075e63598cce5397c400c41a9b6fdcb49b35cec6af1be73d5
    ubuntu@ip-172-30-2-68:~$ docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                   NAMES
    ebf1185f0ae1   wordpress   "docker-entrypoint.s…"   13 seconds ago   Up 11 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   wordpress_app
    be559cba7426   mysql:5.7   "docker-entrypoint.s…"   6 minutes ago    Up 6 minutes    3306/tcp, 33060/tcp                     wordpress_db
    
  9. 手元のPCのブラウザで http://DockerホストのIPアドレス:8080/ にアクセスする。
  10. http://35.76.124.169:8080/
    












  11. 後始末。
  12. ubuntu@ip-172-30-2-68:~$ docker stop wordpress_db wordpress_app
    wordpress_db
    wordpress_app
    ubuntu@ip-172-30-2-68:~$ docker rm wordpress_db wordpress_app
    wordpress_db
    wordpress_app
    ubuntu@ip-172-30-2-68:~$ docker network rm wordpressnet
    wordpressnet
    ubuntu@ip-172-30-2-68:~$ docker volume rm wordpress_db_volume
    wordpress_db_volume
    

Docker Compose

あらかじめコンテナの起動方法やボリューム、ネットワークの構成などを記述した定義ファイル (デフォルトでは docker-compose.yml) を用意しておいて、その定義ファイルからまとめて実行するのが Docker Compose である。

Docker Compose のインストール

  1. python をインストールする。
  2.   $ sudo apt install -y python3 python3-pip
    
  3. Docker Compose をインストールする。
  4.   $ sudo pip3 install docker-compose
    
    [エラーが起きた]
      Complete output from command python setup.py egg_info:
        Traceback (most recent call last):
          File "", line 1, in 
          File "/tmp/pip-build-y54pjucb/cryptography/setup.py", line 17, in 
            from setuptools_rust import RustExtension
        ModuleNotFoundError: No module named 'setuptools_rust'
    
                =============================DEBUG ASSISTANCE==========================
                If you are seeing an error here please try the following to
                successfully install cryptography:
    
                Upgrade to the latest pip and try again. This will fix errors for most
                users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
                =============================DEBUG ASSISTANCE==========================
    
    
        ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-y54pjucb/cryptography/
    
  5. pipをアップグレードする。
  6.   $ sudo python3 -m pip install --upgrade pip
    
  7. pipを使ってインストールする。エラーも出ず、うまくいった。
  8.   $ sudo pip install docker-compose
    
  9. Docker Compose がインストールされたことを確認する。
  10.   $ docker-compose --version
    /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
      from cryptography.hazmat.backends import default_backend
    docker-compose version 1.29.2, build unknown
    

Docker Compose を使った例

  1. 作業用ディレクトリと docker-compose.yml の準備
  2. ubuntu@ip-172-30-2-68:~$ mkdir wordpress
    ubuntu@ip-172-30-2-68:~$ cd wordpress
    
  3. ~/wordpress/docker-compose.yml を作る
  4. wordpress/docker-compose.yml
    version: "3"
    
    services:
      wordpress_db:
        image: mysql:5.7
        networks:
          - wordpressnet
        volumes:
          - wordpress_db_volume:/var/lib/mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: myrootpassword
          MYSQL_DATABASE: wordpressdb
          MYSQL_USER: wordpressuser
          MYSQL_PASSWORD: wordpresspass
    
      wordpress_app:
        depends_on:
          - wordpress_db
        image: wordpress
        networks:
          - wordpressnet
        ports:
          - 8080:80
        restart: always
        environment:
          WORDPRESS_DB_HOST: wordpress_db
          WORDPRESS_DB_NAME: wordpressdb
          WORDPRESS_DB_USER: wordpressuser
          WORDPRESS_DB_PASSWORD: wordpresspass
    
    networks:
      wordpressnet:
    
    volumes:
      wordpress_db_volume:
    
  5. Docker Compose でコンテナを起動する。
  6.   $ cd ~/wordpress
      $ docker-compose up -d
    /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
      from cryptography.hazmat.backends import default_backend
    Creating network "wordpress_wordpressnet" with the default driver
    Creating volume "wordpress_wordpress_db_volume" with default driver
    Creating wordpress_wordpress_db_1 ... done
    Creating wordpress_wordpress_app_1 ... done
    
  7. コンテナが起動されたことを確認する。
  8.   $ docker-compose ps
    /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
      from cryptography.hazmat.backends import default_backend
              Name                         Command               State                  Ports
    ---------------------------------------------------------------------------------------------------------
    wordpress_wordpress_app_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp,:::8080->80/tcp
    wordpress_wordpress_db_1    docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
    
  9. docker コマンドで状況を確認する。
  10. ubuntu@ip-172-30-2-68:~/wordpress$ docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                   NAMES
    143b1bc9558b   wordpress   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   wordpress_wordpress_app_1
    00719e2e791d   mysql:5.7   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   3306/tcp, 33060/tcp                     wordpress_wordpress_db_1
    ubuntu@ip-172-30-2-68:~/wordpress$ docker network ls
    NETWORK ID     NAME                     DRIVER    SCOPE
    d1a42256454b   bridge                   bridge    local
    7334f900df0e   host                     host      local
    fa6aae724b3d   none                     null      local
    25e9376c0a99   wordpress_wordpressnet   bridge    local
    ubuntu@ip-172-30-2-68:~/wordpress$ docker volume ls
    DRIVER    VOLUME NAME
    local     57d578464741e0e819fc1f931e8154e5685a3efc5c5b1f1aac388889b317c709
    local     563b940b7864e020fab60d6a40858a03dcb2fa3c9a2f64c8c937bf650627ad0d
    local     mysqlvolume
    local     wordpress_wordpress_db_volume
    
  11. コンテナの停止と破棄
  12. ubuntu@ip-172-30-2-68:~/wordpress$ docker-compose down
    /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
      from cryptography.hazmat.backends import default_backend
    Stopping wordpress_wordpress_app_1 ... done
    Stopping wordpress_wordpress_db_1  ... done
    Removing wordpress_wordpress_app_1 ... done
    Removing wordpress_wordpress_db_1  ... done
    Removing network wordpress_wordpressnet
    ubuntu@ip-172-30-2-68:~/wordpress$ docker-compose ps
    /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
      from cryptography.hazmat.backends import default_backend
    Name   Command   State   Ports
    ------------------------------