[Cloud Native|Docker Series Part 3] Getting Started with Docker Mirroring

insert image description here

Welcome to the third blog in the Getting Started with Docker series! In the previous two blogs, we have seen what is Docker and how to install and configure it. This blog will focus on the concept of Docker images and the relationship between them. We will also learn how to pull, create, manage and share Docker images, which is one of the important steps in using Docker.

A Docker image is one of the core concepts of Docker, which provides an executable package that contains everything needed to run an application: code, runtime environment, system tools, libraries, and dependencies.

By learning the concept of a Docker image, you'll be able to better understand how Docker works and take advantage of the powerful capabilities Docker provides to develop, test, and deploy applications. Next, let's dive into the world of Docker images!

1. Docker image

A Docker image is the core building block of Docker, a lightweight, executable package for building and running containerized applications. A Docker image contains everything needed to run an application, including the application's code, runtime environment, system tools, libraries, and dependencies.

1.1 Features of Docker image

  • Lightweight : The Docker image adopts a layered storage mechanism, and the same files can be shared between different layers, thereby reducing the storage space occupied.
  • Portability : Docker images have good portability and can run on different hosts and environments without paying attention to differences in the underlying operating systems.
  • Version control : Each Docker image has a unique identifier, called the image ID, which can be used for version control and management of the image.
  • Reusability : By building new images based on existing Docker images, the reuse and expansion of images can be realized.

1.2 Acquisition of Docker image

There are usually two ways to obtain a Docker image: pull an image from a public image repository, or create a custom image.

1.2.1 Pull image

Docker images can be pulled from public mirror warehouses, the most famous of which is Docker Hub. You can find tens of thousands of official and community-maintained Docker images on Docker Hub. You can use docker pullthe command to pull the image, the command format is as follows:

docker pull <镜像名称>:<标签>

For example, to pull the official Ubuntu mirror:

docker pull ubuntu:latest

insert image description here

1.2.2 Build an image using Dockerfile

1.3 Docker image management

When using Docker, we may need to manage images, including viewing the image list, deleting images, exporting and importing images, and so on.

1.3.1 View mirror list

To view a list of existing mirrors on the local host, you can use docker imagesthe command. This command will display information such as the image's warehouse name, label, image ID, creation time, and size.

docker images ls

insert image description here

1.3.2 Delete image

To delete one or more mirrors on the local host, you can use docker rmithe command. You need to specify the image ID or warehouse name and label of the image to be deleted.

docker rmi <镜像ID或名称>:<标签>

1.3.3 Export and import images

Sometimes, we may need to export an image to a file, or import an image from a file. You can use docker savethe command to export the image to a tar file, and then use docker loadthe command to import the image from the tar file.

Export image:

docker save -o <导出文件名.tar> <镜像名称>:<标签>

Import image:

docker load -i <导入文件名.tar>

2. Use Dockerfile to customize the image

2.1 Dockerfile custom image

The customization of the image is actually to customize the configuration and files added by each layer. If we can write each layer of modification, installation, construction, and operation commands into a script, and use this script to build and customize the image, then the aforementioned problems that cannot be repeated, the problem of image construction transparency, and the size of the Problems will be solved. This script is the Dockerfile.
Dockerfile is a text file, which contains a series of instructions (Instruction), each instruction builds a layer, so the content of each instruction is to describe how the layer should be built.
Take the example we just customized the nginx image, we create a text file in a blank directory and name it Dockerfile:

$ mkdir mynginx
$ cd mynginx
$ touch Dockerfile

insert image description here
Its content is:

FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

The command will be described in detail later. We use it to build a mirror. The format of the dockerfile build mirror command is as follows:

docker build -t <镜像名称>:<标签> <Dockerfile路径>

You can then build the image with the following command:

docker build -t myimage:latest .

insert image description here
Then we run this image again, you can choose to run it directly from the command line, or run it directly on the docker client. Here, for the convenience of understanding, I use the docker client to run this image: just fill in the port
insert image description here
:
insert image description here

Open a web page to access this port:
insert image description here

2.2 Dockerfile instructions

2.2.1 COPY to copy files

The format is as follows:

COPY [--chown=<user>:<group>] <源路径>... <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",... "<目标路径>"]

The COPY instruction will copy files/directories from <source_path> in the build context directory to <destination_path> within the new layer's mirror. for example:

COPY package.json /usr/src/app/

<source path> can be multiple, or even a wildcard, and its wildcard rules must meet Go's filepath.Match rules, such as:

COPY hom* /mydir/
COPY hom?.txt /mydir/

2.2.2 FROM specifies the base image

The so-called customized image must be based on an image and customized on it. Just like we ran an nginx mirrored container before and then modified it, the base mirror must be specified. And FROM is to specify the base image, so FROM in a Dockerfile is a necessary instruction and must be the first instruction.
There are a lot of high-quality official images on Docker Hub, including service images that can be used directly, such as nginx, redis, mongo, mysql, httpd, php, tomcat, etc.; there are also some that are convenient for development, construction, and operation Mirror images of various language applications, such as node, openjdk, python, ruby, golang, etc. We can find an image that best meets our final goal as the base image for customization.

The format is as follows:

FROM <镜像名字>

like:

FROM nginx

2.2.3 RUN execute command

The RUN command is used to execute command line commands. Due to the power of the command line, the RUN command is one of the most commonly used commands when customizing images. It has two formats:

  • Shell format: RUN <command>, just like a command entered directly on the command line. The RUN instruction in the Dockerfile just written is in this format.
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
  • exec format: RUN ["executable file", "parameter 1", "parameter 2"], which is more like the format in a function call.

2.2.4 ADD more advanced copy files

The format and nature of the ADD instruction and COPY are basically the same. But some functions are added on the basis of COPY.
For example, <source path> can be a URL. In this case, the Docker engine will try to download the file from this link and put it into <target path>. The downloaded file permission is automatically set to 600. If this is not the desired permission, an additional layer of RUN is needed to adjust the permission. In addition, if the downloaded file is a compressed package, it needs to be decompressed, and an additional A layer of RUN instructions are decompressed. So it is better to use the RUN command directly, and then use the wget or curl tool to download, process permissions, decompress, and then clean up useless files. Therefore, this feature is not practical and is not recommended.
For example:

FROM scratch
ADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz /

2.2.5 CMD container start command

Specify the command to be executed when the container starts. There can be multiple CMDs, but only the last one takes effect.

CMD ["python", "app.py"]

2.2.6 WORKDIR Set the working directory

Set the working directory, and subsequent instructions will be executed under this directory.

WORKDIR /app

2.2.7 EXPOSE exposed port

Declare the port that the container needs to listen to when running.

EXPOSE 80

2.2.8 ENV Set environment variables.

ENV APP_VERSION=1.0

3. Share and push Docker images

Sharing and pushing Docker images is an important part of teamwork and community sharing. You can share the images you create with other developers, and you can also push the images to public or private mirror repositories.

3.1 Share Docker image

To share a Docker image, it can be done by exporting and importing. First, use docker savethe command to export the image into a tar file, then share the file with others via email, file sharing, etc.

docker save -o <导出文件名.tar> <镜像名称>:<标签>

The recipient can use docker loadcommands to import images from tar files.

docker load -i <导入文件名.tar>

3.2 Push Docker image

If you want to push the image to Docker Hub or other mirror warehouse, you can use docker pushthe command. Before pushing an image, you need to log in to the image repository.

docker login

Then, use docker pushthe command to push the image to the repository.

docker push <镜像名称>:<标签>

Summarize

In this blog post, we delved into the concept of Docker images. We learned how to acquire and manage Docker images, including pulling images, building custom images, and exporting and importing images. We also introduced how to make images through Dockerfile and the instructions of Dockerfile. Finally, we saw how to share and push Docker images so we can share and collaborate with others.

By learning and mastering the basic concepts of Docker images, you will be able to better utilize Docker to develop, test, and deploy applications. The emergence of containerization technology makes the development and deployment of applications more flexible and efficient, and brings many advantages. In the next blog, we will continue to explore more functions and application scenarios of Docker. In the next article, we will enter the introductory practice of Docker containers.

I hope this blog has played a good guiding role in your understanding of Docker images. Keep exploring the world of Docker, you will discover more exciting features and applications!

Guess you like

Origin blog.csdn.net/A_D_H_E_R_E/article/details/131714197