docker image

Image (image) is the most important of the three core concepts of docker. Before docker runs a container (container), a corresponding image (image) needs to exist locally. If there is no corresponding image locally, docker will try to download it from the default image repository ( By default, the repository in the Docker Hub public registration server is used), and users can also use a custom image repository through configuration.

 

get mirror

Images are the prerequisite for running containers, and the docker hub website has provided hundreds of thousands of images for everyone to download and use.

The command format for obtaining (downloading) an image is as follows: docker pull NAME[:TAG]

Among them, NAME is the name of the mirror warehouse (used to distinguish the mirror), just like the name of the git warehouse; TAG is the label of the mirror (generally used to indicate version information), just like the corresponding version number in the git warehouse, if the specified TAG is not displayed, The last tag is selected by default, that is, the latest version of the mirror in the mirror warehouse is downloaded. Therefore, in the case of the same mirror repository server, it can be said that NAME + TAG can uniquely identify a mirror.

For example, to get a base image of an ubuntu 14.04 system, use the following command:

For example, to get an up-to-date Ubuntu OS image, use the following command:

In fact, docker pull ubuntu is equivalent to docker pull ubuntu:latest.

In addition, strictly speaking, the warehouse address (registry, registration server) should also be added as a prefix to the warehouse name of the image, but here we use the docker hub service by default, and the prefix can be ignored. The previous command: docker pull ubuntu:14.04 is equivalent to docker pull registry.hub.docker.com/ubuntu:14.04, which is to obtain the ubuntu repository and the image marked as 14.04 from the default registration server docker hub registry. Of course, you can download images from some unofficial warehouse servers, such as NetEase Honeycomb's mirror source to download the ubuntu 14.04 image, the command is as follows: docker pull hub.c.163.com/public/ubuntu:14.04.

 

View mirror

Use the docker images command to list basic information about existing images on the local host.

As shown in the figure above, the red area is the image obtained earlier, including ubuntu:14.04, ubuntu:latest, and images such as hub.c.163.com/public/ubuntu:14.04 downloaded from the NetEase Honeycomb mirror source.

1. Repository, which indicates which repository it comes from, for example, the ubuntu repository is used to save the basic image of the ubuntu system;

2. Tag, which means the image tag, which is equivalent to the version number;

3. Image ID, the ID number of the image, uniquely identifies the image;

4. Created, indicating the last update time of the image;

5. Size, which indicates the size of the image. Generally, good images tend to be smaller in size; the image size generally refers to the logical volume of the image. In fact, only one copy of the same image layer is stored locally, so it physically occupies The storage space will be less than the sum of the logical volumes of each image.

 

Use the docker tag command to add any new tags to the local image, for example, add a new myubuntu:latest image tag to the ubuntu:latest image: docker tag ubuntu:latest myubuntu:latest 

As shown in the figure above, the image ids of the new tags myubuntu:latest and ubuntu:latest are the same, indicating that they both point to an image file, and myubuntu:latest just acts as a similar link.

 

Use the docker inspect command to get the detailed information of the image, including the manufacturer, adaptation architecture, digital summaries of each layer, etc. For example, to view the detailed information of the ubuntu:14.04 image, docker inspect ubunut:14.04

 

search mirror

Use the docker search command to search for images shared in remote repositories. By default, images in official repositories are searched. For example, to search for an image with nginx keyword with a rating of 50+ stars, docker search -s 50 nginx, the default output result is sorted in descending order according to the star rating.

 

delete mirror

Use the docker rmi command to delete a local image, format: docker rmi <tag name> or <image id>. There are multiple tags in the image file. Deleting an image based on a tag will only delete the tag and will not affect the image file. However, if there is only one tag in the image file, deleting the image based on the tag will delete the image file. In addition, deleting the image based on the image id will delete the image file directly.

Currently, the ubuntu:latest image has three labels, namely ubuntu, myubuntu, and myubuntu2.

Delete the local image according to the label name, docker rmi myubuntu2. It can be seen that there are also ubuntu, myubuntu tags.

Use the image id method to delete the local image, docker rmi 452a96d81c30. Where 452a96d81c30 is the ubuntu:latest image ID number.

 

It can be seen that when there are multiple tags in the image file, it will prompt that there are multiple tags. You can use the dokcer rmi -f 452a96d81c30 command to force deletion, which is not recommended.

In addition, if the container created by this image exists, the image file cannot be deleted by default. It can also be deleted by forced deletion, but this is not recommended. The normal processing is to delete all containers that depend on the image first, then delete the image, and then explain and operate the subsequent container part.

 

Create an image

At present, there are three main ways to create images, namely: container creation based on existing images; import based on local templates; creation based on Dockerfile (detailed later).

1. Create a container based on an existing image

Use the docker commit command. The command format is docker commit [OPTIONS] container [Repository[:Tag]]. The optional parameters are as follows:

-a, --author="": author information;

-c, --change=[]: Execute the Dockerfile command when submitting;

-m, --message="": commit message;

-p, --pause=true: Pause the container running on commit.

First, start an image generation container, and then create a test file in the container;

At this point, the container has changed compared with the original ubuntu:14.04 image. Use the docker commit command to submit a new image, docker commit -a "limin" -m "add a new file" 8a7a1a9c1a62 test:0.1, where 8a7a1a9c1a62 is the ID number of the container.

2. Import using local template

Use the docker import command to directly import an image from an operating system template file. The command format is: docker import [OPTIONS] file | URL | [Repository[:Tag]]. Create an image from the template provided by OpenVZ , download ubuntu-14.04-x86_64-minimal.tar.gz, and use the command cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04.01.

 

Save and load images

1. You can use the docker save command to export the image to a local file, so that you can share the locally stored image file with others, docker save -o ubuntu_14.04.tar ubuntu:14.04, and store the ubuntu:14.04 image file as a file ubuntu_14.04.tar. The generated ubuntu_14.04.tar file is in the current directory.

2. You can use the docker load command to import the exported tar file into the local mirror library, docker load --input ubuntu_14.04.tar or docker load < ubuntu_14.04.tar.

 

upload image

You can use the docker push command to upload the image to the warehouse. By default, it will be uploaded to the official docker hub warehouse (account and password login is required). The command format is: docker push NAME[:TAG] | [Registry_host[:registry_port]/]NAME[:TAG]

To upload the local image ubuntu:14.04, you can first add a new label lim1208/ubuntu:14.04, where lim1208 is the username of my docker hub, and then use the docker push command to upload the image.

Note: Before pushing the image, use the docker login command to log in to your docker hub account information, otherwise the error message unauthorized: incorrect username or password will be reported.

Log in to docker hub and you can see the image repository we just uploaded.

This is the description of the docker image, and the knowledge points related to the docker container will be explained later.

Note: This article refers to the second edition of "Docker Technology Introduction and Practice".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306017&siteId=291194637