The idea behind my starting a TechBite series of articles is to share technical solutions I have found out or stumbled upon whilst using tools and/or services.
Now to the first TechBite!
Before we begin, let's answer your question — Why would you want to save a docker image? 🤔
Why?
- Suppose docker images used by your organization are stored in a private container registry and a new team member joins your team but currently does not have access permission to this private registry and needs to set up his/her development environment. In this case, you might want to save (export) your image and share it with the new team member so he/she can get started.
- There is a requirement for you to store your images in a binary repository like Artifactory or Nexus and during the build process, this gets distributed to your docker servers using Jenkins.
- You just want a backup image 😃
How do we save (export) a Docker image? 🤔
One of the below command variations will help you save (export) a specified IMAGE_NAME to tar or gzip file.
docker save <IMAGE_NAME> > <FILE>.tar
docker save <IMAGE_NAME> -o <FILE>.tar
docker save <IMAGE_NAME> --output <FILE>.tardocker save <IMAGE_NAME> | gzip > <FILE>.tar.gz
How do we load our saved Docker image? 🤔
docker load < <FILE>.tar
docker load -i <FILE>.tar
docker load --input <FILE>.tardocker load -i <FILE>.tar.gz
docker load < <FILE>.tar.gz
An example
To demonstrate how the above commands can be used, I will give an example.
First, let's create our own image from the below Dockerfile
FROM alpine:latest
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]
Next, we build and tag our image
$ docker build -t techbitefriday/save-laod-docker:latest .
Then, we save this image
$ docker save techbitefriday/save-laod-docker:latest > firstTechbite.tar
To show a scenario of having to load an image, let us delete the initial image we built.
$ docker images
$ docker image rm techbitefriday/save-laod-docker
Now, we load up our image from the tar file it was saved to.
$ docker load -i firstTechbite.tar
And its a wrap for today's TechBite 😃
References