labunix's blog

labunixのラボUnix

Raspberry Pi 4B(Ubuntu 20.04)のdockerでnginxを試してみる。

■Raspberry Pi 4B(Ubuntu 20.04)のdockerでnginxを試してみる。

$ lsb_release -d;uname -r -m
Description:	Ubuntu 20.04.1 LTS
5.4.0-1022-raspi aarch64

$ docker --version
Docker version 19.03.8, build afacb8b7f0

■公式のnginxイメージを検索

$ docker search -f is-official=true nginx
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   13790               [OK]                

■nginx-sampleという名前で、ホスト側ポート8080で待ち受けるように、
 デタッチモードでdockerを起動

$ docker run --name nginx-sample -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
29ade854e0dc: Pull complete 
d378f052820a: Pull complete 
7c9f4f539072: Pull complete 
474d8c2a0e68: Pull complete 
bc6c6b0ca885: Pull complete 
Digest: sha256:c3a1592d2b6d275bef4087573355827b200b00ffc2d9849890a4f3aa2128c4ae
Status: Downloaded newer image for nginx:latest
096b1833e139196496a444fa1674433cbcce657334649daa50fdb6d6dec67c95

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f1a884f3b57c        5 days ago          126MB

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
096b1833e139        nginx               "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   nginx-sample

$ ss -tln | grep :8080
LISTEN  0        4096                   *:8080                 *:*      

$ w3m -dump http://127.0.0.1:8080/
Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

■「-v ${HTTP_PATH}:${MOUNT_POINT}:ro」を使って、読み取り専用で、
 ホスト上の静的Webコンテンツを返すようにする。

$ mkdir ~/public-html
$ cd ~/public-html
$ wget http://127.0.0.1:8080/
$ sed -i -e 's/\(Welcome to \)\(nginx\)/\1local \2/' index.html 
$ cd ~/

$ docker stop nginx-sample
nginx-sample

$ docker rm $(docker ps -a | awk '/nginx-sample/{print $1}')

$ docker run --name nginx-sample -v ~/public-html:/usr/share/nginx/html:ro -d -p 8080:80 nginx
8c1dbcebea522d0d5412283b9dbaba664858e0228b0df805ab14e424fc6a605f

$ w3m -dump http://127.0.0.1:8080/
Welcome to local nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 ■お片付け

$ docker stop $(docker ps | awk '/nginx-sample/{print $1}')

$ docker rm $(docker ps -a | awk '/nginx-sample/{print $1}')
8c1dbcebea52

$ docker rmi $(docker image ls | awk '$1 ~ /nginx/&& $2 ~ /latest/{print $3}')
Untagged: nginx:latest
Untagged: nginx@sha256:c3a1592d2b6d275bef4087573355827b200b00ffc2d9849890a4f3aa2128c4ae
Deleted: sha256:f1a884f3b57c84547a5f233dc36e2a081391195a808541d546889278715c9131
Deleted: sha256:e99c2784cd6f5f8d3536413fad2df76198b19cc377b51137a36a67a0d330b2cf
Deleted: sha256:5e3e3c7fa73bd4990af646d786e2fa1654248bab37e992a9c8db0e202dbdad7b
Deleted: sha256:8015d170aac60bd1bfee01a4cd4d8bdf8ff40c1684d3a47520eb141cf0755246
Deleted: sha256:c6751df78066e9f4dbe38b9cb0deeecba5ddd1b9c7310b074e7c674dc2a4991a
Deleted: sha256:61a114ac0af584e165fb35c0b1ac76bff706506bd00bcb8481ee7cc1339cf7ec

$ rm -fr ~/public-html/