nginxコンテナを使ってみる①（コンテナ起動）

単なる作業メモ。
dockerインストール～nginxコンテナ起動まで。

nginxを使ってみたかったので
書籍（[Nginx実践ガイド](https://www.amazon.co.jp/dp/B01N183E3H/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1)）に書かれている内容を実際にやってみる。

書籍はCentOS上にyumでnginxをインストールしているが
今回はEC2上にdockerをインストールしてnginxコンテナで動かす。

■EC2起動
何でも良かったので無料のt2.micro、AmazonLinux2でインスタンス起動。
セキュリティーグループはSSH(22)とHTTP(80)のみ、SourceIPは自PCに絞っておく。

■EC2インスタンスのSSH接続
SSHクライアント(今回はTeraterm)でインスタンスにログイン

■dockerインストール

```sh:dockerインストール
[ec2-user]$ sudo amazon-linux-extras install docker
Installing docker
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker
10 metadata files removed
4 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                 | 3.7 kB  00:00:00
amzn2extra-docker                                                          | 3.0 kB  00:00:00
(1/5): amzn2-core/2/x86_64/group_gz                                        | 2.5 kB  00:00:00
(2/5): amzn2-core/2/x86_64/updateinfo                                      | 223 kB  00:00:00
(3/5): amzn2extra-docker/2/x86_64/updateinfo                               |   69 B  00:00:00
(4/5): amzn2extra-docker/2/x86_64/primary_db                               |  68 kB  00:00:00
(5/5): amzn2-core/2/x86_64/primary_db                                      |  42 MB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:19.03.6ce-4.amzn2 will be installed
--> Processing Dependency: runc >= 1.0.0 for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: containerd >= 1.3.2 for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: pigz for package: docker-19.03.6ce-4.amzn2.x86_64
--> Processing Dependency: libcgroup for package: docker-19.03.6ce-4.amzn2.x86_64
--> Running transaction check
---> Package containerd.x86_64 0:1.3.2-1.amzn2 will be installed
---> Package libcgroup.x86_64 0:0.41-21.amzn2 will be installed
---> Package pigz.x86_64 0:2.3.4-1.amzn2.0.1 will be installed
---> Package runc.x86_64 0:1.0.0-0.1.20200204.gitdc9208a.amzn2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================
 Package        Arch       Version                                    Repository             Size
==================================================================================================
Installing:
 docker         x86_64     19.03.6ce-4.amzn2                          amzn2extra-docker      37 M
Installing for dependencies:
 containerd     x86_64     1.3.2-1.amzn2                              amzn2extra-docker      24 M
 libcgroup      x86_64     0.41-21.amzn2                              amzn2-core             66 k
 pigz           x86_64     2.3.4-1.amzn2.0.1                          amzn2-core             81 k
 runc           x86_64     1.0.0-0.1.20200204.gitdc9208a.amzn2        amzn2extra-docker     2.6 M

Transaction Summary
==================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 65 M
Installed size: 271 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): libcgroup-0.41-21.amzn2.x86_64.rpm                                  |  66 kB  00:00:00
(2/5): pigz-2.3.4-1.amzn2.0.1.x86_64.rpm                                   |  81 kB  00:00:00
(3/5): containerd-1.3.2-1.amzn2.x86_64.rpm                                 |  24 MB  00:00:00
(4/5): docker-19.03.6ce-4.amzn2.x86_64.rpm                                 |  37 MB  00:00:00
(5/5): runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64.rpm                 | 2.6 MB  00:00:00
--------------------------------------------------------------------------------------------------
Total                                                              69 MB/s |  65 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64                                1/5
  Installing : containerd-1.3.2-1.amzn2.x86_64                                                2/5
  Installing : libcgroup-0.41-21.amzn2.x86_64                                                 3/5
  Installing : pigz-2.3.4-1.amzn2.0.1.x86_64                                                  4/5
  Installing : docker-19.03.6ce-4.amzn2.x86_64                                                5/5
  Verifying  : docker-19.03.6ce-4.amzn2.x86_64                                                1/5
  Verifying  : containerd-1.3.2-1.amzn2.x86_64                                                2/5
  Verifying  : pigz-2.3.4-1.amzn2.0.1.x86_64                                                  3/5
  Verifying  : runc-1.0.0-0.1.20200204.gitdc9208a.amzn2.x86_64                                4/5
  Verifying  : libcgroup-0.41-21.amzn2.x86_64                                                 5/5

Installed:
  docker.x86_64 0:19.03.6ce-4.amzn2

Dependency Installed:
  containerd.x86_64 0:1.3.2-1.amzn2       libcgroup.x86_64 0:0.41-21.amzn2
  pigz.x86_64 0:2.3.4-1.amzn2.0.1         runc.x86_64 0:1.0.0-0.1.20200204.gitdc9208a.amzn2

Complete!
  0  ansible2                 available    \
        [ =2.4.2  =2.4.6  =2.8  =stable ]
  2  httpd_modules            available    [ =1.0  =stable ]
  3  memcached1.5             available    \
        [ =1.5.1  =1.5.16  =1.5.17 ]
  5  postgresql9.6            available    \
        [ =9.6.6  =9.6.8  =stable ]
  6  postgresql10             available    [ =10  =stable ]
  8  redis4.0                 available    \
        [ =4.0.5  =4.0.10  =stable ]
  9  R3.4                     available    [ =3.4.3  =stable ]
 10  rust1                    available    \
        [ =1.22.1  =1.26.0  =1.26.1  =1.27.2  =1.31.0  =1.38.0
          =stable ]
 11  vim                      available    [ =8.0  =stable ]
 13  ruby2.4                  available    \
        [ =2.4.2  =2.4.4  =2.4.7  =stable ]
 15  php7.2                   available    \
        [ =7.2.0  =7.2.4  =7.2.5  =7.2.8  =7.2.11  =7.2.13  =7.2.14
          =7.2.16  =7.2.17  =7.2.19  =7.2.21  =7.2.22  =7.2.23
          =7.2.24  =7.2.26  =stable ]
 17  lamp-mariadb10.2-php7.2  available    \
        [ =10.2.10_7.2.0  =10.2.10_7.2.4  =10.2.10_7.2.5
          =10.2.10_7.2.8  =10.2.10_7.2.11  =10.2.10_7.2.13
          =10.2.10_7.2.14  =10.2.10_7.2.16  =10.2.10_7.2.17
          =10.2.10_7.2.19  =10.2.10_7.2.22  =10.2.10_7.2.23
          =10.2.10_7.2.24  =stable ]
 18  libreoffice              available    \
        [ =5.0.6.2_15  =5.3.6.1  =stable ]
 19  gimp                     available    [ =2.8.22 ]
 20  docker=latest            enabled      \
        [ =17.12.1  =18.03.1  =18.06.1  =18.09.9  =stable ]
 21  mate-desktop1.x          available    \
        [ =1.19.0  =1.20.0  =stable ]
 22  GraphicsMagick1.3        available    \
        [ =1.3.29  =1.3.32  =1.3.34  =stable ]
 23  tomcat8.5                available    \
        [ =8.5.31  =8.5.32  =8.5.38  =8.5.40  =8.5.42  =8.5.50
          =stable ]
 24  epel                     available    [ =7.11  =stable ]
 25  testing                  available    [ =1.0  =stable ]
 26  ecs                      available    [ =stable ]
 27  corretto8                available    \
        [ =1.8.0_192  =1.8.0_202  =1.8.0_212  =1.8.0_222  =1.8.0_232
          =1.8.0_242  =stable ]
 28  firecracker              available    [ =0.11  =stable ]
 29  golang1.11               available    \
        [ =1.11.3  =1.11.11  =1.11.13  =stable ]
 30  squid4                   available    [ =4  =stable ]
 31  php7.3                   available    \
        [ =7.3.2  =7.3.3  =7.3.4  =7.3.6  =7.3.8  =7.3.9  =7.3.10
          =7.3.11  =7.3.13  =stable ]
 32  lustre2.10               available    \
        [ =2.10.5  =2.10.8  =stable ]
 33  java-openjdk11           available    [ =11  =stable ]
 34  lynis                    available    [ =stable ]
 35  kernel-ng                available    [ =stable ]
 36  BCC                      available    [ =0.x  =stable ]
 37  mono                     available    [ =5.x  =stable ]
 38  nginx1                   available    [ =stable ]
 39  ruby2.6                  available    [ =2.6  =stable ]
 40  mock                     available    [ =stable ]
 41  postgresql11             available    [ =11  =stable ]
 42  php7.4                   available    [ =stable ]
 43  livepatch                available    [ =stable ]
 44  python3.8                available    [ =stable ]
 45  haproxy2                 available    [ =stable ]
[ec2-user]$
```

```sh:dockerグループ追加
[ec2-user]$ sudo usermod -a -G docker ec2-user
[ec2-user]$
```

```sh:サービス起動
[ec2-user]$ sudo systemctl start docker.service
[ec2-user]$
```

```sh:自動起動設定
[ec2-user]$ sudo systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[ec2-user]$
```

■nginxコンテナ起動

```sh:PULL
[ec2-user]$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Pull complete
8d69e59170f7: Pull complete
3f9f1ec1d262: Pull complete
d1f5ff4f210d: Pull complete
1e22bfa8652e: Pull complete
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[ec2-user]$
```

```sh:コンテナ起動
[ec2-user]$ docker run -d --name nginx -p 80:80 --rm nginx:latest
6775ebb4074c0c7f8ded76587c1d6f6dbbc9f4aef345add09b73ad2cb005216c
[ec2-user]$
[ec2-user]$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6775ebb4074c        nginx:latest        "/docker-entrypoint.…"   11 seconds ago      Up 11 seconds       0.0.0.0:80->80/tcp   nginx
[ec2-user]$
```

■コンテナに入ってみる

```sh:コンテナ接続
docker container exec -it nginx /bin/bash
```

■nginxのバージョン
nginxコマンドを-vオプションで打つとバージョン情報が出てくる。
-Vオプションで打つとバージョン情報に加えビルド情報等も出てくる。

```sh:nginxのバージョン確認
root@6775ebb4074c:~# nginx -V
nginx version: nginx/1.19.0
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1d  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@6775ebb4074c:~#
```

■nginxのディレクトリ構造

__/etc/nginx__
設定ファイル置き場。
ここに置かれているnginx.confが最初に読み込まれる設定ファイル。
この中からincude指定された設定ファイルが追加で読み込まれていく。
デフォルトでは以下が追加で読み込まれる。
/etc/nginx/conf.d/*.conf;
/etc/nginx/mime.types;

```sh:設定ファイル置き場
root@6775ebb4074c:~# ls -l /etc/nginx
total 36
drwxr-xr-x 1 root root   26 Jul  4 09:57 conf.d
-rw-r--r-- 1 root root 1007 May 26 15:00 fastcgi_params
-rw-r--r-- 1 root root 2837 May 26 15:00 koi-utf
-rw-r--r-- 1 root root 2223 May 26 15:00 koi-win
-rw-r--r-- 1 root root 5231 May 26 15:00 mime.types
lrwxrwxrwx 1 root root   22 May 26 15:01 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  643 May 26 15:01 nginx.conf
-rw-r--r-- 1 root root  636 May 26 15:00 scgi_params
-rw-r--r-- 1 root root  664 May 26 15:00 uwsgi_params
-rw-r--r-- 1 root root 3610 May 26 15:00 win-utf
root@6775ebb4074c:~#
root@6775ebb4074c:~#
root@6775ebb4074c:~#
root@6775ebb4074c:~# grep include /etc/nginx/nginx.conf
    include  /etc/nginx/mime.types;
    include  /etc/nginx/conf.d/*.conf;
root@6775ebb4074c:~#
```


__/usr/lib/nginx/modules__
追加モジュール置き場。書籍にはlibではなくlib64と書かれていたがこの環境ではlibだった。

```sh:追加モジュール置き場
root@6775ebb4074c:~# ls -l /usr/lib/nginx/modules/
total 2640
-rw-r--r-- 1 root root  24328 May 26 15:01 ngx_http_geoip_module-debug.so
-rw-r--r-- 1 root root  24328 May 26 15:01 ngx_http_geoip_module.so
-rw-r--r-- 1 root root  27568 May 26 15:01 ngx_http_image_filter_module-debug.so
-rw-r--r-- 1 root root  27568 May 26 15:01 ngx_http_image_filter_module.so
-rw-r--r-- 1 root root 632800 May 26 15:01 ngx_http_js_module-debug.so
-rw-r--r-- 1 root root 628704 May 26 15:01 ngx_http_js_module.so
-rw-r--r-- 1 root root  23400 May 26 15:01 ngx_http_xslt_filter_module-debug.so
-rw-r--r-- 1 root root  23400 May 26 15:01 ngx_http_xslt_filter_module.so
-rw-r--r-- 1 root root  19864 May 26 15:01 ngx_stream_geoip_module-debug.so
-rw-r--r-- 1 root root  19864 May 26 15:01 ngx_stream_geoip_module.so
-rw-r--r-- 1 root root 619232 May 26 15:01 ngx_stream_js_module-debug.so
-rw-r--r-- 1 root root 615136 May 26 15:01 ngx_stream_js_module.so
root@6775ebb4074c:~#
```

__/usr/share/nginx/html__
デフォルトのドキュメントルート。エラードキュメントもここ。
```sh:デフォルトのドキュメントルート
root@6775ebb4074c:~# ls -l /usr/share/nginx/html
total 8
-rw-r--r-- 1 root root 494 May 26 15:00 50x.html
-rw-r--r-- 1 root root 612 May 26 15:00 index.html
root@6775ebb4074c:~#
```

__/var/cache/nginxv__
キャッシュファイル置き場

```sh:キャッシュファイル置き場
root@6775ebb4074c:~# ls -l /var/cache/nginx
total 0
drwx------ 2 nginx root 6 Jul  4 09:57 client_temp
drwx------ 2 nginx root 6 Jul  4 09:57 fastcgi_temp
drwx------ 2 nginx root 6 Jul  4 09:57 proxy_temp
drwx------ 2 nginx root 6 Jul  4 09:57 scgi_temp
drwx------ 2 nginx root 6 Jul  4 09:57 uwsgi_temp
root@6775ebb4074c:~#
```

__/var/log/nginx__
ログファイル置き場。
 nginxのログはアクセスログとエラーログの2種類。
 今回はコンテナなのでそれぞれ標準出力、標準エラー出力に
 シンボリックリンクが張られている。

```sh:ログファイル置き場
root@6775ebb4074c:~# ls -l /var/log/nginx
total 0
lrwxrwxrwx 1 root root 11 Jun  9 16:57 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jun  9 16:57 error.log -> /dev/stderr
root@6775ebb4074c:~#
```

■ブラウザアクセス
今回はブラウザアクセスできることを確認して終わり。

http://**EC2インスタンスのパブリックDNS名**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/651099/20b7955f-978d-5ac7-ec43-10d06acf5af2.png)


無事表示されたので今日はここまで。