Docker 入門
Python Django サーバを Docker で構築する
MySQL を使った Django プロジェクトを公開する
ここではバックエンドのデータベースに MySQL コンテナを使用する環境を構築します.これは単純な SQLite を利用するよりもより実践的な例となることでしょう.具体的には,Nginx コンテナを Web サーバとして http://localhost/ という URL でリクエストを受け付け,Python Django で開発したコメントアプリケーションサーバのコンテナに転送します.データベースは MySQL のコンテナで運用します.
このページの Docker サンプルコードは GitHub の Django02MySQL フォルダで公開しています.また,Python Django のコードも GitHub で公開しています.
任意の新規ディレクトリを作成し,その中に次の構成でディレクトリとファイルを設置します.なお,Django のプロジェクトは django ディレクトリに設置しますが,Docker の Git リポジトリに Django のコードが含まれないように,django/.gitignore を設置しておきます.また,ログファイルも Git に含まれないように logs/nginx/.gitignore を設置しておきます.さらに,.env ファイルには MySQL のユーザ名とパスワードを保存しますが,やはりそのファイルが Git のリポジトリに含まれないように .gitignore を設置します.
% tree -a -F ⏎
./
├── .env
├── .env.example
├── .gitignore
├── django/
│ └── .gitignore
├── docker-compose.yml
├── logs/
│ └── nginx/
│ └── .gitignore
├── mysql/
│ ├── my.cnf
│ └── script/
│ └── init-db.sql
├── nginx/
│ ├── conf.d/
│ │ └── default.conf
│ └── Dockerfile
├── python/
│ ├── Dockerfile
│ └── entrypoint.sh*
└── Readme.md
9 directories, 13 files
%
全体の設計図である docker-compose.yml は次のような内容にします.MySQL のコンテナ(サービス名 mysql),Python アプリケーションサーバ用のコンテナ(サービス名 python_app),Web サーバ用の Nginx コンテナ(サービス名 web)という合計3つのコンテナとデータベースを保存するためのボリューム(ボリューム名 mysqlvolume)からなる構成です.
docker-compose.yml
services:
mysql:
image: mysql:8.0
container_name: python_mysql_02
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
# MYSQL_DATABASE: ${MYSQL_DATABASE} # データベースの作成は script で実行
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
- mysqlvolume:/var/lib/mysql
- ./mysql/script:/docker-entrypoint-initdb.d
ports:
- 3306:3306
python_app:
build:
context: .
dockerfile: ./python/Dockerfile
container_name: python_app_02
volumes:
- ./python/entrypoint.sh:/entrypoint.sh
- ./django/code:/code
expose:
- "8000"
command: /bin/sh -c "pip install -r /code/requirements.txt && chmod +x /entrypoint.sh && /entrypoint.sh"
web:
image: nginx:1.25.5
container_name: python_nginx_02
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
# - ./web/html:/usr/share/nginx/html
- ./django/code/comments/static:/static
- ./logs/nginx:/var/log/nginx
ports:
- "80:80"
depends_on:
- python_app
volumes:
mysqlvolume:
まず,MySQL については管理者パスワード (MYSQL_ROOT_PASSWORD) とユーザ (docker-user) 用のパスワード (MYSQL_PASSWORD) を .env に保存します.実際には複雑なパスワードを指定し,かつ,2種類のパスワードが異なるようにしてください.これらの値は docker-compose.yml の6-9行目において ${MYSQL_ROOT_PASSWORD} などの記述で読み込まれます.
.env
MYSQL_ROOT_PASSWORD=password
MYSQL_USER=docker-user
MYSQL_PASSWORD=password
また,機密情報を含む .env が Git のリポジトリに含まれないようにするために .gitignore を作成します.
.gitignore
.env
一方で GitHub からクローンを作成したときに,.env のサンプルとなるように .env.example を作成しておき,これは Git リポジトリに含めておきます.
.env.example
MYSQL_ROOT_PASSWORD=password
MYSQL_USER=docker-user
MYSQL_PASSWORD=password
MySQL の設定を次のファイルに加えます.これらの設定は日本語文字がターミナルで文字化けしないようにするための設定です.
mysql/my.cnf
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
[client]
default-character-set=utf8mb4
MySQL の初期設定として django-comments データベースを作成し,権限を付与するスクリプトを作成します.ここで,5行目にユーザ名 docker-user が指定されていますが,このユーザ名は .env の MYSQL_USER と同じものを指定するようにしてください.
mysql/script/init-db.sql
-- ユーザの作成とパスワードの設定は docker-compose.yml で実行済み
-- djangoデータベースの作成と権限付与
CREATE DATABASE IF NOT EXISTS `django-comments`;
GRANT ALL PRIVILEGES ON `django-comments`.* TO 'docker-user';
FLUSH PRIVILEGES;
次に Python アプリケーションサーバのコンテナを設定します.python ディレクトリに Dockerfile を次のとおり作成します.
python/Dockerfile
FROM python:3.11
# pycファイル(および__pycache__)の生成を行わないようにする
ENV PYTHONDONTWRITEBYTECODE=1
# 標準出力・標準エラーのストリームのバッファリングを行わない
ENV PYTHONUNBUFFERED=1
# コンテナのワークディレクトリを/codeに指定
WORKDIR /code
RUN pip install --upgrade pip
コンテナの起動時に実行されるスクリプトを作成します.前のページと同様に gunicorn を利用します.
python/entrypoint.sh
#!/bin/bash
# pip list
gunicorn django_comment.wsgi:application --bind 0.0.0.0:8000
Web サーバとなる Nginx コンテナについても nginx ディレクトリに Dockerfile を作成します.
nginx/Dockerfile
FROM nginx:1.25.5
COPY conf.d/default.conf /etc/nginx/conf.d/default.conf
設定ファイルは次の内容にします.つまり,/static/ 以外へのリクエストは python_app サービスへ8000番ポートを利用して転送されることを意味しています.
nginx/conf.d/default.conf
upstream python {
server python_app:8000;
}
server {
listen 80;
server_name 0.0.0.0;
location / {
proxy_pass http://python;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
location /static/ {
alias /static/;
}
}
また,Nginx へのアクセスログ (access.log および error.log) は logs/nginx/ ディレクトリに格納されます.これらのログファイルが Git のリポジトリに含まれないように .gitignore を作成しておきます.
logs/nginx/.gitignore
*
Python Django のプロジェクトは django/code/ 以下に配置することになります.Django のプロジェクト自体が Git で管理されるはずなので,Docker のリポジトリには Django のプロジェクトが含まれないように django/.gitignore を作成します.
django/.gitignore
*
Docker に関する設定が終われば,Python Django のコードを配置します.ここでは,GitHub で公開している Django プロジェクトについて,リポジトリのクローンを作成します.
% cd django ⏎ # (1) ディレクトリを移動 % git clone https://github.com/rinsaka/django_comment2022_mysql.git ⏎ # (2) クローンを作成 Cloning into 'django_comment2022_mysql'... remote: Enumerating objects: 461, done. remote: Counting objects: 100% (461/461), done. remote: Compressing objects: 100% (251/251), done. remote: Total 461 (delta 222), reused 438 (delta 199), pack-reused 0 (from 0) Receiving objects: 100% (461/461), 419.40 KiB | 6.17 MiB/s, done. Resolving deltas: 100% (222/222), done. % ls ⏎ # (3) クローンが作成されたことを確認 django_comment2022_mysql % mv django_comment2022_mysql code ⏎ # (4) ディレクトリ名を変更 % ls ⏎ # (5) 変更されたことを確認 code % cd code ⏎ # (6) ディレクトリを移動 % ls -a ⏎ # (7) 隠しファイルも含めてファイルの一覧を表示 . .gitignore readme.md .. comments requirements.txt .coverage db.sqlite3.sample staticfiles .env.example django_comment .git manage.py % cp .env.example .env ⏎ # (8) ファイルのコピーを作成 %
上の作業で django/code/ ディレクトリに .env ファイルが作成されました.サンプルファイルは次のようになっています.
django/code/.env(サンプル)
# 公開する URL をサブディレクトリにする場合は APP_URL に指定する
APP_URL=
# APP_URL=/django_comment
# Falseにすると本番環境としてエラーページが 404 ページになる
DEBUG=True
# DEBUG=False
# データベースを sqlite か mysql のどちらかを選ぶ
# mysql の場合は DB_NAME 以下の設定も必要
DB=sqlite
# DB=mysql
# DB_NAME=django-comments
# DB_USER=docker-user
# DB_PASSWORD=password
# DB_HOST=mysql
# DB_PORT=3306
これを次のとおり書き換えます.ここで,15行目の DB_NAME は init-db.sql の4行目で指定したデータベース名と同じ設定にしてください.また,16行目の DB_USER や17行目の DB_PASSWORD は .env に設定した MYSQL_USER や MYSQL_PASSWORD と同じ内容を指定してください.
django/code/.env
# 公開する URL をサブディレクトリにする場合は APP_URL に指定する
APP_URL=
# APP_URL=/django_comment
# Falseにすると本番環境としてエラーページが 404 ページになる
DEBUG=True
# DEBUG=False
# データベースを sqlite か mysql のどちらかを選ぶ
# mysql の場合は DB_NAME 以下の設定も必要
# DB=sqlite
DB=mysql
DB_NAME=django-comments
DB_USER=docker-user
DB_PASSWORD=password
DB_HOST=mysql
DB_PORT=3306
2つ上の階層(Docker のディレクトリ)に戻っておきます.
% cd .. ⏎ % cd .. ⏎ % ls ⏎ django mysql Readme.md docker-compose.yml nginx logs python %
すべてのファイルの準備ができたらコンテナを起動します.
% docker compose up -d ⏎
[+] Running 13/13
✔ web Pulled 3.4s
✔ mysql Pulled 16.9s
✔ ecb83b09a418 Pull complete 3.4s
✔ ffbb77c7a643 Pull complete 3.4s
✔ 8dc2f5fb29fb Pull complete 3.4s
✔ 932ae097baf0 Pull complete 3.5s
✔ 62105207231f Pull complete 3.5s
✔ 9d8b8ceea9d7 Pull complete 3.6s
✔ 73f02261ef9b Pull complete 5.0s
✔ 8f7f67d38417 Pull complete 5.0s
✔ a0f47f504c03 Pull complete 12.1s
✔ 05cff585f6c0 Pull complete 12.1s
✔ 8bcf37a9b9eb Pull complete 12.2s
[+] Building 1.8s (9/9) FINISHED
=> [internal] load local bake definitions 0.0s
=> => reading from stdin 594B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 385B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11 1.6s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/python:3.11@sha256:a2bd92ce584000ca1a93aea40b7afa13 0.0s
=> CACHED [2/3] WORKDIR /code 0.0s
=> CACHED [3/3] RUN pip install --upgrade pip 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:27da249c9d9d5894ff7b26d7b34a8cdfc99eb538b626ba8de62f6df3 0.0s
=> => naming to docker.io/library/django02mysql-python_app 0.0s
=> resolving provenance for metadata file 0.0s
[+] Running 6/6
✔ django02mysql-python_app Built 0.0s
✔ Network django02mysql_default Created 0.0s
✔ Volume "django02mysql_mysqlvolume" Create... 0.0s
✔ Container python_mysql_02 Started 0.6s
✔ Container python_app_02 Started 0.6s
✔ Container python_nginx_02 Started 0.3s
%
Docker イメージの一覧を確認します.過去の作業や上の作業でダウンロード(プル)やビルドされたイメージが表示されるはずです.なお,ビルドされたイメージの名称は ディレクトリ名-サービス名 になっていることに注意してください.
% docker image ls ⏎
REPOSITORY TAG IMAGE ID CREATED SIZE
django01sqlite-python_app latest aefe61bf7d8d 21 minutes ago 1.13GB
django02mysql-python_app latest 27da249c9d9d 21 minutes ago 1.13GB
mysql 8.0 4b0d5362c262 2 weeks ago 776MB
nginx 1.25.5 8dd77ef2d82e 17 months ago 193MB
%
3つのコンテナが起動状態であることを確認します.
% docker container ls ⏎
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23e0909892c0 nginx:1.25.5 "/docker-entrypoint.…" 43 seconds ago Up 42 seconds 0.0.0.0:80->80/tcp, [::]:80->80/tcp python_nginx_02
06d27c96deef django02mysql-python_app "/bin/sh -c 'pip ins…" 44 seconds ago Up 43 seconds 8000/tcp python_app_02
5f529459da2b mysql:8.0 "docker-entrypoint.s…" 44 seconds ago Up 43 seconds 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp python_mysql_02
%
MySQL のコンテナにログインし,管理者ID (root) でデータベースにログインします.(4) では Django のプロジェクトで利用するデータベースが利用できることを確認します.
% docker compose exec mysql /bin/bash ⏎ # (1) コンテナにログイン bash-5.1# mysql -u root -p ⏎ # (2) rootユーザで MySQL に接続 Enter password: ⏎ # (3) パスワードを入力(表示されない) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.43 MySQL Community Server - GPL Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; ⏎ # (4) データベースの一覧を表示 +--------------------+ | Database | +--------------------+ | django-comments | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> exit ⏎ # (5) データベースからログアウト Bye bash-5.1# exit ⏎ # (6) コンテナからログアウト exit %
次に,Django で利用するユーザ (docker-user) でもデータベースにログインします.このとき,利用可能なデータベースが管理者よりも少ないことに注意してください.
% docker compose exec mysql /bin/bash ⏎ bash-5.1# mysql -u docker-user -p ⏎ Enter password: ⏎ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.43 MySQL Community Server - GPL Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; ⏎ +--------------------+ | Database | +--------------------+ | django-comments | | information_schema | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) mysql> exit ⏎ Bye bash-5.1# exit ⏎ exit %
次に,Python アプリケーションサーバにログインして,データベースにテーブルを生成し,サンプルデータを投入します.次のコマンドでアプリケーションサーバ(サービス名 python_app)にログインします.
% docker compose exec python_app /bin/bash ⏎ root@06d27c96deef:/code# ls ⏎ comments django_comment readme.md staticfiles db.sqlite3.sample manage.py requirements.txt root@06d27c96deef:/code#
次のコマンドでマイグレーションの実行状態を確認します.まだ一切実行されていない(テーブルが生成されていない)ことがわかります.
root@06d27c96deef:/code# python manage.py showmigrations ⏎
System check identified some issues:
WARNINGS:
?: (staticfiles.W004) The directory '/code/static' in the STATICFILES_DIRS setting does not exist.
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
[ ] 0003_logentry_add_action_flag_choices
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length
[ ] 0010_alter_group_name_max_length
[ ] 0011_update_proxy_permissions
[ ] 0012_alter_user_first_name_max_length
comments
[ ] 0001_initial
[ ] 0002_alter_comment_options
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
sessions
[ ] 0001_initial
root@06d27c96deef:/code#
テーブルを生成します.
root@06d27c96deef:/code# python manage.py migrate ⏎
System check identified some issues:
WARNINGS:
?: (staticfiles.W004) The directory '/code/static' in the STATICFILES_DIRS setting does not exist.
Operations to perform:
Apply all migrations: admin, auth, comments, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying comments.0001_initial... OK
Applying comments.0002_alter_comment_options... OK
Applying sessions.0001_initial... OK
root@06d27c96deef:/code#
マイグレーションの実行状態を確認します.すべてのテーブルが生成されているようです.
root@06d27c96deef:/code# python manage.py showmigrations ⏎
System check identified some issues:
WARNINGS:
?: (staticfiles.W004) The directory '/code/static' in the STATICFILES_DIRS setting does not exist.
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
comments
[X] 0001_initial
[X] 0002_alter_comment_options
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
root@06d27c96deef:/code#
サンプルデータをデータベースに投入して,Python アプリケーションサーバからログアウトします.
root@06d27c96deef:/code# python manage.py loaddata comments/fixtures/comments-data.json ⏎ System check identified some issues: WARNINGS: ?: (staticfiles.W004) The directory '/code/static' in the STATICFILES_DIRS setting does not exist. Installed 10 object(s) from 1 fixture(s) root@06d27c96deef:/code# root@06d27c96deef:/code# exit ⏎ exit %
% docker compose exec mysql /bin/bash ⏎ # コンテナにログイン bash-5.1# mysql -u docker-user -p ⏎ # MySQL にログイン Enter password: ⏎ # パスワードの入力 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.43 MySQL Community Server - GPL Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; ⏎ # データベースの一覧表示 +--------------------+ | Database | +--------------------+ | django-comments | | information_schema | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) mysql> use django-comments; ⏎ # データベースの切替 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> SHOW TABLES; ⏎ # テーブルの一覧表示 +----------------------------+ | Tables_in_django-comments | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | comments_comment | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 11 rows in set (0.00 sec) mysql> DESC comments_comment; ⏎ # テーブルの定義を確認 +------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------+------+-----+---------+----------------+ | id | bigint | NO | PRI | NULL | auto_increment | | title | varchar(200) | NO | | NULL | | | body | varchar(1000) | NO | | NULL | | | created_at | datetime(6) | NO | | NULL | | | updated_at | datetime(6) | NO | | NULL | | +------------+---------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec) mysql> SELECT * from comments_comment; ⏎ # 全データの取得 +----+--------------------------+---------------------------+----------------------------+----------------------------+ | id | title | body | created_at | updated_at | +----+--------------------------+---------------------------+----------------------------+----------------------------+ | 1 | 最初のコメント | コメントの本文 | 2022-07-01 12:00:00.000000 | 2022-07-01 12:00:00.000000 | | 2 | 2個目のコメント | 2個目の本文 | 2022-07-02 13:00:00.000000 | 2022-07-02 13:00:00.000000 | | 3 | <3個目>のコメント | <h1>3個目の本文</h1> | 2022-07-02 13:03:00.000000 | 2022-07-02 13:03:00.000000 | | 4 | 4個目のコメント | 4個目の本文 | 2022-07-02 13:04:00.000000 | 2022-07-02 13:04:00.000000 | | 5 | 5個目のコメント | 5個目の本文 | 2022-07-02 13:05:00.000000 | 2022-07-02 13:05:00.000000 | | 6 | 6個目のコメント | 6個目の本文 | 2022-07-02 13:06:00.000000 | 2022-07-02 13:06:00.000000 | | 7 | 7個目のコメント | 7個目の本文 | 2022-07-02 13:07:00.000000 | 2022-07-02 13:07:00.000000 | | 8 | 8個目のコメント | 8個目の本文 | 2022-07-02 13:08:00.000000 | 2022-07-02 13:08:00.000000 | | 9 | 9個目のコメント | 9個目の本文 | 2022-07-02 13:09:00.000000 | 2022-07-02 13:20:00.000000 | | 10 | 10個目のコメント | 10個目の本文 | 2022-07-02 13:10:00.000000 | 2022-07-02 13:10:00.000000 | +----+--------------------------+---------------------------+----------------------------+----------------------------+ 10 rows in set (0.00 sec) mysql> exit ⏎ # データベースからログアウト Bye bash-5.1# exit ⏎ # コンテナからログアウト exit %
すべての準備が整ったので,ブラウザで http://localhost/ に接続します.
comments アプリケーションに移動すると正しくシステムが動作していることが確認できるはずです.コメントの新規投稿や編集,削除などの機能を試してください.
起動中のコンテナが3つあることを確認します.
% docker container ls ⏎
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23e0909892c0 nginx:1.25.5 "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp python_nginx_02
06d27c96deef django02mysql-python_app "/bin/sh -c 'pip ins…" 8 minutes ago Up 8 minutes 8000/tcp python_app_02
5f529459da2b mysql:8.0 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp python_mysql_02
%
すべてのコンテナを終了して破棄します.
% docker compose down ⏎
[+] Running 4/4
✔ Container python_mysql_02 Removed 0.9s
✔ Container python_nginx_02 Removed 0.2s
✔ Container python_app_02 Removed 10.2s
✔ Network django02mysql_default Removed 0.2s
%
すべてのコンテナが破棄されたことを確認します.
% docker container ls -a ⏎
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
%
Docker イメージの一覧を表示します.
% docker image ls ⏎
REPOSITORY TAG IMAGE ID CREATED SIZE
django01sqlite-python_app latest aefe61bf7d8d 30 minutes ago 1.13GB
django02mysql-python_app latest 27da249c9d9d 30 minutes ago 1.13GB
mysql 8.0 4b0d5362c262 2 weeks ago 776MB
nginx 1.25.5 8dd77ef2d82e 17 months ago 193MB
%
ボリュームの一覧を表示します.ここで MySQL データベースの内容はこのボリュームに格納されるので,コンテナを終了したり破棄したりしてもデータは残ることに注意してください.
% docker volume ls ⏎
DRIVER VOLUME NAME
local django02mysql_mysqlvolume
%
ストレージの消費量は時折確認すると良いでしょう.
% docker system df ⏎
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 4 0 2.1GB 2.1GB (100%)
Containers 0 0 0B 0B
Local Volumes 1 0 198.4MB 198.4MB (100%)
Build Cache 13 0 692B 692B
%



