Docker Image
An Docker image is basically a big tarball containing a filesystem.
Docker Image Explained
Docker image is composed of many layers. Each layer contains files and folders. Docker uses a union filesystem to create a virtual filesystem out of the layers. All layers in a Docker image are immutable.
When the container is created from an Image. Docker adds a writable layer on top of the immutable layers.
Tag
Each image has a tag. A tag is used to version image.
Image Operations
Search Image
Search the Docker Hub for images
1 | docker search image_name |
Example
1 | docker search busybox |
Pull Image
By default, docker pull pulls images from Docker Hub.
If no tag is provided, Docker Engine uses the :latest tag as a default.
Syntax
1 | docker pull image_name |
Example to pull the latest alpine image
1 | docker pull alpine |
Example to pull MySQL image version 5.7
1 | docker pull mysql:5.7 |
Example to pull from a private registry hosted at https://localhost:5000
1 | docker pull localhost:5000/test/test-image |
List Image
docker image command
1 | docker images |
docker images
can take an optional [REPOSITORY[:TAG]] argument. You can set REPOSITORY but no TAG or REPOSITORY only
1 | docker images maven |
You can use filterring flag( -f or –filter) to list with filter
To list dangling images
1 | $ docker images --filter "dangling=true" |
To list images before given image id(ab4287b7a939)
1 | $ docker images -f "before=ab4287b7a939" |
docker images command can follow option -q
to show only the numeric IDs. This is useful when it comes to removing image.
Example to use docker image in conjunction with docker rmi
to remove images. see Example to remove dangling images:
1 | docker rmi $(docker images -f "dangling=true" -q) |
Reference
Build Image from Dockerfile
1 | docker build -t image_name directory_path |
Remove Image
To remove an image
1 | docker rmi image_name/image_id |
To remove dangling images
1 | docker rmi $(docker images -f "dangling=true" -q) |
This also removes all dangling images
1 | docker image prune |
Save and load
Example to save an image to a tarball
1 | docker image save -o /tmp/testimage.tar testimage:v1 |
Example to load an image from a tarball
1 | docker image load -i /tmp/testimage.tar |
Docker Registry
A Docker registry
stores many Docker Repositories. A Docker Repository
contains many versions of the same Docker image.
Docker Trusted Registry(DTR) is the enterprise-level registry from Docker.
When you use the docker pull
or docker run
commands, the required images are pulled from your configured registry. When you use the docker push
command, your image is pushed to your configured registry.
Registry is also a Docker image you can run.
1 | docker run -d -p 5000:5000 --name registry registry |
Push Image
Example to push image to docker hub
First tag an image. A tag is used to version images.
1 | docker image tag 196d12cf6ab1 username/image:v1 |
Next, Need to login to the account. Docker Hub in this case
1 | docker login -u username |
Push image to Docker Hub
1 | docker image push username/image:v1 |