Docker save Command
docker save The 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: The name or ID of one or more images 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 a 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
# Add maintainer information
LABEL maintainer="yourname@example.com"
# Update package list and install Nginx
RUN apt-get update && apt-get install -y nginx
# Copy custom webpage to Nginx default webpage directory
COPY index.html /var/www/html/
# Set the default command on startup
CMD ["nginx", "-g", "daemon off;"]
Build the 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 a mynginx.tar file.
3. Verify the saved file
ls -lh mynginx.tar
Example output:
-rw-r--r-- 1 user user 200M Jul 24 14:00 mynginx.tar
4. Load the saved image
To load a saved image into another Docker environment, you can use the docker load command:
docker load -i mynginx.tar
Notes
- When saving an image, all its layers are included, so the resulting tar file can be very large.
- If you save multiple images to the same file, using the
docker loadcommand will load all the contained images. - To reduce the file size, you can use the
docker image prunecommand before saving to clean up unused images and layers.
docker save The command is a convenient tool for saving Docker images to tar files for backup, distribution, and migration. By combining it with docker load you can easily restore and use saved images in different environments.
Docker Command Reference