自分用メモ:はじめてDockerを使う


はじめに

初めてDockerを使うため、調べたことを自分用にメモします。

参考にさせていただいたページ

https://hub.docker.com/
https://weblabo.oscasierra.net/docker-ce-install-centos7/

実行環境(ホスト側)

# uname -a 
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

ゴール

ホストPC(CentOS7 64bit)にDockerをインストールして、
CentOS7のDockerイメージを起動させて、
Dockerイメージの変更、保存、破棄を行って、また最初から。をやってみる

以下、やったことメモ

ホストPC側のセットアップ

hostPC
# yum update
hostPC
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
読み込んだプラグイン:fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
hostPC
# yum -y install docker-ce
...
エラー: docker-ce conflicts with 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
エラー: docker-ce-cli conflicts with 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
 問題を回避するために --skip-broken を用いることができます。
 これらを試行できます: rpm -Va --nofiles --nodigest

とな。

hostPC
# yum remove docker*
...
削除中:
 docker           x86_64    2:1.13.1-96.gitb2f74b2.el7.centos       @extras     65 M
 docker-client    x86_64    2:1.13.1-96.gitb2f74b2.el7.centos       @extras     13 M
 docker-common    x86_64    2:1.13.1-96.gitb2f74b2.el7.centos       @extras    4.4 k
...

で削除対象になるパッケージを確認したところ回避できた.

hostPC
# yum -y install docker-ce
...
インストール:
  docker-ce.x86_64 3:18.09.7-3.el7

依存性関連をインストールしました:
  containerd.io.x86_64 0:1.2.6-3.3.el7      docker-ce-cli.x86_64 1:18.09.7-3.el7

完了しました!
hostPC
# systemctl enable docker
# systemctl start docker

で開始できた。

hostPC
# ps aux | grep docker
root      83281  0.3  2.8 462248 52492 ?        Ssl  15:35   0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root      83444  0.0  0.0 112724   992 pts/1    S+   15:37   0:00 grep --color=auto docker

あんまりにもサービス開始にレスポンスがなかったので、
調べてみた。ちゃんと動いていそう。

hostPC
systemctl stop docker

※止めるときは↑。

CentOSのDockerイメージを作ってみる

hostPC
# docker pull centos:centos7
centos7: Pulling from library/centos
8ba884070f61: Pull complete
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for centos:centos7

でCentOS7をDockerHubから取得。

hostPC
# docker run -it -d --name test1 centos:centos7
# docker exec -it test1 /bin/bash

test1という名前のCentOS7をDocker上で実行.

test1
# uname -a
Linux ed00158e3703 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
# exit

で抜けれた。DockerはKernel共有すると聞いたけど、
このunameで取得できるのはホストPCなのか、Docker側なのか、
気になって眠れなかったので調べてみた。

hostPC
# docker pull centos:centos6
centos6: Pulling from library/centos
ff50d722b382: Pull complete
Digest: sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7
Status: Downloaded newer image for centos:centos6
# docker run -it -d --name test3 centos:centos6
e090c530af6713a4b1dbaa4841a9ff72276f2b9f6417f9de8620b4872ebafb29
# docker exec -it test3 /bin/bash

と、CentOS7と同じ要領でCentOS6を取得、実行。

test3
[root@e090c530af67 /]# uname -a
Linux e090c530af67 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

たしかにkernelが共有されていることを確認。

# docker stop test1

で停止し、

# docker rm test1

で消せると。
サクサクすぎて怖いくらい簡単に消せる。

Dockerイメージを変更、保存、削除、復元をやってみる

hostPC
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             d0957ffdf8a2        4 months ago        194MB
centos              centos7             9f38484d220f        4 months ago        202MB

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

この状態からスタート

hostPC
# docker run -it -d --name test1 centos:centos7
44672aa1dcb04ddbea58b897c3dfb700b873d4f0fc4a2617ea26ad82d69935b7

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
44672aa1dcb0        centos:centos7      "/bin/bash"         4 seconds ago       Up 3 seconds                            test1

# docker exec -it test1 /bin/bash

で、test1のDockerイメージを作成して、
test1にbashで入る。

test1
[root@44672aa1dcb0 /]# cd
[root@44672aa1dcb0 ~]# touch test.txt
[root@44672aa1dcb0 ~]# ll
total 4
-rw-------. 1 root root 3415 Mar  5 17:36 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Jul 15 07:30 test.txt

[root@44672aa1dcb0 ~]# exit

test1上の/rootに、test.txtを作成してみる。

hostPC
# docker commit -m "test.txt add" 44672aa1dcb0 centos7-test1-1st
sha256:40d24fae2e41daebf700e7ea38a98052acc6b23f991eddf44d7ef13371927d8d

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7-test1-1st   latest              40d24fae2e41        8 seconds ago       202MB
centos              centos6             d0957ffdf8a2        4 months ago        194MB
centos              centos7             9f38484d220f        4 months ago        202MB

となった状態で、

hostPC
# docker exec -it test1 /bin/bash

からの

test1
[root@44672aa1dcb0 ~]# vi test.txt

[root@44672aa1dcb0 ~]# ll
total 8
-rw-------. 1 root root 3415 Mar  5 17:36 anaconda-ks.cfg
-rw-r--r--. 1 root root    4 Jul 15 07:40 test.txt

で、なんらかtext.txtを修正.

hostPC
# docker stop test1

# docker rm test1

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

で停止→全消し。

hostPC
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7-test1-1st   latest              40d24fae2e41        2 minutes ago       202MB
centos              centos6             d0957ffdf8a2        4 months ago        194MB
centos              centos7             9f38484d220f        4 months ago        202MB

# docker run -it -d --name test1 centos7-test1-1st
88e69cb9ad2f53c1ff784d7c801490fb329d30b3fd0e065abae89ea3f7729838

# docker exec -it test1 /bin/bash

で、再生成してみる。

test1
[root@88e69cb9ad2f /]# cd
[root@88e69cb9ad2f ~]# ll
total 4
-rw-------. 1 root root 3415 Mar  5 17:36 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Jul 15 07:30 test.txt

復元(もとに戻ること)を確認。

ここまでやって、次アクションが分かるようになった