docker 容器打包镜像,并导出镜像,放在服务器中的docker容器中

5,493 阅读5分钟

在本地项目开,完成项目开发后,本地启动docker容器可以看到下面列表中有多个容器,而第一个则是演示的案例。

image.png

一、打包镜像

1、根据当前运行容器生成docker镜像,(这个是有特殊要求可不做) 使用 docker commit 命令打包容器为镜像

docker commit [选项] <容器ID或容器名称> <镜像名称:标签>

参数说明:

  1. <容器ID或容器名称>:指定你要打包的容器。
  2. <镜像名称:标签>:为新生成的镜像命名并添加标签(可选标签,默认是 latest)。

常用选项:

  1. -m "message":添加提交说明信息。
  2. -a "author":指定作者信息。 示例:
# 将容器 my-container 打包为名为 my-image,标签为 v1 的镜像:
docker commit -m "Custom image from container" -a "YourName" my-container my-image:v1

案例:进行设置案例,就可以看到有个镜像叫zhichang:

docker commit 2ac0f35401fb zhichang:latest
docker images

image.png

二、导出镜像

导出镜像到文件 使用 docker save 将本地镜像导出为 .tar 文件。

导出单个镜像

docker save -o <文件路径> <镜像名称>:<标签>

示例:

docker save -o nginx_latest.tar nginx:latest

这会将 nginx:latest 镜像保存为 nginx_latest.tar 文件。 导出多个镜像 如果需要导出多个镜像,可以将它们的名称和标签一同指定:

docker save -o images_backup.tar nginx:latest mysql:5.7 redis:6.2

这会将指定的镜像一起保存到 images_backup.tar 文件中。

案例:打包镜像到本地文件夹中,获取zhichang.tar文件

docker save -o zhichang.tar zhichang:latest

image.png

三、上传文件到服务器

使用 scp 命令可以将文件从本地传输到远程 SSH 服务器中,以下是具体的使用方法和示例。

# 命令格式
scp [OPTIONS] <本地文件路径> <远程用户>@<远程主机>:<目标路径>

上传文件到远程服务器

示例 1:上传单个文件
scp /path/to/local/file.txt user@remote_host:/path/to/remote/directory/

示例 2:上传文件并重命名
scp /path/to/local/file.txt user@remote_host:/path/to/remote/directory/new_file.txt

示例 3:上传整个目录
scp -r /path/to/local/directory/ user@remote_host:/path/to/remote/directory/

/path/to/local/file.txt:本地文件路径。

user:SSH 用户名。

remote_host:远程主机地址(可以是 IP 或域名)。

/path/to/remote/directory/:远程目标目录路径。

将本地文件 file.txt 上传到远程路径,并重命名为 new_file.txt。 -r 参数表示递归上传整个目录。

案例 :

ds@M1-A1029845:~/projects$ scp ./zhichang.tar  root@47.119.55.16:/home
root@47.119.55.16's password:
zhichang.tar                                                                           16%  207MB   9.1MB/s   02:00 ETAL
zhichang.tar                                                                           64% 
zhichang.tar                                                                          100% 1295MB   7.5MB/s   02:51


上传完成后登录服务器查看home文件夹中是否存在该文件

ds@M1-A1029845:ssh root@47.119.55.16
root@47.119.55.16's password:
[root@iZ2vce7h83cpide1n6l956Z /]# cd /home
[root@iZ2vce7h83cpide1n6l956Z home]# ls -l
total 1328108
drwxr-xr-x  5 ds ds       4096 Dec 30 17:29 lottery
drwxr-xr-x  5 ds ds       4096 Dec 24 11:54 lottery-old
-rw-r--r--  1 ds ds    2513073 Dec 30 17:33 lottery.zip
drwxr-xr-x 23 ds ds       4096 Dec 17 09:51 metadata
drwxr-xr-x 16 ds ds       4096 Dec  5 17:19 noexcel
drwxr-xr-x  2 ds ds       4096 Dec 27 15:55 other
drwxr-xr-x 14 ds ds       4096 Dec 27 17:17 supersonic
drwxr-xr-x 13 ds ds       4096 Dec 30 13:59 udata
-rw-------  1 ds ds 1357434880 Dec 31 09:25 zhichang.tar

四、导入镜像到docker中

在 Linux 系统中,导入 Docker 镜像(即加载 .tar 文件到 Docker)可以通过以下步骤完成。

1. 准备镜像文件

确保你已经有一个导出的 Docker 镜像文件(通常是 .tar 格式)。如果没有,可以通过 docker save 命令在其他系统中导出镜像(参考上一步)。

2. 使用 docker load 命令导入镜像

bash
复制代码
docker load -i <镜像文件路径>
示例:
docker load -i nginx_latest.tar

Docker 会开始加载镜像文件,并输出类似以下的信息:

Loaded image: nginx:latest

3. 验证导入结果

使用以下命令检查镜像是否成功加载到本地:

docker images

你应该能在列表中看到导入的镜像信息,例如:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    1a2b3c4d5e6f   2 days ago    133MB

4. 使用导入的镜像

导入成功后,你可以直接使用该镜像启动容器。例如:

bash
复制代码
docker run -d -p 8080:80 nginx:latest

5. 注意事项

  1. 镜像版本问题: 如果 .tar 文件中包含多个镜像版本,docker load 会逐一导入并显示加载的版本。

  2. 文件路径问题: 确保 .tar 文件路径正确。如果镜像文件在其他目录,先切换到该目录或提供完整路径:

    docker load -i /path/to/nginx_latest.tar
    
  3. 清理未使用镜像: 如果发现本地存储空间不足,可以清理未使用的镜像和容器:

    docker image prune -f
    docker container prune -f
    
  4. 镜像冲突: 如果本地已经存在相同名称和标签的镜像,docker load 会覆盖旧镜像。

案例: 查看当前容器的镜像,通过docker load 将本地的镜像上传到docker中,再次查看会多一个zhichang的镜像,启动镜像则该容器启动成功

docker load -i zhichang.tar
docker images
docker run -d -p 7727:8888 zhichang

image.png