This post feels like a step back from my previous
post
on managing services on Docker Swarm. The major reason for this: when
working with a multi-node Docker Swarm cluster, Docker images must be
on each cluster. I used two ways to get images onto the cluster:
traditional file transfer and via Docker Hub.
I had to learn how to work with Docker images by:
Build images
Tag images
Save built images to disk
Load images from disk
Push images
Pull images
Requirements
If you would like to follow along this article, these are the
requirements:
A working Dockerfile, I am using this
one
from a previous article.
To build a Docker image, have a working Dockerfile. I will be using
the
rails-git-version
app built previously that has this basic Dockerfile:
Docker Build
The Docker build command is simply: docker build ., which builds an
image using Dockerfile.
The image built, but the name is <none> and tagged as <none>. The
only way to use this is image is to use its image ID: 182196cb787b,
not useful when working with Docker Swarm, which requires images have
names.
After creating a Docker image, to save the image to disk, so you can
put it on an external drive, email it, FTP it, SCP it, etc. use the
Docker save command, which has the format:
docker save -o <image filename> <image>
This is the easiest way to make Docker images accessible.
Now that you have a Docker image on disk, how to load a Docker image
from disk so Docker services, such as docker run, or docker service
create can use it? Use the docker load command, which has the
command format of:
docker load < <image filename>
Let’s delete the current image from Docker and load it back:
Another way to load the Docker image: put it on Docker
Hub! It’s a Docker image registry. Once an
image is on there, any system that can connect to Docker Hub can load
the image.
Login
After creating an account on Docker Hub, login on the command line
using docker login command:
Rename Image
To push the image to Docker Hub, rename the image to the format:
<docker hub username>/<image name>
The reason for this: the default registry for the docker push
command is: https://docker.io (which is equivalent to Docker
Hub). Adding that in front of the image, the full URI would become:
This time we went over how to work with Docker images through tags, at
a file level: saving images to disk, loading images from disk, and
using Docker Hub as a way to store and access images remotely.