Image
Understand how to manage image.
Image Operations
List images
Search images
Pull / Download the image
Push image
Tag image
Inspect image
It will display detailed information on one or more images.
Remove image and remove all unused image
Before deleting an image, all containers must be removed or deleted first, as they are dependent on that image.
Display image layers
Save or load image
Imagine you are in an environment without access to Wifi to download the image. With image save commands, you can convert your image into a tar file, copy that to your environment, and then extract it.
Convert container into image in a tar format using Import and Export operations
Make or modify the image to a single layer.
With import and export commands, basically, we are flattening a Docker image into a single layer, therefore, we will get a smaller size of the image.
You can also export the exited container as well.
It only exports the contents of the directory, without the contents of the volume.
Image naming convention and Authenticate to registries
It's made up of a slash-separated name components. Before you push those images to respective registries, you have to perform authentication to the registry that you want to push.
For example;
- Harbor: "harbor_address"/project/repository
- "karchunt.registry.com/python/auto-deploy"
- Dockerhub: "docker.io/username/repository"
- "docker.io/karchunt/maven-with-docker"
Once you login successfully, those credentials will be stored in $HOME/.docker/config.json
on Linux or %USERPROFILE%/.docker/config.json
on Windows.
Naming | registry | project/user/account | image/repository |
---|---|---|---|
karchunt.registry.com/python/auto-deploy | karchunt.registry.com | python | auto-deploy |
docker.io/karchunt/maven-with-docker | docker.io | karchunt | maven-with-docker |
Dockerfile (Build a custom image)
Dockerfile is a text file that will consist of all the steps that are required to build a custom image. Here is a very basic and sample Dockerfile to dockerize Flask application.
When you run the docker build
command, all files under the build context are transferred to the Docker Daemon. It stores them in /var/lib/docker/tmp
for temporary storage. Docker looks for Dockerfile
at whatever path is specified in the build context.
- You can also create
.dockerignore
file to tell the build context to exclude or ignore those files or directories.
Dockerfile explanation
Instruction | Description |
---|---|
ADD | Add local or remote files and directories. |
ARG | Use build-time variables. This argument will be used during the docker build section, to remove those hardcoded values |
CMD | Specify default commands should be executed when container is running. |
COPY | Copy files and directories. |
ENTRYPOINT | Specify default executable. |
ENV | Set environment variables. |
EXPOSE | Describe which ports your application is listening on. It does not actually publish the port. |
FROM | Create a new build stage from a base image. Our image will be customized using this initial set of programs or tools |
HEALTHCHECK | Check a container's health on startup. |
LABEL | Add metadata to an image and it's a key value pair. |
MAINTAINER | Specify the author of an image. |
ONBUILD | Specify instructions for when the image is used in a build. |
RUN | Execute build commands. |
SHELL | Set the default shell of an image. |
STOPSIGNAL | Specify the system call signal for exiting a container. |
USER | Set user and group ID. |
VOLUME | Create volume mounts that need specified folder to be persistent inside container. We also need to specify docker run -v <path>:<path-in-container> |
WORKDIR | Change working directory. |
WORKDIR
It can be used multiple times in a Dockerfile
.
HEALTHCHECK
Basically, it will check a container's health on startup by telling the platform on how to test the application is healthy. It will also monitor the container process when it's running.
Parameters for HEALTHCHECK
--interval=DURATION
(default: 30s)--timeout=DURATION
(default: 30s)--start-period=DURATION
(default: 0s)--retries=N
(default: 3)
Exit status | Description |
---|---|
0 | success |
1 | failure |
2 | reserved (do not use the exit code) |
COPY vs ADD
In Dockerfile, COPY
is recommended over ADD
to reduce layer count
Both of them are just copying files, but the ADD
instruction will have more usage compared to COPY
. COPY
just lets you copy, while ADD
can auto extract the tar file into the path inside the image. For URL, it will only download, but does not perform the extraction.
CMD vs ENTRYPOINT (Utility container)
A lot of people confuse CMD
and ENTRYPOINT
instructions.
ENTRYPOINT
- Specify the default executable, which means setting the image's main command. It cannot be overridden.
CMD
- Able to override, for example
docker run <image> 7
, "7" will be replacedCMD
command that you specify inDockerfile
.
Build cache
Every layer in the Dockerfile has a cache. It will compare instructions in Dockerfile and checksums of files in ADD
or COPY
. If the instructions have been modified, then it will rebuild that layer.
Multi-stage builds
Multi-stage builds will help to generate smaller images. It makes the developers easy to read and maintain as you only keep the required dependencies, therefore resulting in a more secure container.
Create custom image from running container
Create custom image from running container is not recommended. Please use Dockerfile instead.
When you change files or settings inside the container, docker commit
command can be useful to commit them to a new image. Do take note that, the processes within the container will be paused when the image is being committed.
The --change
or -c
option will apply Dockerfile
instructions to the image that is created, it's supported Dockerfile
instructions.
CMD
ENTRYPOINT
ENV
EXPOSE
LABEL
ONBUILD
USER
VOLUME
WORKDIR