2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dockerコンテナ↔Dockerイメージ↔バックアップファイル(バックアップ・リストア)

Last updated at Posted at 2022-07-18

1.Dockerコンテナ→Dockerイメージ作成

$ docker container commit container_name image_name:TAG

$ sudo docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB

$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND                 CREATED        STATUS                       PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   30 hours ago   Exited (137) 18 hours ago              sleepy_beaver
1917c7f76a5f   centos    "/bin/bash"             40 hours ago   Exited (255) 3 minutes ago             test2
35cb1215e744   centos    "/bin/cal"              40 hours ago   Exited (0) 19 hours ago                test1
99671ed077ff   centos    "/bin/bash"             40 hours ago   Exited (0) 18 hours ago                sad_wescoff

#Dockerイメージを作成
$ sudo docker container commit test2 masa/webtest:1.0
sha256:54000727131c8d1e4f63bb495bcb7c0fcaa11281a2d4c4ad2585c14f52e668a3

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
masa/webtest   1.0       54000727131c   8 seconds ago   231MB
centos         latest    5d0da3dc9764   10 months ago   231MB

#Dockerイメージの確認
$ sudo docker image inspect masa/webtest:1.0
[
    {
        "Id": "sha256:54000727131c8d1e4f63bb495bcb7c0fcaa11281a2d4c4ad2585c14f52e668a3",
        "RepoTags": [
            "masa/webtest:1.0"
        ],
(省略)

2.Dockerコンテナ→tarファイルバックアップ

$ docker container export container_name > stoc_name.tar

$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND                 CREATED        STATUS                        PORTS     NAMES
35cb1215e744   centos    "/bin/cal"              41 hours ago   Exited (0) 19 hours ago                 test1
99671ed077ff   centos    "/bin/bash"             41 hours ago   Exited (0) 18 hours ago                 sad_wescoff

#tarファイル出力
$ sudo docker container export test1 > test1_t1.tar

$ ls -lh
合計 228M
-rw-rw-r-- 1 masa masa 228M  7月 18 14:26 test1_t1.tar

#tarファイルの中身確認
$ tar -tf test1_t1.tar | head -10
.dockerenv
bin
dev/
dev/console
dev/pts/
dev/shm/
etc/
etc/.pwd.lock
etc/BUILDTIME
etc/GREP_COLORS

2.1 Dockerコンテナtarファイル→Dockerイメージ作成

$ cat stoc_name.tar | sudo docker image import - image_name:TAG

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
masa/webtest   1.0       54000727131c   25 minutes ago   231MB
centos         latest    5d0da3dc9764   10 months ago    231MB

$ ls -lh
合計 228M
-rw-rw-r-- 1 masa masa 228M  7月 18 14:26 test1_t1.tar

#DockerコンテナのtarファイルからDockerイメージファイルを作成
$ cat test1_t1.tar | sudo docker image import - masa/test1:1.1
sha256:6d190dae723018c8b6cbcef90444c593edea9aeb5b5d5527fae0b0c962c8f4f5

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
masa/test1     1.1       6d190dae7230   10 seconds ago   231MB  ←追加されたDockerイメージ
masa/webtest   1.0       54000727131c   29 minutes ago   231MB
centos         latest    5d0da3dc9764   10 months ago    231MB

3.Dockerイメージ→tarファイルバックアップ

$ docker image save -o file_name.tar Docker_image_name

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
masa/test1     1.1       6d190dae7230   4 hours ago     231MB
masa/webtest   1.0       54000727131c   4 hours ago     231MB
centos         latest    5d0da3dc9764   10 months ago   231MB

#失敗時のエラーメッセージ
$ sudo docker image save -o centos_img.tar centos
open .docker_temp_056170351: permission denied

#自分の環境では保存ディレクトリに書き込み権限が必要だった
$ chmod 777 docker/

#再度コマンドを実行して無事に成功
$ sudo docker image save -o ./webtest_img.tar masa/webtest:1.0
masa@masa-hp-compaq:~/docker$ ls -l
合計 465980
-rw-rw-r-- 1 masa masa 238571520  7月 18 14:26 test1_t1.tar
-rw------- 1 root root 238587904  7月 18 18:10 webtest_img.tar

3.1 Dockerイメージtarファイル→Dockerイメージ作成

$ docker image load -i Docker_img.tar

$ sudo docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
masa/test1   1.1       6d190dae7230   4 hours ago     231MB
centos       latest    5d0da3dc9764   10 months ago   231MB

$ ls
test1_t1.tar  webtest_img.tar

#Dockerイメージファイル(webtest_img.tar)からのリストア
$ sudo docker image load -i webtest_img.tar
fd69d8d21afa: Loading layer  3.584kB/3.584kB
Loaded image: masa/webtest:1.0

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
masa/test1     1.1       6d190dae7230   4 hours ago     231MB
masa/webtest   1.0       54000727131c   4 hours ago     231MB  ←リストア完了
centos         latest    5d0da3dc9764   10 months ago   231MB

4.Dockerイメージの削除

$ docker image rm Docker_img_name

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
masa/test1     1.1       6d190dae7230   4 hours ago     231MB
masa/webtest   1.0       54000727131c   4 hours ago     231MB  ←削除対象
centos         latest    5d0da3dc9764   10 months ago   231MB

#削除実行
$ sudo docker image rm masa/webtest:1.0
Untagged: masa/webtest:1.0
Deleted: sha256:54000727131c8d1e4f63bb495bcb7c0fcaa11281a2d4c4ad2585c14f52e668a3
Deleted: sha256:cb1f46395e98515dc7926d430428c94f86f4e44130d81be68d44fad47ad25ae8

$ sudo docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
masa/test1   1.1       6d190dae7230   4 hours ago     231MB
centos       latest    5d0da3dc9764   10 months ago   231MB
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?