投げ銭

★当サイトへの投げ銭(PayPal)★

LINK


(無償、寄付歓迎) logo
世界中で使われるISO標準オフィスソフト(MSオフィス互換)

★LibreOfficeの導入事例★
詳細について

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

★当サイトへの投げ銭(PayPal)★

2018年8月24日金曜日

【Linux CentOS 7】変更済みのDockerコンテナから別名でイメージを作成することでその加えた変更を維持する手順【Docker-ce 18.06.1.ce】


Dockerコンテナに加えた変更は、そのコンテナを終了して次回オリジナルのイメージから起動しても復元されない。
内容変更済みのコンテナは、終了する前に、別名でイメージ化することで維持することが可能である。

その手順を下記に記している。


■centosイメージをDocker Hubから取得してコンテナを起動

イメージ(centos)からコンテナを作成し、
コンテナ内のbashコマンドを実行するように指定したので、そのコマンドプロンプトが表示された。

[root@localhost ~]# docker run -it centos bash
[root@97e4da7f0251 /]#

以降、このコンテナ内に、例としてパッケージの導入することで変更を加えていく。


■起動したコンテナにおいて何かパッケージをインストール

○試しに、ifconfig コマンドを実行

初期状態では、ifconfigコマンドは存在しないため、not found となった。

[root@97e4da7f0251 /]# ifconfig
bash: ifconfig: command not found

○ifconfig コマンドを含む net-toolsパッケージをインストール

[root@97e4da7f0251 /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * extras: mirrors.cat.net
 * updates: mirrors.cat.net
base                                                     | 3.6 kB     00:00   
extras                                                   | 3.4 kB     00:00   
updates                                                  | 3.4 kB     00:00   
(1/4): extras/7/x86_64/primary_db                          | 186 kB   00:00   
(2/4): base/7/x86_64/group_gz                              | 166 kB   00:00   
(3/4): updates/7/x86_64/primary_db                         | 5.2 MB   00:00   
(4/4): base/7/x86_64/primary_db                            | 5.9 MB   00:01   
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
 Package         Arch         Version                          Repository  Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.22.20131004git.el7         base       305 k
Transaction Summary
================================================================================
Install  1 Package
Total download size: 305 k
Installed size: 917 k
Is this ok [y/d/N]:
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.22.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.22.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm              | 305 kB   00:00   
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-5.1804.1.el7.centos.x86_64 (@Updates)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.22.20131004git.el7.x86_64                    1/1
  Verifying  : net-tools-2.0-0.22.20131004git.el7.x86_64                    1/1
Installed:
  net-tools.x86_64 0:2.0-0.22.20131004git.el7                                 
Complete!

○パッケージの導入が終わったので、ifconfigコマンドを再び実行

今度は、コマンドが実行できるようになった。

[root@97e4da7f0251 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 4470  bytes 13690881 (13.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3750  bytes 257994 (251.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

これで、コンテナに変更が加わった。


■変更を加えたコンテナを別のイメージとして保存

 上記では、コンテナに何かパッケージをインストールしてコンテナに変更を加えた。
しかし、そのままこのコンテナを終了し、再度イメージからコンテナを起動しても、変更は復元されない
 復元するには、パッケージやファイルなど変更したコンテナを終了する前にイメージ化する必要がある


○ 現存するimageの一覧

centos というイメージがオリジナルである。

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              5182e96772bf        2 weeks ago         200MB

○ 動作中のコンテナのIDの確認

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
97e4da7f0251        centos              "bash"              2 hours ago         Up 2 hours                              gifted_fermi

○ 上記コンテナを新しい名前でイメージ化

この変更済みコンテナ(97e4da7f0251)から、イメージを別名(centos-test)で新規作成する。

# docker commit 97e4da7f0251 centos-test
sha256:b48aa8f6e884845571ec780e5583ae59a3165604641685a8beb6c43e4bf12a06

○ imageの一覧

作成した新規イメージを確認できる。

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-test         latest              b48aa8f6e884        8 seconds ago       293MB
centos              latest              5182e96772bf        2 weeks ago         200MB


■保存したイメージからコンテナを起動

# docker run -it centos-test bash

ifconfigコマンドが利用できる状態である。

[root@b3b25c971605 /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
念の為、パッケージの状態を確認した。
下記の通り、インストール済みになっている。

[root@b3b25c971605 /]# yum info net-tools
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Installed Packages
Name        : net-tools
Arch        : x86_64
Version     : 2.0
Release     : 0.22.20131004git.el7
Size        : 917 k
Repo        : installed
From repo   : base
Summary     : Basic networking tools
URL         : http://sourceforge.net/projects/net-tools/
License     : GPLv2+
Description : The net-tools package contains basic networking tools,
            : including ifconfig, netstat, route, and others.
            : Most of them are obsolete. For replacement check iproute package.

以上



<参考>
・I lose my data when the container exits
< https://stackoverflow.com/questions/19585028/i-lose-my-data-when-the-container-exits > 2018年8月24日

・Difference between save and export in Docker
< https://tuhrig.de/difference-between-save-and-export-in-docker/ > 2018年8月24日

・docker commit
< https://docs.docker.com/engine/reference/commandline/commit/ > 2018年8月24日

・How to commit changes to a docker image
< https://www.techrepublic.com/article/how-to-commit-changes-to-a-docker-image/ > 2018年8月24日

投げ銭

★当サイトへの投げ銭(PayPal)★

Ad

Ad