Docker Compose クィックスタート

This tutorial aims to introduce fundamental concepts of Docker Compose by guiding you through the development of a basic Python web application.

Using the Flask framework, the application features a hit counter in Redis, providing a practical example of how Docker Compose can be applied in web development scenarios.

The concepts demonstrated here should be understandable even if you're not familiar with Python.

This is a non-normative example that just highlights the key things you can do with Compose.

前提条件

以下を行っておくことが必要です。

  • Docker Compose の最新版をインストールしていること。
  • Docker の考え方や Docker がどのようにして動作しているのかの基本を理解していること。

手順 1: セットアップ

  1. プロジェクト用のディレクトリを生成します。

    $ mkdir composetest
    $ cd composetest
    
  2. プロジェクトディレクトリ内に app.py というファイルを生成して、以下のコードを記述します。

    import time
    
    import redis
    from flask import Flask
    
    app = Flask(__name__)
    cache = redis.Redis(host='redis', port=6379)
    
    def get_hit_count():
        retries = 5
        while True:
            try:
                return cache.incr('hits')
            except redis.exceptions.ConnectionError as exc:
                if retries == 0:
                    raise exc
                retries -= 1
                time.sleep(0.5)
    
    @app.route('/')
    def hello():
        count = get_hit_count()
        return f'Hello World! I have been seen {count} times.\n'

    この例において redis とは、このアプリケーションネットワーク上の redis コンテナーのホスト名です。 redis のデフォルトポートとして 6379 を利用します。

    メモ

    Note the way the get_hit_count function is written. This basic retry loop attempts the request multiple times if the Redis service is not available. This is useful at startup while the application comes online, but also makes the application more resilient if the Redis service needs to be restarted anytime during the app's lifetime. In a cluster, this also helps handling momentary connection drops between nodes.

  3. プロジェクト用のディレクトリにもう一つ requirements.txt という名称のファイルを作成し、次のコードを記述します。

    flask
    redis
  4. Dockerfile を生成し、次のコードを記述します。

    # syntax=docker/dockerfile:1
    FROM python:3.10-alpine
    WORKDIR /code
    ENV FLASK_APP=app.py
    ENV FLASK_RUN_HOST=0.0.0.0
    RUN apk add --no-cache gcc musl-dev linux-headers
    COPY requirements.txt requirements.txt
    RUN pip install -r requirements.txt
    EXPOSE 5000
    COPY . .
    CMD ["flask", "run", "--debug"]

    これは Docker に対して以下の指示を行います。

    • Python 3.10 イメージを使ってこのイメージをビルドします。
    • ワーキングディレクトリを /code に設定します。
    • flask コマンドが利用する環境変数を設定します。
    • gcc やその依存パッケージをインストールします。
    • requirements.txt をコピーして Python 依存パッケージをインストールします。
    • イメージにメタデータを追加して、コンテナーがポート 5000 をリッスンするようにします。
    • このプロジェクトのカレントディレクトリ . をイメージ内のワークディレクトリ . にコピーします。
    • コンテナーのデフォルトコマンドを flask run --debug にします。

    重要

    Dockerfile には .txt などのような拡張子がないことを確認してください。 エディターの中には自動的に拡張子をつけてしまうものがあり、その場合にはアプリケーション実行時にエラーとなってしまいます。

    Dockerfile の書き方の詳細は Dockerfile リファレンス を参照してください。

手順 2: Compose ファイルでのサービス定義

Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file.

Create a file called compose.yaml in your project directory and paste the following:

services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"

This Compose file defines two services: web and redis.

The web service uses an image that's built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 8000. This example service uses the default port for the Flask web server, 5000.

The redis service uses a public Redis image pulled from the Docker Hub registry.

For more information on the compose.yaml file, see How Compose works.

手順 3: Compose によるアプリのビルドと実行

With a single command, you create and start all the services from your configuration file.

  1. From your project directory, start up your application by running docker compose up.

    $ docker compose up
    
    Creating network "composetest_default" with the default driver
    Creating composetest_web_1 ...
    Creating composetest_redis_1 ...
    Creating composetest_web_1
    Creating composetest_redis_1 ... done
    Attaching to composetest_web_1, composetest_redis_1
    web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    redis_1  | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1  | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1  | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    web_1    |  * Restarting with stat
    redis_1  | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
    redis_1  | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    web_1    |  * Debugger is active!
    redis_1  | 1:M 17 Aug 22:11:10.483 # Server initialized
    redis_1  | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    web_1    |  * Debugger PIN: 330-787-903
    redis_1  | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
    

    Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.

  2. Enter http://localhost:8000/ in a browser to see the application running.

    If this doesn't resolve, you can also try http://127.0.0.1:8000.

    You should see a message in your browser saying:

    Hello World! I have been seen 1 times.
    hello world in browser
  3. Refresh the page.

    The number should increment.

    Hello World! I have been seen 2 times.
    hello world in browser
  4. Switch to another terminal window, and type docker image ls to list local images.

    Listing images at this point should return redis and web.

    $ docker image ls
    
    REPOSITORY        TAG           IMAGE ID      CREATED        SIZE
    composetest_web   latest        e2c21aa48cc1  4 minutes ago  93.8MB
    python            3.4-alpine    84e6077c7ab6  7 days ago     82.5MB
    redis             alpine        9d8fa9aa0e5b  3 weeks ago    27.5MB
    

    You can inspect images with docker inspect <tag or id>.

  5. Stop the application, either by running docker compose down from within your project directory in the second terminal, or by hitting CTRL+C in the original terminal where you started the app.

手順 4: Compose Watch 利用のための Compose ファイル修正

Edit the compose.yaml file in your project directory to use watch so you can preview your running Compose services which are automatically updated as you edit and save your code:

services:
  web:
    build: .
    ports:
      - "8000:5000"
    develop:
      watch:
        - action: sync
          path: .
          target: /code
  redis:
    image: "redis:alpine"

Whenever a file is changed, Compose syncs the file to the corresponding location under /code inside the container. Once copied, the bundler updates the running application without a restart.

For more information on how Compose Watch works, see Use Compose Watch. Alternatively, see Manage data in containers for other options.

メモ

For this example to work, the --debug option is added to the Dockerfile. The --debug option in Flask enables automatic code reload, making it possible to work on the backend API without the need to restart or rebuild the container. After changing the .py file, subsequent API calls will use the new code, but the browser UI will not automatically refresh in this small example. Most frontend development servers include native live reload support that works with Compose.

手順 5: Compose を使ったアプリの再ビルドと実行

From your project directory, type docker compose watch or docker compose up --watch to build and launch the app and start the file watch mode.

$ docker compose watch
[+] Running 2/2
 ✔ Container docs-redis-1 Created                                                                                                                                                                                                        0.0s
 ✔ Container docs-web-1    Recreated                                                                                                                                                                                                      0.1s
Attaching to redis-1, web-1
         ⦿ watch enabled
...

Check the Hello World message in a web browser again, and refresh to see the count increment.

手順 6: アプリケーションのアップデート

To see Compose Watch in action:

  1. Change the greeting in app.py and save it. For example, change the Hello World! message to Hello from Docker!:

    return f'Hello from Docker! I have been seen {count} times.\n'
  2. Refresh the app in your browser. The greeting should be updated, and the counter should still be incrementing.

    hello world in browser
  3. Once you're done, run docker compose down.

手順 7: サービスの分割

Using multiple Compose files lets you customize a Compose application for different environments or workflows. This is useful for large applications that may use dozens of containers, with ownership distributed across multiple teams.

  1. In your project folder, create a new Compose file called infra.yaml.

  2. Cut the Redis service from your compose.yaml file and paste it into your new infra.yaml file. Make sure you add the services top-level attribute at the top of your file. Your infra.yaml file should now look like this:

    services:
      redis:
        image: "redis:alpine"
  3. In your compose.yaml file, add the include top-level attribute along with the path to the infra.yaml file.

    include:
       - infra.yaml
    services:
      web:
        build: .
        ports:
          - "8000:5000"
        develop:
          watch:
            - action: sync
              path: .
              target: /code
  4. Run docker compose up to build the app with the updated Compose files, and run it. You should see the Hello world message in your browser.

This is a simplified example, but it demonstrates the basic principle of include and how it can make it easier to modularize complex applications into sub-Compose files. For more information on include and working with multiple Compose files, see Working with multiple Compose files.

手順 8: Experiment with some other commands

  • If you want to run your services in the background, you can pass the -d flag (for "detached" mode) to docker compose up and use docker compose ps to see what is currently running:

    $ docker compose up -d
    
    Starting composetest_redis_1...
    Starting composetest_web_1...
    
    $ docker compose ps
    
           Name                      Command               State           Ports
    -------------------------------------------------------------------------------------
    composetest_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
    composetest_web_1     flask run                        Up      0.0.0.0:8000->5000/tcp
    
  • Run docker compose --help to see other available commands.

  • If you started Compose with docker compose up -d, stop your services once you've finished with them:

    $ docker compose stop
    
  • You can bring everything down, removing the containers entirely, with the docker compose down command.

次に読むものは