そういうのがいいブログ

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

Docke CEでImageのpull~コンテナ作成・実行・削除のテスト手順

※[PR]当ブログの記事の中にはプロモーションが含まれています。

Oracle Linux8にインストールしたDocker CEのコンテナ仮想環境でコンテナ作成~実行~削除までの簡易動作テストをやりたい。
・具体的な手順を教えてほしい。

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

本記事の内容

サトナカ (@souiunogaii)

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

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

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

Oracle Linux8サーバにインストールしたDocke CEのコンテナ仮想環境でImageのpull~コンテナの作成~実行~削除までの簡易動作テストの手順

前回の記事

souiunogaii.hatenablog.com

手順①:テスト用の「hello-world」Imageをpull

現在のImageの確認

docker images
[root@SV-TITAN ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

まだ何も無いのでImageは一つも表示されません。

Docke Hubから、テスト用の「hello-world」Imageのpull

docker pull hello-world
[root@SV-TITAN ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

再度、Imageの確認

docker images
[root@SV-TITAN ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB

「hello-world」というImageが追加されました。

手順②:コンテナの作成と実行

docker run hello-world
[root@SV-TITAN ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

「Hello from Docker!
This message shows that your installation appears to be working correctly.」

と表示されたOKです。

コンテナの状況を確認

docker ps -a
[root@SV-TITAN ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
9ea6beb5c18d   hello-world   "/hello"   28 seconds ago   Exited (0) 25 seconds ago             sharp_ardinghelli

コンテナの詳細情報を確認

docker inspect sharp_ardinghelli
[root@SV-TITAN ~]# docker inspect sharp_ardinghelli
[
    {
        "Id": "9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf",
        "Created": "2021-01-11T02:36:02.645439382Z",
        "Path": "/hello",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-11T02:36:05.38101401Z",
            "FinishedAt": "2021-01-11T02:36:05.380290534Z"
        },
        "Image": "sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b",
        "ResolvConfPath": "/var/lib/docker/containers/9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf/hostname",
        "HostsPath": "/var/lib/docker/containers/9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf/hosts",
        "LogPath": "/var/lib/docker/containers/9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf/9ea6beb5c18d794d31550673dc74cabc05eb01169ed1b0a98ceae04bcca955cf-json.log",
        "Name": "/sharp_ardinghelli",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "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/af78a7432d6f455d3e99b1135a0da3754582893245c262d17dba623e573f8be0-init/diff:/var/lib/docker/overlay2/4b8ecafd3455229683f44b2bb18220b2585ed617e8e8ada1d6ac8f88b4837d92/diff",
                "MergedDir": "/var/lib/docker/overlay2/af78a7432d6f455d3e99b1135a0da3754582893245c262d17dba623e573f8be0/merged",
                "UpperDir": "/var/lib/docker/overlay2/af78a7432d6f455d3e99b1135a0da3754582893245c262d17dba623e573f8be0/diff",
                "WorkDir": "/var/lib/docker/overlay2/af78a7432d6f455d3e99b1135a0da3754582893245c262d17dba623e573f8be0/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "9ea6beb5c18d",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "Image": "hello-world",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "920ef25c64884d993704f92e271adec8e41ff51fa54ffdb2c9865151facfe295",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/920ef25c6488",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1b61214ad6db146dbb7e8ebbc86492f08a1a0ec73da2bef2c1b89dabc2caa915",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

手順③:コンテナの削除

docker rm sharp_ardinghelli
[root@SV-TITAN ~]# docker rm sharp_ardinghelli
sharp_ardinghelli

手順④:Imageの削除

docker rmi hello-world
[root@SV-TITAN ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

Image削除後の確認

docker images
[root@SV-TITAN ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

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

souiunogaii.hatenablog.com

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

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

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

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

インフラエンジニアの勉強ができるオンライン学習サイト

Udemy (オンライン動画学習サイト)

Udemy(ユーデミー)は、オンラインで動画で学習できるサービスです。
ITエンジニア系の講座もたくさん公開されています。

≫Udemy 世界最大級のオンライン学習サイト

Udemyの特徴
  • ユーザー登録すれば、第1回の講座は無料で視聴できる
  • 無料のコンテンツもある
  • 過去に受講した人の人数や、受講した人の評価も表示されるので選びやすい

≫Udemy 世界最大級のオンライン学習サイト

開発の人気オンラインコース