Image usage of docker Foundation

Time:2020-4-25

When running the container, if the image used does not exist locally, docker will automatically download it from the docker image warehouse. By default, it is downloaded from the docker hub public image source.

Let’s learn:

  • 1. Manage and use local docker host image
  • 2. Create a mirror

List mirrors
We can usedocker imagesTo list the mirrors that exist on the host.

 [email protected]:~# docker images
 REPOSITORY         TAG        IMAGE ID     CREATED      SIZE
 alpine           latest      055936d39205   3 weeks ago    5.53MB
 mysql           5.7        7faa3c53e6d6   3 weeks ago    373MB
 ubuntu           15.04       d1b55fd07600   3 years ago    131MB
 ubuntu           16.04       2a697363a870   2 weeks ago    119MB

Description of each option:

  • REPOSITORY: represents the warehouse source of the image
  • TAG: label of the image
  • IMAGE ID: image ID
  • CREATED: image creation time
  • SIZE: mirror size

The same warehouse source can have multiple tags, which represent different versions of the warehouse source. For example, in Ubuntu warehouse source, there are multiple versions such as 16.04 and 15.04. We use reposition: tag to define different images.

Start the container with the corresponding image
If we want to use the Ubuntu system image of version 16.04 to run the container, the command is as follows:

 [email protected]:~# docker run -it ubuntu:16.04 sh

If you want to use Ubuntu system image version 15.04, the command is as follows:

[email protected]:~# docker run -it ubuntu:15.04 sh

Analysis of each parameter:

  • docker: the binary execution file for docker.
  • run: combined with the previous docker to run a container.
  • -it: it is actually composed of two parameters, – I and – t, – I: allow you to interact with standard input (stdin) in the container. -t: Specify a pseudo terminal or terminal in the new container.
  • ubuntu:15.04Specifies the image to run. Docker first looks for the existence of the image from the local host. If not, docker will download the public image from the docker hub of the image warehouse.
  • sh: Execute command.

Get a new image
When we use a nonexistent image on the host, docker will automatically download the image. If we want to download this image in advance, we can use thedocker pullCommand to download it.

 [email protected]:~# docker pull ubuntu:18.04
 18.04: Pulling from library/ubuntu
 6abc03819f3e: Pull complete
 05731e63f211: Pull complete
 0bd67c50d6be: Pull complete
 Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
 Status: Downloaded newer image for ubuntu:18.04

After downloading, we can use this image to run the container directly.

Find image

We can search image from docker hub website, which is https://hub.docker.com/
We can also usedocker searchCommand to search for images. Like we need ahttpdAs our web service. We can go throughdocker searchCommand searchhttpdTo find the right image for us.
Image usage of docker Foundation

NAME: name of the image warehouse source
DESCRIPTION: description of the image
OFFICIAL: official release of docker or not

Pull httpd image
We decided to use the official version of httpd image in the figure above, using the commanddocker pullTo download the image.

[email protected]:~# docker pull httpd
 Using default tag: latest
 latest: Pulling from library/httpd
 743f2d6c1f65: Already exists
 c92eb69846a6: Pull complete
 2211b052800a: Pull complete
 aed180197314: Pull complete
 7c472a4980a7: Pull complete
 Digest: sha256:a35ad614c1ffc6fe931f12dc42b682edbdcc62cf78d8edc41499dd90ef0f8003
 Status: Downloaded newer image for httpd:latest

After downloading, we can use the image.

create mirror
When the image we download from the docker image warehouse cannot meet our needs, we can change the image in two ways.

  • 1. Update the image from the created container and submit the image
  • 2. Use the dockerfile command to create a new image

Update image
Before updating the image, we need to use the image to create a container.

[email protected]:~# docker run -it ubuntu:16.04 sh

Use the apt get update command to update within the running container.

 # apt-get update
 Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
 Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
 Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
 Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
 Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
 Get:6 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844 kB]
 Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
 Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
 Get:9 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
 Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB]
 Get:11 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]
 Get:12 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB]
 Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]
 Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB]
 Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]
 Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]
 Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]
 Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B]
 Fetched 15.8 MB in 8s (1933 kB/s)
 Reading package lists... Done

After completing the operation, type the exit command to exit the container.

adoptdocker ps -aView all containers:

[email protected]:~# docker ps -a
 CONTAINER ID   IMAGE              COMMAND        CREATED      STATUS           PORTS          NAMES
 12847d9f5071   ubuntu:16.04           "sh"          2 minutes ago   Exited (0) 43 seconds ago               peaceful_edison
 3cec6f5e47c0   httpd              "httpd-foreground"   5 minutes ago   Exited (0) 5 minutes ago               vigilant_heisenberg

The ID is12847d9f5071The container of is changed according to our requirements. We can orderdocker commitTo submit a copy of the container.

[email protected]:~# docker commit -m="has update" -a="yeqing112" 12847d9f5071 yeqing112/ubuntu:v2
sha256:5cce1167ee4c083ff9b87e61a58081805e76d015a12a4811bd152263aa013860

Description of each parameter:

  • -m: description information submitted.
  • -a: Specifies the mirror author.
  • 12847d9f5071: container ID.
  • yeqing112/ubuntu:v2: Specifies the name of the target image to create.

We can usedocker imagesCommand to view our new imageyeqing112/ubuntu:v2

[email protected]:~# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
yeqing112/ubuntu             v2                  5cce1167ee4c        2 minutes ago       144MB
ubuntu                       16.04               2a697363a870        2 weeks ago         119MB

From the above information, we can see that the size of the updated image is significantly larger than before.

Build a mirror
In addition to the above method of updating the image, we can also use the commanddocker build, build a new image from scratch. To do this, we need to create a dockerfile file that contains a set of instructions to tellDockerHow to build our image.

[email protected]:~# cat Dockerfile 
FROM    ubuntu:16.04
MAINTAINER      Fisher "[email protected]"

RUN     apt-get update

EXPOSE  22
EXPOSE  80

CMD     /bin/bash

Each instruction creates a new layer on the image, and the prefix of each instruction must be uppercase.

Article 1FROM, specify which mirror source to use

RUNThe command tells docker to execute the command in the image and what is installed

We’re still the same here, executing aapt-get updateUpdate command

Then, we useDockerfileDocuments, bydocker build -t yeqing112/ubuntu:v3 .Command to build a new image.

[email protected]:~# [email protected]:/home# docker build -t yeqing112/ubuntu:v3 .
Sending build context to Docker daemon  422.4kB
Step 1/6 : FROM    ubuntu:16.04
 ---> 2a697363a870
Step 2/6 : MAINTAINER      Fisher "[email protected]"
 ---> Running in 92229e7a091a
Removing intermediate container 92229e7a091a
 ---> 9a89f4debc7d
Step 3/6 : RUN     apt-get update
 ---> Running in d709ef335784
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]
Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]
Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B]
Fetched 15.8 MB in 5s (3051 kB/s)
Reading package lists...
Removing intermediate container d709ef335784
 ---> 540751496556
Step 4/6 : EXPOSE  22
 ---> Running in 9ae528bc083f
Removing intermediate container 9ae528bc083f
 ---> cfccc621a52d
Step 5/6 : EXPOSE  80
 ---> Running in efd88e9349c1
Removing intermediate container efd88e9349c1
 ---> 428cb5221a06
Step 6/6 : CMD     /bin/bash
 ---> Running in 2bc08b664f97
Removing intermediate container 2bc08b664f97
 ---> 9c55ea9b9ee8
Successfully built 9c55ea9b9ee8
Successfully tagged yeqing112/ubuntu:v3

Parameter Description:

  • -t: Specifies the name of the target image to create
  • .: the directory of the dockerfile. You can specify the absolute path of the dockerfile

After a successful build, use thedocker imagesTo view the image:

[email protected]:~# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
yeqing112/ubuntu             v3                  9c55ea9b9ee8        9 seconds ago       144MB
yeqing112/ubuntu             v2                  5cce1167ee4c        16 minutes ago      144MB
ubuntu                       16.04               2a697363a870        2 weeks ago         119MB

From the above information, it can be seen that the volume of the new image V3 and V2 is the same, and both images are only executedapt-get update

Set image label
We can usedocker tagCommand to add a new label to the image.

[email protected]:/home# docker tag 9c55ea9b9ee8 yeqing112/ubuntu:v3-1

Docker tag image ID, here is9c55ea9b9ee8, user name, mirror source name, and new tag name.

As you can see with the docker images command, the ID is9c55ea9b9ee8The image of has a label of v3-1.

[email protected]:/home# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
yeqing112/ubuntu             v3                  9c55ea9b9ee8        21 minutes ago      144MB
yeqing112/ubuntu             v3-1                9c55ea9b9ee8        21 minutes ago      144MB
yeqing112/ubuntu             v2                  5cce1167ee4c        38 minutes ago      144MB
ubuntu                       16.04               2a697363a870        2 weeks ago         119MB