docker 从容器创建新镜像,及镜像的备份和恢复

Docker/容器
340
0
0
2023-04-15
标签   Docker

从容器创建新镜像:docker commit

备份/恢复镜像:docker save + docker loaddocker export + docker import

docker commit

操作的是容器。从容器创建新镜像

https://docs.docker.com/engine/reference/commandline/commit/

cuiwei@weideMacBook-Pro server % docker ps                                                  
CONTAINER ID   IMAGE                COMMAND                  CREATED              STATUS              PORTS                                                                                    NAMES
df15d5b449c6   nginx:1.21.3         "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   server-docker-nginx-1

#在此之前,先进入容器进行一些修改
cuiwei@weideMacBook-Pro server % docker commit df15d5b449c6  chudaozhe/nginx:test1
sha256:98f7915e8f85b81d12eadad38dc6124bae858384c03734886432f8dce7ea5c36

cuiwei@weideMacBook-Pro server % docker images                                    
REPOSITORY                                                TAG                                        IMAGE ID       CREATED              SIZE
chudaozhe/nginx                                           test1                                      98f7915e8f85   About a minute ago   133MB
nginx                                                     1.21.3                                     87a94228f133   17 months ago        133MB

#运行一下
cuiwei@weideMacBook-Pro server % docker run -d chudaozhe/nginx:test1          
6417f9eccd033d651351cb1d35004aa63efa163bd2d56be2e16c749d3c062dae

#进入新容器,看下刚才的修改是否还在
#肯定在!

其他命令

cuiwei@weideMacBook-Pro server % docker inspect -f "{{ .Config.Env }}" 6417f9eccd03                                                    
[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_VERSION=1.21.3 NJS_VERSION=0.6.2 PKG_RELEASE=1~buster]

docker commit --change "ENV DEBUG=true" df15d5b449c6 chudaozhe/nginx:test1

docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" df15d5b449c6 chudaozhe/nginx:test1

docker save + docker load

操作的是镜像。备份和恢复镜像

cuiwei@weideMacBook-Pro server % docker save nginx:1.21.3 | gzip > nginx_1.21.3.tar.gz
cuiwei@weideMacBook-Pro server % docker load < nginx_1.21.3.tar.gz
43f4e41372e4: Loading layer [==================================================>]  64.97MB/64.97MB
788e89a4d186: Loading layer [==================================================>]  3.072kB/3.072kB
f8e880dfc4ef: Loading layer [==================================================>]  4.096kB/4.096kB
f7e00b807643: Loading layer [==================================================>]  3.584kB/3.584kB
9959a332cf6e: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:1.21.3

docker save

将一个或多个图像保存到tar存档(默认流式传输到STDOUT)

https://docs.docker.com/engine/reference/commandline/save/

创建一个备份,然后与docker load一起使用。

docker save nginx:1.21.3 | gzip > nginx_1.21.3.tar.gz

docker save nginx:1.21.3 > nginx_1.21.3.tar

docker save --output nginx_1.21.3.tar nginx:1.21.3

docker save -o nginx_1.21.3.tar nginx:1.21.3

#同时备份两个标签
docker save -o nginx.tar nginx:1.21.3 nginx:mainline

docker load

tar存档或STDIN加载图像

https://docs.docker.com/engine/reference/commandline/load/

docker load < nginx_1.21.3.tar.gz

docker load --input nginx_1.21.3.tar.gz

docker export + docker import

操作的是容器。从容器创建新镜像,和docker commit导出所有层级不同,它只有一层。

另外运行时要加-t和bash参数,否则无法启动新容器1️⃣

cuiwei@weideMacBook-Pro server % docker export server-docker-nginx-1 > nginx_1.21.3.tar
cuiwei@weideMacBook-Pro server % docker export df15d5b449c6 > nginx_1.21.3_2.tar

#一定要指定镜像名和tag,否则就成虚悬镜像了(仓库名 (镜像名) 和标签 TAG 都是<none>的镜像。)
cuiwei@weideMacBook-Pro server % docker import nginx_1.21.3.tar chudaozhe/nginx:test1
sha256:c7905c3274d23683e66bcab36b860144857faec6bb3813384212002092dc0971

#运行一下,注意:要加-t和bash参数
cuiwei@weideMacBook-Pro server % docker run -d -t chudaozhe/nginx:test1 bash
fb64767ec3dacc97f0ccd26252fa9e51ceebddd453a07a0a7e380a2781c33791

docker export

将容器导出为tar存档

https://docs.docker.com/engine/reference/commandline/export/

docker export server-docker-nginx-1 > nginx_1.21.3.tar
docker export df15d5b449c6 > nginx_1.21.3_2.tar
docker export --output="nginx_1.21.3.tar" server-docker-nginx-1

docker import

从tarball导入内容以创建文件系统映像(tarball 即 Tar包(Tarball))

https://docs.docker.com/engine/reference/commandline/import/

docker import https://example.com/exampleimage.tgz chudaozhe/nginx:test1

cat exampleimage.tgz | docker import - exampleimagelocal:new

cat exampleimage.tgz | docker import --message "New image imported from tarball" - exampleimagelocal:new

docker import /path/to/exampleimage.tgz chudaozhe/nginx:test1

sudo tar -c . | docker import - exampleimagedir

sudo tar -c . | docker import --change "ENV DEBUG=true" - exampleimagedir

备份、恢复或迁移数据卷

https://docs.docker.com/storage/volumes/#back-up-restore-or-migrate-data-volumes

备份

#创建一个名为dbstore的新容器
docker run -v /dbdata --name dbstore ubuntu /bin/bash

#...上面容器运行一段时间,dbdata卷已经产生了数据

#备份dbdata卷到/backup目录
docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

恢复

#创建一个名为dbstore2的新容器
docker run -v /dbdata --name dbstore2 ubuntu /bin/bash

#恢复备份
docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1" 2️⃣

备注

1️⃣ 比如nginx

docker-compose.yml

...
    restart: always
    tty: true
    command:
      - /bin/bash
      - -c
      - nginx -g "daemon off;"
    volumes:
...

2️⃣ --strip 1:tar解压删除上层目录 需求

使用tar解压文件,需要移除第一层目录,例如

package
└── target
    └── vsomeip.json

转化为

target
└── vsomeip.json

实现

使用tar --strip参数

tar xvf xxxx.tar.gz --directory /your/path --strip 1

https://www.cnblogs.com/azureology/p/15834298.html