Previous review:

  • First Docker and install | Docker series

Today, mirror is a very important concept in Docker, which is the basis of container operation. Without mirror, everything else is not true.

A typical mirror representation is divided into three parts, separated by / :

remote image hub/namespace/name:tag
Copy the code
  • Remote Image Hub: Web server address for storing images.
  • Namespace: indicates the namespace of all images under a user or organization.
  • Name: indicates the image name.
  • Tag: indicates the mirror tag.

In fact, we often see the image with the name:tag, because the image pulled down from the official Docker warehouse can omit the first two parts.

Access to the mirror

Use the docker pull name[:tag] command to download the image. If no tag is explicitly specified, the latest tag is selected by default.

$ docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
76df9210b28c: Pull complete
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
Copy the code

Viewing Mirror Information

Use the Docker images command to list native images.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              1c35c4412082        6 days ago          1.22MB
Copy the code

The image ID is important because it uniquely identifies the image.

Use the docker tag command to add new tags to the local image.

$ docker tag busybox:latest mybusybox:latest

$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE Busybox latest 1C35C4412082 6 days ago 1.22MB Mybusybox latest 1C35C4412082 6 days A line 1.22 MBCopy the code

As you can see, the two local mirrors now have the same ID, which means they refer to the same image with different labels.

Use the docker inspect command to get the details of the image.

Use the docker history command to list the creation information for each layer of the image.

Search the mirror

Use the Docker search command to search for images.

$ docker search centosNAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6039 [OK] ansible/centos7-ansible Ansible On Centos7 130 [OK] centos/centos-xfCE-VNC centos container with "headless" VNC session 116 [OK] jdeathe/centos-ssh OpenSSH/Supervisor/EPEL/IUS/SCL Repos -... 114 [OK] centos/mysql-57-centos7 mysql 5.7 SQL Database Server 76 Imagine10255 /centos6-lnmp-php56 Centos6-lnmp-php56 [OK]Copy the code

Delete and clear mirrors

Docker rmI command is used to delete the image. There are two ways: one is to delete the image name and label; One is to delete by the mirror ID. In normal use, most of them are deleted by the mirror ID.

$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE Busybox latest 1C35C4412082 8 days ago 1.22MB Mybusybox latest 1C35C4412082 8 days A line 1.22 MB
$ docker rmi mybusybox:latest
#or
$ docker rmi 1c35c4412082
Copy the code

If an image is referenced by a container, it cannot be deleted. You need to delete the container that depends on the image before deleting the image. Or if it is more violent, using the -f parameter to delete can also achieve the effect, but this is not recommended.

Use the Docker image prune command to clean up temporary images left in the system, as well as unused images.

Create a mirror image

There are three ways to create an image:

  • Created based on an existing container
  • Import based on local templates
  • Create from Dockerfile

1. Create from an existing container

#Running a container
$ docker run -it centos /bin/bash

#Create a file in the container and exit
[root@f0767e2e8964 /]# touch text.txt
[root@f0767e2e8964 /]# exit
exit

#Check the container
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
f0767e2e8964        centos              "/bin/bash"         17 seconds ago      Exited (0) 4 seconds ago                       stupefied_ptolemy

#Create images based on containers
$ docker commit -a 'add file'F0767e2e8964 centos: 1.0
sha256:a651491d9bfe6d00eef7a23bd290be839d59efafa31183ef2038399271dee459

#You can view not only the original image but also the newly generated image with different labels
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              1.0                 a651491d9bfe        4 seconds ago       237MB
centos              latest              470671670cac        4 months ago        237MB
Copy the code

2. Import based on local templates

Use the docker import command to import the template as an image.

3, based on Dockerfile create

This is the most used method in practical work, first sell in suspense, subsequent separate to write a detailed introduction.

Save and load images

Use the docker save command to save the image to a file.

$ docker save -o busybox.tar busybox
Copy the code

After you execute, you’ll have the busybox.tar file in your current directory, which you can then share with others.

After receiving the file, use the Docker load command to load the image.

$ docker load < busybox.tar
Copy the code

Upload the image

Use the Docker push command to upload the image to the image repository, so that you can use the image on other servers, directly docker pull, very convenient.

By default, it will be uploaded to the official Docker Hub warehouse, and we can also build our own private warehouse. Generally speaking, the company will have its own mirror warehouse, we can use according to the demand.

So that’s it for this post, and we’ll talk about containers in the next post.

Reference Books:

  • Introduction and Actual Combat of Docker Technology
  • Docker Advanced and Combat

Past highlights:

First Docker and install | Docker series

Tech Blog:

Github.com/yongxinz/te…

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.