はじめに
Docker ComposeでNginxコンテナを構築して、設定ファイルを更新しながら設定ファイルの検証ができるような環境を構築します。
ホスト側のディレクトリをマウントすることで、ホスト側のファイルを更新しながらNginxの動作を検証できるようにします。
Docker Composeについては、下記で解説しています。

【超基礎】Docker Compose
はじめにDocker Composeについて、基本的な使い方の解説と具体的な例としてDjango/PostgreSQLアプリを構築をしてみたいと思います。Dockerについてはこちらで解説しています。環境DocerとDocker ...
準備
まずは必要なファイルを準備します。
必要なファイルとディレクトリは以下のようになります。
.
├── default.conf
└── docker-compose.yml
default.conf
Nginxの設定ファイルとなるdefault.conf
を用意します。
下記はデフォルトで用意されている設定内容と同じになります。
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
docker-compose.yml
次にdocker-compose.yml
を作成します。
公式のNginxイメージを利用してコンテナを作成します。また、先ほど用意したdefault.conf
とコンテナ側のetc/nginx/conf.d/default.conf
をマウントします。
version: "3"
services:
nginx:
image: nginx:latest
container_name: nginx00
ports:
- "80:80"
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
コンテナを実行
必要なファイルが準備できたらコンテナを実行します。
❯ docker-compose up -d
設定を変更
ここからNginxの設定を変更しながらNginxの動作を検証していきます。
ホスト側のdefault.conf
を変更したら、下記コマンドで再起動します。
❯ docker-compose restart
default.conf
はマウントされているため、ホスト側のファイルを更新するとコンテナ側のファイルも更新されます。