Windows11 WSL2 Ubuntu 24.04 LTS Docker

bash シェルを開いて doc-nginx コンテナの構成確認

ここまでで Dockerfile を使用して Docker Nginx を構築し これを使用して Web サイトを表示することができました。
引き続き、bash シェルを開いて doc-nginx コンテナのファイル構成を確認していきます。
CMD 命令は、コンテナーが Docker イメージから起動されたときに実行する既定のコマンドを指定します。
コンテナの起動時に(つまり、 docker run コマンドで)コマンドが指定されていない場合、 このデフォルトが使用されます。
CMD は docker run にコマンドライン引数を指定することでオーバーライドされます。
今回の場合、デフォルトでは、nginx サーバーを起動するようにしていますが、 ユーザーはこれをオーバーライドしてシェルを実行できます。
すなわち、
CMD [ "nginx", "-g", "daemon off;" ]
となっているところをユーザーは、nginx を起動する代わりに


yamada@yama:~/doc-nginx$ docker run -it --rm doc-nginx /bin/bash

でコンテナを起動して bash シェルを取得できます。
doc-nginx はイメージ名です。
ちょっとオプションの説明をします。

  1. オプション -i

    ホストの入力をコンテナの標準出力につなげます。
    i は interactive の略。
    対話するようにシェルなどでコンテナ内で操作ができます。
    -i を指定することで、コンテナにアタッチしていない状態でもSTDIN(標準入力)をオープンにし続け、 標準入力を受付ける状態にします。

  2. オプション -t

    疑似ターミナル(pseudo-TTY)を割り当てます。
    -t は tty の略。
    この割り当てがないとSTDIN(標準入力)に対して内容を入力できません。

  3. オプション -it

    -it は疑似 TTY(pseudo-TTY)をコンテナの標準入力に接続するよう Docker に対して命令します。

  4. オプション --rm

    コンテナ内でのコマンドの実行が終わったらコンテナを自動で削除します。

bash というのは対話式のコマンドで bash という名前のシェルを立ち上げます。
bash を使うと新たな画面が開いてコンテナ内の操作をすることができます。
コンテナ nginx のファイル構成を bash を使用して確認することができます。
そして、bash を実行した後は exit コマンドで bash から出ることができます。

doc-nginx コンテナの構成確認

開いた bash シェルを使用して doc-nginx コンテナがどのようなファイル構成になっているか確認しましょう。
bash シェルが立ち上がったら ls -la でファイルやディレクトリ一覧を表示してみます。

yamada@yama:~$ docker run -it --rm doc-nginx /bin/bash
[bash シェル 起動]
root@a3541b9126ac:/# ls -la
total 72
drwxr-xr-x   1 root root 4096 May 14 08:13 .
drwxr-xr-x   1 root root 4096 May 14 08:13 ..
-rwxr-xr-x   1 root root    0 May 14 08:13 .dockerenv
lrwxrwxrwx   1 root root    7 Apr 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root 4096 Mar 31  2024 bin.usr-is-merged
drwxr-xr-x   2 root root 4096 Apr 22  2024 boot
drwxr-xr-x   5 root root  360 May 14 08:13 dev
drwxr-xr-x   1 root root 4096 May 14 08:13 etc
drwxr-xr-x   3 root root 4096 Apr 15 14:11 home
lrwxrwxrwx   1 root root    7 Apr 22  2024 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Apr 22  2024 lib64 -> usr/lib64
drwxr-xr-x   2 root root 4096 Apr 15 14:04 media
drwxr-xr-x   2 root root 4096 Apr 15 14:04 mnt
drwxr-xr-x   2 root root 4096 Apr 15 14:04 opt
dr-xr-xr-x 376 root root    0 May 14 08:13 proc
drwx------   2 root root 4096 Apr 15 14:11 root
drwxr-xr-x   4 root root 4096 Apr 15 14:11 run
lrwxrwxrwx   1 root root    8 Apr 22  2024 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Mar 31  2024 sbin.usr-is-merged
drwxr-xr-x   2 root root 4096 Apr 15 14:04 srv
dr-xr-xr-x  11 root root    0 May 14 08:13 sys
drwxrwxrwt   1 root root 4096 May  6 06:11 tmp
drwxr-xr-x   1 root root 4096 Apr 15 14:04 usr
drwxr-xr-x   1 root root 4096 May  6 06:11 var

nginx の設定ファイルは
/etc/nginx/nginx.conf
にありますのでこれを確認します。

root@a3541b9126ac:~# cat /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
・・・ 中略 ・・・
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

最後のところに
include /etc/nginx/sites_enabled/*;
の記述があります。
この行で
/etc/nginx/sites_enabled/default
を読み込んでいる(include)ことが分かります。
引き続き
/etc/nginx/sites_enabled/default
の中身を見てみます。

root@a3541b9126ac:~# cat /etc/nginx/sites_enabled/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
・・・・・・・・
# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }
・・・・・・・・

root /var/www/html;
の記述が見つかります。
これが Ngnix の DocumentRoot ディレクトリのデフォルト値であることが確認できました。
bash シェルの使い方がわかったところでこれは閉じておきます。
root@a3541b9126ac:~# exit
yamada@yama:~$
--rm で bash シェルコンテナを起動しましたので、このコンテナは自動的に削除されます。

doc-nginx イメージ削除

おまけで doc-nginx イメージの削除方法も書いておきます。
まずはイメージの状況確認をします。

yamada@yama:~/doc-nginx$ docker images
[結果]
REPOSITORY  TAG     IMAGE ID      CREATED             SIZE
doc-nginx   latest  5cb328430095  About a minute ago  135MB

とし、この IMAGE ID を使い
yamada@yama:~/doc-nginx$ docker image rm 5cb328430095
でイメージ削除ができます。

ここまでで Dockerfile を使用して Docker Nginx を構築し そのファイル構成も確認することができました。
引き続き Docker-compose を使用して Docker Nginx を構築して行きます。


  • Docker-compose で Docker nginx 構築 に進む
  • Docker nginx Dockerfile ファイル作成 Ⅱ に戻る
  • Dockerfile で Docker nginx 構築 に戻る
  • Docker Compose ファイル実行 に戻る
  • Docker 使用 Web サイト表示 に戻る
  • Docker コンテナ チュートリアルなぜ動く に戻る
  • Docker コンテナ チュートリアル に戻る
  • Docker CE インストール概要 に戻る
  • 70VPS に戻る