docker Tutorial - mirroring (IV)

First, get a mirror

        There Docker Hub on a large number of high-quality image can be used, here we say about how to obtain these images. Get command from the Docker Mirror Mirror warehouse is docker pull. The command format is:

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]

Specific options can be seen by docker pull --help command, here we talk about the format of the image name.

  • Warehouse Docker mirror address: The address is typically <domain name / IP> [: port number]. The default address is Docker Hub.
  • Warehouse name: as said before, here's the repository name is the name of a two-stage, namely <username> / <software name>. For Docker Hub, if you do not give the user name, the default for the library, which is the official image.

such as:

$ docker pull ubuntu:18.04

The above command is not given warehouse address Docker mirror, the mirror will therefore be acquired from Docker Hub. The image name is ubuntu: 18.04, and will therefore get the official image library / ubuntu warehouse 18.04 tags for the image.

Download process can be seen from the concept of tiered storage we mentioned before, is composed of a multilayer mirror storage. Download is a layer of to download, not a single file. Download process before the given ID 12 of each layer. And after the download is complete, the mirror gives a complete summary of the sha256 to ensure consistency download.
        With the image, we can start with this image-based and run a container. At the top of ubuntu: 18.04, for example, if we're going to start inside the bash and interactive operation, you can execute the following command.

$ docker run -it --rm \
    ubuntu:18.04 \
    bash
root@e7009c6ce357:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

docker run command is run container, concrete form we will explain in detail in a container, we have here a brief description about the parameters used above.

  • -it: These are two parameters, a -i: interactive operation, a terminal is -t. Here we intend to enter the bash to execute commands and return the results to see, so we need interactive terminals.
  • --rm: This parameter is to say after the vessel exit will be deleted. By default, the demand for troubleshooting, exit the container does not delete immediately, unless manually docker rm. We are here only just execute a command, look at the results, does not require troubleshooting and retention results, so use --rm avoid wasting space.
  • ubuntu: 18.04: This refers to the use ubuntu: 18.04 mirroring basis starting container.
  • bash: image name is placed after the command, here we want to have an interactive Shell, therefore using a bash.

After entering the container, we can operate under the Shell, execute any desired command. Here, we have implemented cat / etc / os-release, which is commonly used to check the current Linux version of the command, from the returned results can be seen within the container is Ubuntu 18.04.1 LTS system. Finally, we pulled out of the container through the exit.
        When we use a mirror that does not exist on the local host Docker will automatically download the image. If we want to pre-download this image, we can use the docker pull command to download it.

runoob@runoob:~# docker pull training/webapp

After the download is complete, we can directly use this image to run the container. We can search image from Docker Hub website, Docker Hub site at: https://hub.docker.com/  . We can also use the docker search command to search for images. For example, we need a mirror image of httpd as our web service. We can docker search command httpd search to find suitable for our image.


Additional: When we downloaded from the docker Mirror Mirror warehouse can not meet our needs, we can make changes to the image in two ways.

  • Update image has been created from the container, and submit this image
  • Use Dockerfile instructions to create a new image

Second, the local mirror

        When running the container, using a mirror if there is no local, the docker will automatically download from the docker image repository, the default is downloaded from Docker Hub public image source. We can use docker images mirrored to list on the local host.

Option Description:

Options Explanation
REPOSITORY It represents the repository of the source image
TAG Mirroring label
IMAGE ID Mirror ID
CREATED Mirror Created
SIZE Image size

        The same warehouse source can have multiple TAG, on behalf of the warehouse source different versions, such as ubuntu warehouse source, there are several different versions 15.10,14.04, etc., we use REPOSITORY: TAG to define different image. So, if we want to use the system image version is ubuntu 15.10 to run the container, the command is as follows:

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash

If you do not specify a mirrored version tag, for example, you only use ubuntu, docker will default ubuntu: latest image.

1, the mirror dangling

        Mirror list above, you can see a special image, this image is neither warehouse name, there is no label, are <none>. This image was originally there image name and label for the original mongo: 3.2, with the official image maintenance, released a new version, re-docker pull mongo: 3.2 when, mongo: 3.2 This mirror name was transferred to the newly downloaded image body , while the names on the old image is canceled, thus becoming <none>. In addition docker pull might cause this condition, docker build also can lead to this phenomenon. As the old and the new image of the same name, the name of the old mirror was canceled, which appeared warehouse name tags are <none> mirroring. No such label is also called dangling mirror image (after the dangling Image) , the following command can be used exclusively when such image display:

$ docker image ls -f dangling=true
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 00285df0df87 5 days ago 342 MB

Generally, dangling mirror has lost its value of existence, is free to delete, you can use the following command to delete.

$ docker image prune

2, the intermediate layer mirror

        In order to accelerate the mirror construct, recycling of resources, we will use the intermediate layer Docker mirror. Therefore, after a period of use, the intermediate layer may see some image-dependent. The default image ls list docker will only display the top image, if you want to display all image includes an intermediate layer including a mirror, then, need to add -a parameter.

$ docker image ls -a

        This will see a lot of label-free mirror, different dangling previous image, many of which are unlabeled intermediate layer mirror image, the mirror image is dependent on the other mirror. These unlabeled image should not be deleted, because otherwise it will lead to an upper Mirror dependent loss and error. In fact, these images did not need to be deleted, as I said before, will keep the same level again, and these are dependent on the mirror image of the other, and therefore they will not be listed and kept more than a, regardless how you will need them. As long as delete those dependent on their mirror image, these image-dependent intermediate layer will be deleted jointly.

3, the mirror portion List

        Without adding any parameters, docker image ls will list all top-level mirroring, but sometimes we just want to list part of the image. docker image ls has several parameters that can help to do this thing.

  • According to the repository name listed Mirror
$ docker image ls ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 f753707788c5 4 weeks ago 127 MB
ubuntu latest f753707788c5 4 weeks ago 127 MB
  • Listed a particular image, name and label that is designated warehouse
$ docker image ls ubuntu:18.04
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 f753707788c5 4 weeks ago 127 MB

In addition, docker image ls also supports a powerful filter parameters --filter, or short -f. We have seen before usage use filters to list the dangling mirror, it also has more usage. For example, we want to see in mongo: Mirror established after 3.2, you can use the following command:

$ docker image ls -f since=mongo:3.2
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5f515359c7f8 5 days ago 183 MB
nginx latest 05a60462f8ba 5 days ago 181 MB

To get a mirror before a location can be, since just need to be replaced before. Further, if the mirror constructed, the LABEL defined, may also be filtered by LABEL.

$ docker image ls -f label=com.example.version=0.1
...

4, displayed in a particular format

        By default, docker image ls will output a complete form, but not all the time we will need these elements. For example, just delete dangling mirror, we need to use docker image ls all the dangling mirror ID listed before it can be handed over to docker image rm command as an argument to delete the specified these images, this time used the -q parameter.

$ docker image ls -q
5f515359c7f8
05a60462f8ba
fe9198c04d62
00285df0df87
f753707788c5
f753707788c5
1e0c3dd64ccd
--filter produce -q ID list with a specified range, and then sent to another docker command as a parameter, so that a certain operation practices for this group of entities batch command line during use is very common in Docker, only it is a mirror, in the future we will see this kind of match in each command to complete a very powerful feature. After seeing so each document filters, you can pay more attention to their usage.

Other times, we may just not satisfied with the structure of the table, they hope to organize column; or do not want to have the title, so easy analysis results, and other programs, which use the template syntax Go's. For example, the following command will list the direct result of the mirror, and contains only a mirror ID and warehouses name:

$ docker image ls --format "{{.ID}}: {{.Repository}}"
5f515359c7f8: redis
05a60462f8ba: nginx
fe9198c04d62: mongo
00285df0df87: <none>
f753707788c5: ubuntu
f753707788c5: ubuntu
1e0c3dd64ccd: ubuntu

Or intended to form isometric display, and has a header row, and by default the same, but their definition of columns:

$ docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID REPOSITORY TAG
5f515359c7f8 redis latest
05a60462f8ba nginx latest
fe9198c04d62 mongo 3.2
00285df0df87 <none> <none>
f753707788c5 ubuntu 18.04
f753707788c5 ubuntu latest

Third, remove the local mirror

        If you want to delete the local mirror, you can use docker image rm command in the format:

$ docker image rm [选项] <镜像1> [<镜像2> ...]

Wherein <image> ID may be a mirror short, long ID mirror, or mirror image name summary. For example, we have so few mirror:

$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0584b3d2cf6d 3 weeks ago 196.5 MB
redis alpine 501ad78535f0 3 weeks ago 21.03 MB
docker latest cf693ec9b5c7 3 weeks ago 105.1 MB
nginx latest e43d811ce2f4 5 weeks ago 181.5 MB

We can use the complete ID mirroring, also called long ID, to remove the mirror. When using a script with a long ID may be, but the manual input too tired, so more time is short ID to delete the image. docker image ls default is already listed in the short ID, and generally above the first three characters, on the other mirror long enough to distinguish it. For example, here, if we want to remove redis: alpine mirror, you can do:

$ docker image rm 501
Untagged: redis:alpine
Untagged: redis@sha256:f1ed3708f538b537eb9c2a7dd50dc90a706f7debd7e1196c9264edeea521a86d
Deleted: sha256:501ad78535f015d88872e13fa87a828425117e3d28075d0c117932b05bf189b7
Deleted: sha256:96167737e29ca8e9d74982ef2a0dda76ed7b430da55e321c071f0dbff8c2899b
Deleted: sha256:32770d1dcf835f192cafd6b9263b7b597a1778a403a109e2cc2ee866f74adf23
Deleted: sha256:127227698ad74a5846ff5153475e03439d96d4b1c7f2a449c7a826ef74a2d2fa
Deleted: sha256:1333ecc582459bac54e1437335c0816bc17634e131ea0cc48daa27d32c75eab3
Deleted: sha256:4fc455b921edf9c4aea207c51ab39b10b06540c8b4825ba57b3feed1668fa7c7

We can also use the mirror name is <repository name>: <tag> to remove the mirror.

$ docker image rm centos
Untagged: centos:latest
Untagged: centos@sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366c
Deleted: sha256:0584b3d2cf6d235ee310cf14b54667d889887b838d3f3d3033acd70fc3c48b8a
Deleted: sha256:97ca462ad9eeae25941546209454496e1d66749d53dfa2ee32bf1faabd239d38

Of course, a more accurate summary of a mirror to remove the mirror.

$ docker image ls --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
node slim sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228 6e0c4c8e3913 3 weeks ago 214 MB

$ docker image rm node@sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228
Untagged: node@sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228

Untagged 和 Deleted

     If you look at the output of the above information run these commands, you will notice delete behavior is divided into two categories, one is Untagged, the other is Deleted. We introduced before, that uniquely identifies the image is its ID and summary, and a mirror can have multiple labels.

     So when we use the above command to remove the mirror, the mirror is in fact requested the removal of a label. So first of all need to do is to satisfy all our requirements mirror tags have been removed, this is what we see Untagged information. Since a mirror can correspond to multiple labels, so when we removed the specified label, the label may have other points to this image, if this is the case, then the Delete behavior does not occur. So not all docker image rm behavior will have to delete the image, there may only be canceled just a label.

     In addition to image-dependent, the container should also be noted that the dependence on the image. If there is a useful container this image start (even if the container is not running), then the same can not delete this image. Before mentioned, the container is a mirror-based, storage vessel coupled with one layer, consisting of such a multilayer structure to run the memory. Thus if the image of the container depends, will inevitably lead to failure is deleted. If the container is not needed, you should delete them first, then come back to delete the image.

        Like other entities may undertake multiple commands like, you can use docker image ls -q to use with docker image rm, so you can batch delete the image you want to delete. We introduced many ways mirrored filter list can take over in "list of mirrors" section. For example, we need to remove the mirror of all warehouse called redis:

$ docker image rm $(docker image ls -q redis)

Or remove all mongo in: 3.2 before the mirror:

$ docker image rm $(docker image ls -q -f before=mongo:3.2)

Fourth, build a mirror

        We use the command docker build, from scratch to create a new image. To this end, we need to create a Dockerfile file, which contains a set of instructions to tell Docker how to build our image.

FROM centos:6.7
MAINTAINER Fisher "[email protected]"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd docker
RUN /bin/echo 'docker:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D

        Each command will create a new layer on the mirror, prefix each command must be capitalized. The first FROM, specify which mirror source use; RUN command tells docker execute commands in the mirror, what is installed ......

        We then use Dockerfile file, to build a mirror by docker build command.

docker build -t docker/centos:6.7 .

Parameter Description:

  • -t: Specifies the name of the target image to be created
  • .: Dockerfile file directory, you can specify the absolute path Dockerfile

Docker images created using view mirror already exists in the list

Here we can use a mirror to create a new container

runoob@runoob:~$ docker run -t -i docker/centos:6.7 /bin/bash

From the image above to see a new docker already contains the user we created.

Five, arranged mirror tag

        We can use docker tag command, add a new label to the mirror.

runoob@runoob:~$ docker tag a575d5581987 docker/centos:dev

Command using the docker images can be seen, ID of a multi-mirror 860c279d2fec label.

 

Published 107 original articles · won praise 184 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_22172133/article/details/85242941