Docker save Command

Docker command reference


docker saveThe command is used to save one or more Docker images to a tar archive file for distribution or backup in other environments.

Syntax

docker save [OPTIONS] IMAGE [IMAGE...]
  • IMAGE: One or more image names or IDs to save.

OPTIONS description:

  • -o, --output: Specify the path of the output file.

1. Save a single image to a file.

docker save -o myimage.tar myimage:latest

This saves the myimage:latest image as the myimage.tar file.

2. Save multiple images to the same file.

docker save -o multiple_images.tar myimage:latest anotherimage:latest

This saves the myimage:latest and anotherimage:latest images to the multiple_images.tar file.

Example

Build a sample image.

1. Create a Dockerfile:

# 使用 Ubuntu 作为基础镜像
FROM ubuntu:20.04

# 添加维护者信息
LABEL maintainer="yourname@example.com"

# 更新包列表并安装 Nginx
RUN apt-get update && apt-get install -y nginx

# 复制自定义网页到 Nginx 的默认网页目录
COPY index.html /var/www/html/

# 设置启动时的默认命令
CMD ["nginx", "-g", "daemon off;"]

Build image:

docker build -t mynginx:latest .

2. Save the image to a file.

docker save -o mynginx.tar mynginx:latest

This saves the mynginx:latest image as the mynginx.tar file.

3. Verify the saved file.

ls -lh mynginx.tar

Output example:

-rw-r--r-- 1 user user 200M Jul 24 14:00 mynginx.tar

4. Load the saved image.

To load the saved images into another Docker environment, you can use the docker load command:

docker load -i mynginx.tar

Notes

  • When saving an image, all layers of the image are included, so the resulting tar file can be very large.
  • If saving multiple images to the same file, usedocker loadThe command will load all contained images.
  • To reduce the file size, you can use the following before saving:docker image pruneThe command cleans up unused images and layers.

docker saveThe command is a convenient tool for saving Docker images as tar files for backup, distribution, and migration. By combiningdocker loadcommand, you can easily restore and use saved images in different environments.


Docker command reference

other extensions