そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

Docker基本コマンド(Dockerfile/CentOS・ApacheのImage作成/コンテナ作成/ホストとファイル共有/bridge作成)

・Dockerの基本コマンドを一通り知りたい。
・具体的な手順を教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. Dockerfileの準備
  2. Dockerfileを使ってImage作成
  3. Imageの一覧表示
  4. Dockerコンテナの作成
  5. コンテナの一覧表示
  6. コンテナの停止
  7. 名前を指定してコンテナの作成
  8. ブラウザでの表示確認
  9. ネットワークの一覧表示
  10. ネットワークの詳細表示
  11. コンテナの詳細情報を表示
  12. テスト用のブリッジの作成
  13. ブリッジをアタッチしてコンテナを作成
  14. ホスト側とコンテナ側のファイル共有(/var/lib/docker/volumes/を使用するパターン)
  15. ホスト側とコンテナ側のファイル共有(bindmountで任意のディレクトリを使用するパターン)
  16. コンテナの停止、削除
  17. Imageの削除
  18. テスト用のbridgeの削除

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

Dockerの基本コマンドを一通り使ってみる

前回の記事

souiunogaii.hatenablog.com

souiunogaii.hatenablog.com

その①:Dockerfileの準備

DockerのImage作成には、「Dockerfile」というファイルを準備しておく必要があります。

今回は、~/dockerというディレクトリの配下にdockerfileという名前のファイルと、テスト用のindex.htmlというファイルを作成しておきます。

$ mkdir ~/docker
$ cd ~/docker
vi ~/docker/dockerfile
FROM centos:latest

RUN dnf install -y httpd

COPY ./index.html /var/www/html/index.html

EXPOSE 80

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
コマンド 意味
FROM centos:latest https://hub.docker.com/_/centosからCentOSの最新版を使います
RUN dnf install -y httpd Apacheをインストール
COPY ./index.html /var/www/html/index.html カレントディレクトリのindex.htmlをコンテナ上の/var/www/htmlにコピーする
EXPOSE 80 80番ポートを開放します
CMD /usr/sbin/httpd Apacheデーモンをフォアグラウンドで実行
vi ~/docker/index.html
hello world

その②:Dockerfileを使ってImage作成

sudo docker image build -t dockerdemo ./

カレントディレクトリにdockerfileというファイル名のファイルがあると、それを読み込んでImageを作成します。

オプション 意味
-t Imageにつけるタグ
$ sudo docker image build -t dockerdemo ./
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM centos:latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
 ---> 300e315adb2f
Step 2/5 : RUN dnf install -y httpd
 ---> Running in e5b4e81ae1be
CentOS Linux 8 - AppStream                      4.4 MB/s | 6.3 MB     00:01
CentOS Linux 8 - BaseOS                         1.4 MB/s | 2.3 MB     00:01
CentOS Linux 8 - Extras                         4.0 kB/s | 8.6 kB     00:02
Dependencies resolved.
================================================================================
 Package           Arch   Version                               Repo       Size
================================================================================
Installing:
 httpd             x86_64 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream 1.7 M
Installing dependencies:
 apr               x86_64 1.6.3-11.el8                          appstream 125 k
 apr-util          x86_64 1.6.1-6.el8                           appstream 105 k
 brotli            x86_64 1.0.6-2.el8                           baseos    322 k
 centos-logos-httpd
                   noarch 80.5-2.el8                            baseos     24 k
 httpd-filesystem  noarch 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream  37 k
 httpd-tools       x86_64 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream 104 k
 mailcap           noarch 2.1.48-3.el8                          baseos     39 k
 mod_http2         x86_64 1.15.7-2.module_el8.3.0+477+498bb568  appstream 154 k
Installing weak dependencies:
 apr-util-bdb      x86_64 1.6.1-6.el8                           appstream  25 k
 apr-util-openssl  x86_64 1.6.1-6.el8                           appstream  27 k
Enabling module streams:
 httpd                    2.4

Transaction Summary
================================================================================
Install  11 Packages

Total download size: 2.6 M
Installed size: 7.5 M
Downloading Packages:
(1/11): apr-util-bdb-1.6.1-6.el8.x86_64.rpm     557 kB/s |  25 kB     00:00
(2/11): apr-util-openssl-1.6.1-6.el8.x86_64.rpm 1.9 MB/s |  27 kB     00:00
(3/11): apr-1.6.3-11.el8.x86_64.rpm             1.8 MB/s | 125 kB     00:00
(4/11): apr-util-1.6.1-6.el8.x86_64.rpm         1.5 MB/s | 105 kB     00:00
(5/11): httpd-filesystem-2.4.37-30.module_el8.3 2.6 MB/s |  37 kB     00:00
(6/11): httpd-tools-2.4.37-30.module_el8.3.0+56 2.5 MB/s | 104 kB     00:00
(7/11): mod_http2-1.15.7-2.module_el8.3.0+477+4 3.5 MB/s | 154 kB     00:00
(8/11): centos-logos-httpd-80.5-2.el8.noarch.rp 390 kB/s |  24 kB     00:00
(9/11): mailcap-2.1.48-3.el8.noarch.rpm         734 kB/s |  39 kB     00:00
(10/11): brotli-1.0.6-2.el8.x86_64.rpm          2.0 MB/s | 322 kB     00:00
(11/11): httpd-2.4.37-30.module_el8.3.0+561+97f 6.6 MB/s | 1.7 MB     00:00
--------------------------------------------------------------------------------
Total                                           2.3 MB/s | 2.6 MB     00:01
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/apr-1.6.3-11.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                       92 kB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : apr-1.6.3-11.el8.x86_64                               1/11
  Running scriptlet: apr-1.6.3-11.el8.x86_64                               1/11
  Installing       : apr-util-bdb-1.6.1-6.el8.x86_64                       2/11
  Installing       : apr-util-openssl-1.6.1-6.el8.x86_64                   3/11
  Installing       : apr-util-1.6.1-6.el8.x86_64                           4/11
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                           4/11
  Installing       : httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.    5/11
  Installing       : mailcap-2.1.48-3.el8.noarch                           6/11
  Installing       : centos-logos-httpd-80.5-2.el8.noarch                  7/11
  Installing       : brotli-1.0.6-2.el8.x86_64                             8/11
  Running scriptlet: httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fd    9/11
  Installing       : httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fd    9/11
  Installing       : mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86   10/11
  Installing       : httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64   11/11
  Running scriptlet: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64   11/11
  Verifying        : apr-1.6.3-11.el8.x86_64                               1/11
  Verifying        : apr-util-1.6.1-6.el8.x86_64                           2/11
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                       3/11
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                   4/11
  Verifying        : httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64    5/11
  Verifying        : httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fd    6/11
  Verifying        : httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.    7/11
  Verifying        : mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86    8/11
  Verifying        : brotli-1.0.6-2.el8.x86_64                             9/11
  Verifying        : centos-logos-httpd-80.5-2.el8.noarch                 10/11
  Verifying        : mailcap-2.1.48-3.el8.noarch                          11/11

Installed:
  apr-1.6.3-11.el8.x86_64
  apr-util-1.6.1-6.el8.x86_64
  apr-util-bdb-1.6.1-6.el8.x86_64
  apr-util-openssl-1.6.1-6.el8.x86_64
  brotli-1.0.6-2.el8.x86_64
  centos-logos-httpd-80.5-2.el8.noarch
  httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64
  httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch
  httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64
  mailcap-2.1.48-3.el8.noarch
  mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64

Complete!
Removing intermediate container e5b4e81ae1be
 ---> 0186eb487e50
Step 3/5 : COPY ./index.html /var/www/html/index.html
 ---> 4c1442296884
Step 4/5 : EXPOSE 80
 ---> Running in b3febceb391a
Removing intermediate container b3febceb391a
 ---> c5d05b1b2686
Step 5/5 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
 ---> Running in 9f4d7e3f6dba
Removing intermediate container 9f4d7e3f6dba
 ---> 2be8de742fc5
Successfully built 2be8de742fc5
Successfully tagged dockerdemo:latest

その②:Dockerfileを編集して再度、Imageを作成

~/docker/dockerfile

80番ポートに加えて、443番ポートも開放します。

FROM centos:latest

RUN dnf install -y httpd

COPY ./index.html /var/www/html/index.html

EXPOSE 80 443

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

今度はdockerdemo2というタグをつけてImageを作成します。

sudo docker image build -t dockerdemo2 ./
$ sudo docker image build -t dockerdemo2 ./
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM centos:latest
 ---> 300e315adb2f
Step 2/5 : RUN dnf install -y httpd
 ---> Using cache
 ---> 0186eb487e50
Step 3/5 : COPY ./index.html /var/www/html/index.html
 ---> Using cache
 ---> 4c1442296884
Step 4/5 : EXPOSE 80 443
 ---> Running in e380239f3637
Removing intermediate container e380239f3637
 ---> 9ba8622d24f2
Step 5/5 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
 ---> Running in 8e972e73994b
Removing intermediate container 8e972e73994b
 ---> 1be9c854d312
Successfully built 1be9c854d312
Successfully tagged dockerdemo2:latest

2回目なので、Using cacheと表示されていて、1回目よりもずいぶん早く作成できました。

その③:Imageの一覧表示

sudo docker image ls
$ sudo docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
dockerdemo2   latest    1be9c854d312   52 seconds ago   250MB
dockerdemo    latest    2be8de742fc5   3 minutes ago    250MB
centos        latest    300e315adb2f   4 weeks ago      209MB

その④:Dockerコンテナの作成

sudo docker run -d -p 8080:80 dockerdemo:latest
オプション 意味
-d バックグラウンドでコンテナを起動する(デタッチモード)
-p 左がホスト側のポート番号、右側がコンテナ側のポート番号
$ sudo docker run -d -p 8080:80 dockerdemo:latest
5f92ae353f2c99e0713f834106016b70eb836afd55a21a48aebdec0156675139

その⑤:コンテナの一覧表示

sudo docker container ls
$ sudo docker container ls
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                  NAMES
5f92ae353f2c   dockerdemo:latest   "/usr/sbin/httpd -D …"   32 seconds ago   Up 29 seconds   0.0.0.0:8080->80/tcp   objective_hoover

NAMESのところには、作成時に指定していなければ、ランダムで自動で名前が付与されます。

その⑥:コンテナの停止

sudo docker container stop 5f92ae353f2c
$ sudo docker container stop 5f92ae353f2c
5f92ae353f2c

その⑦:名前を指定してコンテナの作成

sudo docker run --name dockerdemo -d -p 8080:80 dockerdemo:latest
オプション 意味
--name コンテナにつける名前
$ sudo docker run --name dockerdemo -d -p 8080:80 dockerdemo:latest
99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb
$ sudo docker container ls
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                  NAMES
99637d49c0ae   dockerdemo:latest   "/usr/sbin/httpd -D …"   15 seconds ago   Up 14 seconds   0.0.0.0:8080->80/tcp   dockerdemo

その⑧:ブラウザでの表示確認

http://sv-titan.planet.space.com:8080/

その⑨:ネットワークの一覧表示

sudo docker network ls
$ sudo docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
1b61214ad6db   bridge    bridge    local
8bc049d2b352   host      host      local
5112d005e007   none      null      local

その⑩:ネットワークの詳細表示

sudo docker network inspect bridge
$ sudo docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "1b61214ad6db146dbb7e8ebbc86492f08a1a0ec73da2bef2c1b89dabc2caa915",
        "Created": "2021-01-11T11:27:04.280067669+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb": {
                "Name": "dockerdemo",
                "EndpointID": "e8801880b8593f4598cb0d49eb22522c2e0e388e1d03a5f32c61194daa2c7684",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

その⑪:コンテナの詳細情報を表示

 sudo docker container inspect 99637d49c0ae
$ sudo docker container inspect 99637d49c0ae
[
    {
        "Id": "99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb",
        "Created": "2021-01-11T05:28:12.758734751Z",
        "Path": "/usr/sbin/httpd",
        "Args": [
            "-D",
            "FOREGROUND"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 8493,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-11T05:28:13.834893578Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:2be8de742fc5c02b845a0128f4bca9054f864aeabcace3a3a5a3ca66732e2580",
        "ResolvConfPath": "/var/lib/docker/containers/99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb/hostname",
        "HostsPath": "/var/lib/docker/containers/99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb/hosts",
        "LogPath": "/var/lib/docker/containers/99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb/99637d49c0ae63a997964f32f3f4f3f57a484a2b77edff01d6e458d00696eceb-json.log",
        "Name": "/dockerdemo",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0403794c7ee6c25446e3d1577d7d2390f31b5e4a572514a16037ad4f060f19c7-init/diff:/var/lib/docker/overlay2/c94f973f7b422b890a09114bd8f3d32b667f9cad2ecd9191cd0175fab28b42ed/diff:/var/lib/docker/overlay2/d2a192c705f05a39525fdede8327cc93746901e058d50e6a9c74f2e320a0f4cd/diff:/var/lib/docker/overlay2/cb664e0b702bed0030aa87bd1ab385deab6412ffde496acce25760cee221f24f/diff",
                "MergedDir": "/var/lib/docker/overlay2/0403794c7ee6c25446e3d1577d7d2390f31b5e4a572514a16037ad4f060f19c7/merged",
                "UpperDir": "/var/lib/docker/overlay2/0403794c7ee6c25446e3d1577d7d2390f31b5e4a572514a16037ad4f060f19c7/diff",
                "WorkDir": "/var/lib/docker/overlay2/0403794c7ee6c25446e3d1577d7d2390f31b5e4a572514a16037ad4f060f19c7/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "99637d49c0ae",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/usr/sbin/httpd",
                "-D",
                "FOREGROUND"
            ],
            "Image": "dockerdemo:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d311cfb67b53ded43ef5fad9e9bc6e59a360bc8fa278dc406396d934de2e0f8b",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8080"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/d311cfb67b53",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "e8801880b8593f4598cb0d49eb22522c2e0e388e1d03a5f32c61194daa2c7684",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1b61214ad6db146dbb7e8ebbc86492f08a1a0ec73da2bef2c1b89dabc2caa915",
                    "EndpointID": "e8801880b8593f4598cb0d49eb22522c2e0e388e1d03a5f32c61194daa2c7684",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

今回のコンテナ環境のネットワークは以下の図のようになっていることが分かりました。

その⑫:テスト用のブリッジの作成

sudo docker network create --attachable -d bridge --subnet=10.1.0.0/16 testbridge
$ sudo docker network create --attachable -d bridge --subnet=10.1.0.0/16 testbridge
4055a6e63dc3d39c51ddf7fe2af25c6c6ced47747c07813035371d8832096ce8
オプション 意味
--attachable 手動でコンテナにアタッチ可能
-d ネットワークのドライバのタイプ(今回はbridgeを指定)
--subnet サブネットのアドレス
$ sudo docker network ls
NETWORK ID     NAME         DRIVER    SCOPE
1b61214ad6db   bridge       bridge    local
8bc049d2b352   host         host      local
5112d005e007   none         null      local
4055a6e63dc3   testbridge   bridge    local
$ sudo docker network inspect testbridge
[
    {
        "Name": "testbridge",
        "Id": "4055a6e63dc3d39c51ddf7fe2af25c6c6ced47747c07813035371d8832096ce8",
        "Created": "2021-01-11T14:36:23.140223023+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.1.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

その⑬:ブリッジをアタッチしてコンテナを作成

sudo docker run --name dockerdemo2 -d -p 8081:80 --net testbridge dockerde

mo:latest

$ sudo docker run --name dockerdemo2 -d -p 8081:80 --net testbridge dockerde
mo:latest
3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71
$ sudo docker container ls
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                  NAMES
3aecce46fb85   dockerdemo:latest   "/usr/sbin/httpd -D …"   25 seconds ago   Up 21 seconds   0.0.0.0:8081->80/tcp   dockerdemo2
99637d49c0ae   dockerdemo:latest   "/usr/sbin/httpd -D …"   10 minutes ago   Up 10 minutes   0.0.0.0:8080->80/tcp   dockerdemo
$ sudo docker container inspect 3aecce46fb85
[
    {
        "Id": "3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71",
        "Created": "2021-01-11T05:38:20.638269023Z",
        "Path": "/usr/sbin/httpd",
        "Args": [
            "-D",
            "FOREGROUND"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 8973,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-11T05:38:24.217697912Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:2be8de742fc5c02b845a0128f4bca9054f864aeabcace3a3a5a3ca66732e2580",
        "ResolvConfPath": "/var/lib/docker/containers/3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71/hostname",
        "HostsPath": "/var/lib/docker/containers/3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71/hosts",
        "LogPath": "/var/lib/docker/containers/3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71/3aecce46fb8508df50a59a6f922fdacadb8824e61298d604fc12b33d1e402c71-json.log",
        "Name": "/dockerdemo2",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "testbridge",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8081"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/3ae677710879b563c1fbe0067899ee587fba717b29b8527d2270e9737d6ef25b-init/diff:/var/lib/docker/overlay2/c94f973f7b422b890a09114bd8f3d32b667f9cad2ecd9191cd0175fab28b42ed/diff:/var/lib/docker/overlay2/d2a192c705f05a39525fdede8327cc93746901e058d50e6a9c74f2e320a0f4cd/diff:/var/lib/docker/overlay2/cb664e0b702bed0030aa87bd1ab385deab6412ffde496acce25760cee221f24f/diff",
                "MergedDir": "/var/lib/docker/overlay2/3ae677710879b563c1fbe0067899ee587fba717b29b8527d2270e9737d6ef25b/merged",
                "UpperDir": "/var/lib/docker/overlay2/3ae677710879b563c1fbe0067899ee587fba717b29b8527d2270e9737d6ef25b/diff",
                "WorkDir": "/var/lib/docker/overlay2/3ae677710879b563c1fbe0067899ee587fba717b29b8527d2270e9737d6ef25b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3aecce46fb85",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/usr/sbin/httpd",
                "-D",
                "FOREGROUND"
            ],
            "Image": "dockerdemo:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a64d00820dcdf9fe987e98135879048829250c6979939a55fe5caf0099ee0403",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8081"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/a64d00820dcd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "testbridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "3aecce46fb85"
                    ],
                    "NetworkID": "4055a6e63dc3d39c51ddf7fe2af25c6c6ced47747c07813035371d8832096ce8",
                    "EndpointID": "8c305f27fa76b06e4e89f2f6739095649a740c6ff1046913fafe5ae8d020d891",
                    "Gateway": "10.1.0.1",
                    "IPAddress": "10.1.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:01:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

以下のようになりました。

その⑭:ホスト側とコンテナ側のファイル共有(/var/lib/docker/volumes/を使用するパターン)

dockerfileを編集

dockerfileにVOLUME /var/www/htmlを追記しました。

FROM centos:latest

RUN dnf install -y httpd

COPY ./index.html /var/www/html/index.html

VOLUME /var/www/html

EXPOSE 80 443

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

編集したいdockerfileからImageを作成

$ sudo docker build -t volumedemo ./
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM centos:latest
 ---> 300e315adb2f
Step 2/6 : RUN dnf install -y httpd
 ---> Using cache
 ---> 0186eb487e50
Step 3/6 : COPY ./index.html /var/www/html/index.html
 ---> Using cache
 ---> 4c1442296884
Step 4/6 : VOLUME /var/www/html
 ---> Running in 9fbd10fb4e72
Removing intermediate container 9fbd10fb4e72
 ---> 5c28b9ae6f75
Step 5/6 : EXPOSE 80 443
 ---> Running in d1ee3a4d3c81
Removing intermediate container d1ee3a4d3c81
 ---> 013710fca81e
Step 6/6 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
 ---> Running in 5537e1a297a5
Removing intermediate container 5537e1a297a5
 ---> 9f459b8b4545
Successfully built 9f459b8b4545
Successfully tagged volumedemo:latest
$ sudo docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
volumedemo    latest    9f459b8b4545   16 seconds ago   250MB
dockerdemo2   latest    1be9c854d312   25 minutes ago   250MB
dockerdemo    latest    2be8de742fc5   28 minutes ago   250MB
centos        latest    300e315adb2f   4 weeks ago      209MB

Imageからコンテナ作成

sudo docker run --name volumedemo1 -d -p 8083:80 volumedemo:latest
$ sudo docker run --name volumedemo1 -d -p 8083:80 volumedemo:latest
e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f
$ sudo docker container ls
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                           NAMES
e93e2b54fbc3   volumedemo:latest   "/usr/sbin/httpd -D …"   24 seconds ago   Up 17 seconds   443/tcp, 0.0.0.0:8083->80/tcp   volumedemo1
3aecce46fb85   dockerdemo:latest   "/usr/sbin/httpd -D …"   12 minutes ago   Up 12 minutes   0.0.0.0:8081->80/tcp            dockerdemo2
99637d49c0ae   dockerdemo:latest   "/usr/sbin/httpd -D …"   22 minutes ago   Up 22 minutes   0.0.0.0:8080->80/tcp            dockerdemo
$ sudo docker container inspect e93e2b54fbc3
[
    {
        "Id": "e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f",
        "Created": "2021-01-11T05:50:21.910641551Z",
        "Path": "/usr/sbin/httpd",
        "Args": [
            "-D",
            "FOREGROUND"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9691,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-11T05:50:28.080642322Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:9f459b8b45455074d00821d81ce5848ebfb5458717f80b15d148b686d91a1743",
        "ResolvConfPath": "/var/lib/docker/containers/e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f/hostname",
        "HostsPath": "/var/lib/docker/containers/e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f/hosts",
        "LogPath": "/var/lib/docker/containers/e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f/e93e2b54fbc3ed2c41c7deb7228d68c25a2c480ed5041886741b5be87755bd1f-json.log",
        "Name": "/volumedemo1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8083"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/bd952ca7ae43985a2ee3dfb633f233645274069ee28c49f525df9007af0d129b-init/diff:/var/lib/docker/overlay2/c94f973f7b422b890a09114bd8f3d32b667f9cad2ecd9191cd0175fab28b42ed/diff:/var/lib/docker/overlay2/d2a192c705f05a39525fdede8327cc93746901e058d50e6a9c74f2e320a0f4cd/diff:/var/lib/docker/overlay2/cb664e0b702bed0030aa87bd1ab385deab6412ffde496acce25760cee221f24f/diff",
                "MergedDir": "/var/lib/docker/overlay2/bd952ca7ae43985a2ee3dfb633f233645274069ee28c49f525df9007af0d129b/merged",
                "UpperDir": "/var/lib/docker/overlay2/bd952ca7ae43985a2ee3dfb633f233645274069ee28c49f525df9007af0d129b/diff",
                "WorkDir": "/var/lib/docker/overlay2/bd952ca7ae43985a2ee3dfb633f233645274069ee28c49f525df9007af0d129b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268",
                "Source": "/var/lib/docker/volumes/ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268/_data",
                "Destination": "/var/www/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "e93e2b54fbc3",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "443/tcp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/usr/sbin/httpd",
                "-D",
                "FOREGROUND"
            ],
            "Image": "volumedemo:latest",
            "Volumes": {
                "/var/www/html": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "62ee762a473a7959a41d6dbb0be27f1b701ff08584b930c40c076d1e7ebc023e",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": null,
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8083"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/62ee762a473a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "09fd6ec4a9383d8a2b572220ef324aa8aac629b045083ccef9e85327564330a3",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1b61214ad6db146dbb7e8ebbc86492f08a1a0ec73da2bef2c1b89dabc2caa915",
                    "EndpointID": "09fd6ec4a9383d8a2b572220ef324aa8aac629b045083ccef9e85327564330a3",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

Mountsのところを見ると、以下のようになっています。

        "Mounts": [
            {
                "Type": "volume",
                "Name": "ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268",
                "Source": "/var/lib/docker/volumes/ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268/_data",
                "Destination": "/var/www/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
ホスト側 コンテナ側
/var/lib/docker/volumes/
ec262b...(ランダムな英数字)/_data
/var/www/html

下のイメージ図のような形でホスト側とコンテナ側でファイルが共有できています。

ホスト側でファイルを編集

[root@SV-TITAN ~]# cd /var/lib/docker/
[root@SV-TITAN docker]# cd volumes/
[root@SV-TITAN volumes]# cd ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268/
[root@SV-TITAN ec262bdcf1cf4f47182c9860e55d0a217e81e5eaebc02321565f507b55440268]# cd _data/
[root@SV-TITAN _data]# ls -l
合計 4
-rw-rw-r-- 1 root root 12  111 14:16 index.html
hello world!!!!!

ブラウザで表示確認

その⑮:ホスト側とコンテナ側のファイル共有(bindmountで任意のディレクトリを使用するパターン)

bindmount用のディレクトリ作成

$ cd ~/
$ mkdir bindmount
$ cd bindmount/
$ vi index.html
Hi!!!!!!!!!!!!

bindmoutのディレクトリを指定してコンテナ作成

sudo docker container run --name bindmount1 -d -p 8084:80 -v ~/bindmount:/var/www/html volumedemo:latest
オプション 意味
-v 右側がホスト側のディレクトリ、右側がコンテナ側のディレクト
$ sudo docker container run --name bindmount1 -d -p 8084:80 -v ~/bindmount:/var/www/html volumedemo:latest
70df7f545c39ff8743cffdf02e3a9ed1ba1764fecca307574e7c6af6f085636f

ブラウザで表示確認。

bindmountの場合は、下の図のようなイメージでホスト側とコンテナ側でファイルが共有できています。

ホスト側でファイルの編集

$ vi index.html
Hi!!!!!!!!!!!!
Bindmount test

その⑯:コンテナの停止、削除

sudo docker container stop 70df7f545c39
$ sudo docker container stop 70df7f545c39
70df7f545c39
$ sudo docker container stop e93e2b54fbc3
e93e2b54fbc3
$ sudo docker container stop 3aecce46fb85
3aecce46fb85
$ sudo docker container stop 99637d49c0ae
99637d49c0ae
sudo docker rm bindmount1
$ sudo docker rm bindmount1
bindmount1
$ sudo docker rm volumedemo1
volumedemo1
$ sudo docker rm dockerdemo2
dockerdemo2
$ sudo docker rm dockerdemo
dockerdemo
$ sudo docker rm objective_hoover
objective_hoover

その⑰:Imageの削除

sudo docker rmi volumedemo
$ sudo docker rmi volumedemo
Untagged: volumedemo:latest
Deleted: sha256:9f459b8b45455074d00821d81ce5848ebfb5458717f80b15d148b686d91a1743
Deleted: sha256:013710fca81e87b170bbc74f15a42dfd4527604ecf708ee133398b7612b486e0
Deleted: sha256:5c28b9ae6f756853e80dc104bac92bc5cd90232e865f270d0c0d19dfe90df3a6
$ sudo docker rmi dockerdemo2
Untagged: dockerdemo2:latest
Deleted: sha256:1be9c854d3129b37d9269049f8e73b67316adbd2f71d3898abc31237f9002858
Deleted: sha256:9ba8622d24f2997f150e5274e1e378ebd0e95d66aa4f4739792b1c55eccaddec
$ sudo docker rmi dockerdemo
Untagged: dockerdemo:latest
Deleted: sha256:2be8de742fc5c02b845a0128f4bca9054f864aeabcace3a3a5a3ca66732e2580
Deleted: sha256:c5d05b1b26860b42a6e56c1ced27c52e550fc16e009f0b53df629f7ec26e4c15
Deleted: sha256:4c1442296884c8a7c9e79b1b53cbbe8463174bbf1d2bf47e7b4f8371d19be0e9
Deleted: sha256:0ed82d4f2fb9d14ea99d06a11488aeb9e74556be8dfdcc43fc8d5c239f2b85b3
Deleted: sha256:0186eb487e50960712b817b29a63a33696edd5b5aad7f54501c115f16258e4bf
Deleted: sha256:07cfe4a6a91e06f094cc03d4e88f53f13b05bb0a16355068bd7638dd0d0722bc
$ sudo docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859

その⑱:テスト用のbridgeの削除

sudo docker network rm testbridge
$ sudo docker network rm testbridge
testbridge

さらにDockerについて勉強したい方におすすめの書籍

プログラマのためのDocker教科書 第2版
インフラの基礎知識&コードによる環境構築の自動化

インフラ基礎知識+インフラ環境構築の入門書の決定版!

Linuxのコンテナ仮想化ツールであるDockerを使って、コードでインフラ環境を構築するための入門書」として好評を博した前著の改訂版です。
開発環境や実行環境などインフラ構築「自動化」の経験がない開発者を対象に、おさえておきたいシステム基盤&インフラの基礎知識に加えて、Dockerによるインフラ構築と管理の方法をやさしく解説します。
理解しにくいネットワークや仮想技術などのインフラの基礎概念を図解しながら、開発/実行環境を実際に構築することで、構成管理ツールを使ったインフラ構築の流れを解説していきます。
改訂にあたって、Dockerの新機能/コマンドに対応したほか、マルチホスト環境での実行環境構築をクラウドマネージドサービスを使うように変更。
よりプログラマが注力すべきところにページを割き、Webエンジニアや機械学習エンジニアの環境構築に利用できる具体例を解説しています。

UdemyのおすすめのDocker講座

≫Udemy ゼロからはじめる Dockerによるアプリケーション実行環境構築 https://www.udemy.com/course/docker-k/