Overview of the image

An image is a read-only template used to create containers. Typically, it includes some additional customizations.

For example, we could build an image based on rocky operating system that directly includes some applications like Nginx, Apache or other programs during the image build process.

Once the image is built, we could directly launch containers based on it to quickly achieve the desired business functionality to ensure consistent behavior across different environments.

Sources of the image

  • Pull from the image repository(public:Docker Hub/private: harbor,quay,etc)

    Registry is a stateless, highly scalable service that allows us to store and distribute images. Registry is an open source service licensed under the Apache License.

    The public image repository is a cloud-based registry service.

    Compared with the public image repository, the private repository has these merits:

    • We could strictly control where images are stored.
    • We have a mirror distribution channel that belongs entirely to us.
    • We could tightly integrate image storage and distribution into internal development workflows.
  • Customize and build manually from Dockerfile

Build images manually

There are two methods to build image.

  • docker commit based on original containers

We could modify the applications/services/environments and something else based on the original dokcer image in the containers. After that, we could use docker commit command to export a new image from containers.

  • docker build by using Dockerfile

Build the image you need from scratch. At the beginning of creating the image, set the various settings and requirements you need.
Various applications are included, and the generated images can be directly used for business deployment.

Dockerfile high frequency instruction set

Command Function Format
FROM Specify the base image FROM image:tag
MAINTAINER Specify the image author MAINTAINER name
RUN Excute the Specified command RUN command
ADD Copy files from build context copied into mirror ADD [–chown=user:group]src…dest
COPY Copy files from build context copied into mirror COPY -chown=user:groupsrc…dest
ENV Set the environmental variables ENV key value
EXPOSE Specify the listen port for applications EXPOSE port [port/protocol]
USER Specify the user at boot USER user
CMD Specify the command or scripts at boot CMD command param1 param2
ENTRYPOINT Specify an excutable script or program path ENTRYPOINT command param1 param2
VOLUME Declare files or directories as volume and mount to containers VOLUME [“/data”]
WORKDIR Specify the current working directory WORKDIR /path/to/workdir

You could use all Dockerfile commands to develop the Dockerfile.

After coding, you should use this command to build an image.

The default name of dockerfile is Dockerfile, you could name it as what you want. However, you should specify the file name when you build an image.

The dot represents the context, you also could specify the path as what you want here.

If you do not specify the tag name, the default name of tag is latest. So, please do not misunderstand and remember to set the appropriate name for easily understand!

1
docker build (-f Dockerfile_name) -t image_name:tag_name .(/path/to/context)

Using an image to create containers

There are two methods to create containers.

  • docker run

This command can only start one container at a time.

1
docker run -d -p 8000:80 customized_image:v1
  • docker compose up -d

You must develop a docker-compose.yaml file first, and then change the directory to it. By the way, a container that remain running only if it has a process that sustains its execution.

docker-compose.yaml is an inventory listing all needed services, it is so convenient that you could start more than one container at a time.

Creating the docker-compose.yaml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# change the directory into the docker-compose file
cd ~/docker
# create docker-compose.yml file
vim docker-compose.yaml
# specify Docker Compose file version as 3.8
version: '3.8'
# Define services,for example: "build"
services:
build:
# specify the based on what image to build
image: nginx:latest
# specify the container name as build
container_name: build
# Start the tty console terminal for interacting
tty: true
# specify auto-restart when container crashed
restart: always
# configure the volumes to mount into containers
volumes:
- ./src/:/root/nginx/