Windows11 WSL2 Ubuntu 24.04 LTS Docker
nginx Docker Compose ファイル実行
ここまでで、nginx Docker Compose ファイルの準備が終わりました。
引き続き nginx Docker Compose ファイルを実行していきます。
WSL2 Ubuntu 24.04 LTS ディスクトップ日本語からターミナルを開きます。
PowerShell 7 からでもターミナルを開くことができます。
PowerShell 7 追加インスト-ル参照。
コンテナフォルダ \doc-nginx-c に移動します。
yamada@yama:~$ cd doc-nginx-c
移動できたら次のコマンドを入力実行します。
yamada@yama:~/doc-nginx-c$ docker compose up -d
\doc-nginx-c にある docker-compose.yml を -d バックグラウンドで処理します。
[結果]
yamada@yama:~/doc-nginx-c$ docker compose up -d [+] Running 8/8 ✔ web Pulled 8.5s ✔ 61320b01ae5e Pull complete 4.4s ✔ 670a101d432b Pull complete 5.1s ✔ 405bd2df85b6 Pull complete 5.1s ✔ cc80efff8457 Pull complete 5.1s ✔ 2b9310b2ee4b Pull complete 5.1s ✔ 6c4aa022e8e1 Pull complete 5.2s ✔ abddc69cb49d Pull complete 5.2s [+] Running 2/2 ✔ Network doc-nginx-c_default Created 0.0s ✔ Container doc-nginx-c-web-1 Started 0.3s
コンテナ nginx が作成されました。
ブラウザのアドレスバーに
http://localhost:8091
と入力してみましょう。
Docker 環境の構築に成功していれば
\\wsl.localhost\Ubuntu-24.04\home\yamada\doc-nginx-c\htdocs
に置いた index.html の内容が表示されます。
ハロー!
Docker nginx
docker-compose.yml で起動しました。
Docker Compose で立ち上げたコンテナは
以下のようにで動作を止めることができます。
yamada@yama:~/doc-nginx-c$ docker compose down [+] Running 2/2 ✔ Container doc-nginx-c-web-1 Removed 0.4s ✔ Network doc-nginx-c_default Removed 0.3s
特定のコンテナ内でコマンド実行
Docker nginx Compose ファイル作成の時、
コンテナ内の nginx の DocumentRoot ディレクトリのデフォルト値は
/usr/share/nginx/html
に設定されていると書きました。
なぜそんなことが分かったのでしょうか。
exec を使うと立ち上げた特定のコンテナ内でコマンドを実行することができます。
まずは docker compose start でコンテナを立ち上げます。
次に
docker compose ps
で SERVICE 名を確認しておきましょう。
docker compose exec [SERVICE 名] bash
とします。
exec の後には この SERVICE 名を入力します。
今回は web になります。
bash というのは対話式のコマンドで bash という名前のシェルを立ち上げます。
bash を使うと新たな画面が開いてコンテナ内の操作をすることができます。
そして、exec を実行した後は exit コマンドで bash から出ることができます。
それではコンテナ nginx のファイル構成を bash を使用して確認して行きます。
yamada@yama:~$ cd doc-nginx-c yamada@yama:~/doc-nginx-c$ docker compose start [結果] [+] Running 1/1 ✔ Container doc-nginx-c-web-1 Started 0.2s yamada@yama:~/doc-nginx-c$ docker compose ps [結果] NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS doc-nginx-c-web-1 nginx "/docker-entrypoint.…" web 6 hours ago Up 4 minutes 0.0.0.0:8091->80/tcp, [::]:8091->80/tcp yamada@yama:~/doc-nginx-c$ docker compose exec web bash [結果] root@b64dcf3f3411:/#
シェルが開きます。
このシェルで
ls コマンド
ls ディレクトリの内容をリスト表示する
-l 詳細リスト形式を表示する
-a .で始まる要素を無視しない
を使ってコンテナ nginx のファイルディレクリを見てみます。
root@fc4a768d19d7:/# ls -la total 72 drwxr-xr-x 1 root root 4096 Jun 1 09:26 . drwxr-xr-x 1 root root 4096 Jun 1 09:26 .. -rwxr-xr-x 1 root root 0 Jun 1 09:26 .dockerenv lrwxrwxrwx 1 root root 7 May 20 00:00 bin -> usr/bin drwxr-xr-x 2 root root 4096 May 9 14:50 boot drwxr-xr-x 5 root root 340 Jun 1 09:26 dev drwxr-xr-x 1 root root 4096 May 21 23:13 docker-entrypoint.d -rwxr-xr-x 1 root root 1620 May 21 23:12 docker-entrypoint.sh drwxr-xr-x 1 root root 4096 Jun 1 09:26 etc drwxr-xr-x 2 root root 4096 May 9 14:50 home lrwxrwxrwx 1 root root 7 May 20 00:00 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 20 00:00 lib64 -> usr/lib64 drwxr-xr-x 2 root root 4096 May 20 00:00 media drwxr-xr-x 2 root root 4096 May 20 00:00 mnt drwxr-xr-x 2 root root 4096 May 20 00:00 opt dr-xr-xr-x 405 root root 0 Jun 1 09:26 proc drwx------ 2 root root 4096 May 20 00:00 root drwxr-xr-x 1 root root 4096 Jun 1 09:26 run lrwxrwxrwx 1 root root 8 May 20 00:00 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 May 20 00:00 srv dr-xr-xr-x 13 root root 0 Jun 1 09:26 sys drwxrwxrwt 2 root root 4096 May 20 00:00 tmp drwxr-xr-x 1 root root 4096 May 20 00:00 usr drwxr-xr-x 1 root root 4096 May 20 00:00 var
nginx の設定ファイルは
/etc/nginx/nginx.conf
にありますのでこれを確認します。
root@fc4a768d19d7:/# cat /etc/nginx/nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
最後のところに
include /etc/nginx/conf.d/*.conf;
の記述があります。
この行で
/etc/nginx/conf.d/default.conf;/
を読み込んでいる(include)ことが分かります。
引き続き
/etc/nginx/conf.d/default.conf
の中身を見てみます。
root@fc4a768d19d7:/# cat /etc/nginx/conf.d/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; } ・・・・・・・・ }
これを見ると
server_name は localhost で
その location は
root /usr/share/nginx/html
であることが分かります。
それでは、この中にある index.html の内容を見てみましょう。
root@fc4a768d19d7:/# cat /usr/share/nginx/html/index.html <!doctype html> <html lang="ja"> <head> <meta charset="utf-8"> <title>ドキュメント</title> </head> <body> <p>ハロー!<br> Docker nginx <br> docker-compose.yml で起動しました。 </p> </body> </html>
あら不思議
Docker-compose で Docker nginx 構築
の中の「Docker nginx 動作確認用ファイル作成」で作って
\\wsl.localhost\Ubuntu-24.04\home\yamada\doc-nginx-c\htdocs
に置いた index.html ファイルと全く同じ内容です。
上手くできていますね。
bash シェルを開いて nginx コンテナの構成確認-Ⅱ
bash シェルを開いて doc-nginx コンテナの構成確認
の中の「doc-nginx コンテナの構成確認」で
docker run -it --rm *** /bin/bash
のコマンドでコンテナ構成の確認をする方法を紹介しました。
*** はイメージ名になりますので、ここでは、nginx を入れると
docker compose exec web bash
と同じ動作をさせることができます。
yamada@yama:~/doc-nginx-c$ docker run -it --rm nginx /bin/bash
大差はありません試してみてください。
ただし Docker Compose を使用せず Dockerfile を使用してコンテナを作成した場合は
docker compose exec *** bash
は使用できません。
注意してください。
ここまでで Docker Compose を使用して Docker Nginx を構築し
そのファイル構成も確認することができました。