This article was first published on wechat public account: Xiaoxue Java
As one of the most important keywords in the three core concepts of Docker, image has many operations that you have to master if you want to learn container technology. This article will take you step by step, both graphic and hands-on operation to learn it.
Directory:
- Docker download image
- 1.1 Downloading an Image
- 1.2 validation
- 1.3 Downloading Image Details
- 1.4 PULL subcommand
- Docker view image information
- The 2.1 images command lists the images
- 2.2 Using the tag command to add a tag to an image
- 2.3 Run the inspect command to view details about an image
- 2.4 Using the history command to view the history of a mirror
- Docker search image
- 3.1 the search command
- 3.2 Search subcommand
- Four,
Docker download image
If we want to run the container locally, we must ensure that the corresponding mirror exists locally. So, step one, we need to download the image. When we try to download the image, Docker will try to download it from the default image repository (the default Docker Hub public repository). Of course, users can also customize the image repository they want to download.
1.1 Downloading an Image
We can use docker pull [IMAGE_NAME]:[TAG] to download the image, where IMAGE_NAME represents the image name and TAG is the image TAG. That is to say, we need to download the image by “image + tag” mode.
Note: You can also specify a TAG without explicitly, and it will download the latest TAG by default, which is the latest version of the image in the repository. Downloading the Latest TAB is not recommended because the contents of the image track the latest version of the image and change accordingly, so it is not stable. In a production environment, it is recommended that you specify a specific TAG instead of displaying it.
For example, if we want to download a Mysql 5.7 image, we can use the following command:
Docker pull mysql: 5.7Copy the code
You’ll see console output like this:
Note: Because the official DockerHub warehouse server is in foreign countries, the download speed is slow, so I changed the address of the warehouse to the domestic docker. IO mirror warehouse, so in the figure above, there will be docker. IO in front of the image.
If a string is displayed in the Downloaded file, the download is successful.
1.2 validation
To verify that a local image of Mysql5.7 exists, run the following command:
docker images
Copy the code
You can see that the local image does exist, indeed is a successful download!
1.3 Downloading Image Details
Here’s how to download the image:
Through the download process, it can be seen that an image is generally composed of multiple layers, such as f7e2B70D04AE string represents the unique ID of the layer.
PS: In fact, the complete ID contains 256 bits, 64 hexadecimal characters.
You might think that if multiple different mirrors contain the same layer (layer
), this repeated download, does not lead to storage space waste?
In fact,Docker is not stupid enough to download duplicate layers. Before downloading,Docker will check whether there is a local layer with the same ID. If the local layer already exists, it will use the local layer directly.
Another problem is that in different warehouses, mirror names may also occur. What should we do in this case?
Strictly speaking, when using the Docker pull command, we also need to specify the repository address (Registry) in front of the image. Otherwise, Docker will use your default repository address. For example, as I configured the domestic docker. IO warehouse address, when I pull, Docker will add the prefix docker. IO /library for me by default.
IO /mysql:5.7, if you do not have a custom configuration repository, then by default when downloading, you will add DockerHub in front of the image address.
Docker ensures the uniqueness of mirror names in different warehouses through different prefix addresses.
1.4 PULL subcommand
Enter the following information in the command line:
docker pull --help
Copy the code
You get the following information:
[root@iZbp1j8y1bab0djl9gdp33Z ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--help Print usage
Copy the code
We can see that the main supported subcommands are:
-a, --all-tags=true|false
: Indicates whether to obtain all mirrors in the repository. The default value is no.--disable-content-trust
: skips the verification of the mirror content. The default value is true.
Docker view image information
The 2.1 images command lists the images
To list the existing images on the machine, run the following two commands:
docker images
Copy the code
Or:
docker image ls
Copy the code
As shown below:
Here are some explanations for the fields highlighted in red above:
- REPOSITORY: from which REPOSITORY;
- TAG: indicates the TAG information of a mirror. For example, 5.7 and latest indicate different versions.
- IMAGE ID: the ID of the IMAGE. If you see two ids that are exactly the same, they actually refer to the same IMAGE with different label names.
- CREATED: time when the image was last updated;
- SIZE: The SIZE of an image. Good images tend to be small, which is why I prefer the lightweight Alpine version;
Note: The image size information in the figure is only logical. An image is composed of multiple image layers, and only one copy of the same image layer is stored locally. Therefore, the physical storage space occupied may be smaller than the logical size.
2.2 Using the tag command to add a tag to an image
In general, we can use the docker tag command to add a new tag to the local image in order to quickly find the image later. As shown below:
Add new image tag allen_mysql:5.7 for docker. IO /mysql image. Then use the docker images command to view the local image:
Allen_mysql :5.7; Allen_mysql :5.7 and docker. IO /mysql:5.7 have the same image ID, but the alias is different.
The Docker tag command functions more like adding a shortcut to a given image.
2.3 Run the inspect command to view details about an image
With the Docker inspect command, we can get the details of the image, including the creator, the digital summary of each layer, etc.
Docker inspect docker. IO/mysql: 5.7Copy the code
Docker inspect returns information in JSON format. If you want to obtain one of the specified contents, you can specify it by -f, such as obtaining the image size:
docker inspect -f {{".Size"}} docker. IO/mysql: 5.7Copy the code
2.4 Using the history command to view the history of a mirror
In the previous section, we learned that a mirror is composed of multiple layers, so how do we know the specific contents of each layer?
Docker. IO /mysql:5.7
docker historyDocker. IO/mysql: 5.7Copy the code
If you want to see specific information, you can add –no-trunc, as shown in the following command:
docker history- no - trunc docker. IO/mysql: 5.7Copy the code
Docker search image
3.1 the search command
You can search by running the following command:
docker search [option] keyword
Copy the code
For example, if you want to search for mysql-related images in the repository, enter the following command:
docker search mysql
Copy the code
3.2 Search subcommand
Docker search –help docker search –help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f. --filter filter Filter output based on conditions provided --help Print usage --limit int Max number of search results (default 25) --no-index Don't truncate output --no-trunc Don't truncate output
Copy the code
As you can see, search supports the following subcommands:
-f, --filter filter
: Filter output content;--limit int
: Specifies the number of search contents to be displayed.--no-index
: Do not truncate the output;--no-trunc
: Do not truncate the output;
For example, if we want to search for an official mysql image, run the following command:
docker search --filter=is-offical=true mysql
Copy the code
For example, we want to search for mysql images with Stars greater than 100:
docker search --filter=stars=100 mysql
Copy the code
Four,
In this article, we focus on learning related operations of downloading images, viewing image information and searching images in Docker. This article is the first, the next article, will lead you to learn more mirror operations, also wish you a happy learning!
Gifts | interview & learning welfare resources
Recently found a good PDF resource on the Internet “Java core interview knowledge. PDF” to share with you, not only interview, learning, you are worth having!!
How to obtain: pay attention to the public account: Xiahalearning Java, background reply resources, both can obtain the resource link, the following is the directory and some screenshots:
Important things to say twice, access: pay attention to the public number: ha learning Java, background reply resources, both can get resource links!!