Docker images are used

When the container is run, the image used is automatically downloaded from the docker mirror repository if it does not exist locally, by default from the Docker Hub public mirror source.

Let's learn:

  • 1. Manage and use local Docker host mirroring
  • 2, create a mirror

A list of mirrors is listed

We can use docker images to list images on the local host.

w3cschool@w3cschool:~$ docker images           
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              14.04               90d5884b1ee0        5 days ago          188 MB
php                 5.6                 f40e9e0f10c8        9 days ago          444.8 MB
nginx               latest              6f8d099c3adc        12 days ago         182.7 MB
mysql               5.6                 f2e8d6c772c0        3 weeks ago         324.6 MB
httpd               latest              02ef73cf1bc0        3 weeks ago         194.4 MB
ubuntu              15.10               4e3b13c8a266        4 weeks ago         136.3 MB
hello-world         latest              690ed74de00f        6 months ago        960 B
training/webapp     latest              6fae60ef3446        11 months ago       348.8 MB

Description of the options:

  • REPOSTITORY: Represents the warehouse source of the mirror

  • TAG: Mirrored label

  • IMAGE ID: Mirror ID

  • CREATED: Mirror creation time

  • SIZE: Mirror size

The same warehouse source can have multiple TAGs, representing different versions of this warehouse source, such as the ubuntu warehouse source, there are 15.10, 14.04 and other different versions, we use REPOSTITORY:TAG to define different mirrors.

So, if we want to run the container using the ubuntu system image with version 15.10, the command is as follows:

w3cschool@w3cschool:~$ docker run -t -i ubuntu:15.10 /bin/bash 
root@d77ccb2e5cca:/#

If you want to run the container using the ubuntu system image with version 14.04, the command is as follows:

w3cschool@w3cschool:~$ docker run -t -i ubuntu:14.04 /bin/bash 
root@39e968165990:/# 

If you do not specify a version label for a mirror, for example, if you only use ubuntu, docker will use ubuntu:latest mirror by default.


Get a new image

Docker automatically downloads a nonexistent mirror when we use it on the local host. If we want to download this image in advance, we can download it using the docker pull command.

Cw3cschool@w3cschool:~$ docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
6599cadaf950: Pull complete 
23eda618d451: Pull complete 
f0be3084efe9: Pull complete 
52de432f084b: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:15b79a6654811c8d992ebacdfbd5152fcf3d165e374e264076aa435214a947a3
Status: Downloaded newer image for ubuntu:13.10

Once the download is complete, we can run the container directly using this image.


Look for a mirror

We can search for images from the Docker Hub website at: https://hub.docker.com/

We can also use the docker search command to search for images. F or example, we need a image of httpd as our web service. We can search for httpd using the docker search command to find the right image for us.

w3cschool@w3cschool:~$  docker search httpd

Docker images are used

NAME: The name of the mirror warehouse source

DESCRIPTION: Description of the mirror

OFFICIAL: Whether docker is officially released


Drag the mirror

We decided to use the official version of httpd in the image above and use the command docker pull to download the image.

w3cschool@w3cschool:~$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
8b87079b7a06: Pulling fs layer 
a3ed95caeb02: Download complete 
0d62ec9c6a76: Download complete 
a329d50397b9: Download complete 
ea7c1f032b5c: Waiting 
be44112b72c7: Waiting

Once the download is complete, we can use this image.

w3cschool@w3cschool:~$ docker run httpd

Create a mirror

When the image we downloaded from the docker mirror repository doesn't meet our needs, we can make changes to the mirror in two ways.

  • 1, update the mirror from the container you have created, and submit the mirror
  • 2. Use the Dockerfile directive to create a new image

Update the mirror

Before we can update the mirror, we need to use the mirror to create a container.
w3cschool@w3cschool:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/# 
Update within a running container using the apt-get update command.

After the operation is complete, enter the exit command to exit the container.

The container with ID e218edb10161 at this time is changed according to our needs. We can submit a copy of the container by commanding docker commit.

w3cschool@w3cschool:~$ docker commit -m="has update" -a="youj" e218edb10161 w3cschool/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8

Description of each parameter:

  • -m: The description information submitted

  • -a: Specify the mirror author

  • e218edb10161: Container ID

  • w3cschool/ubuntu:v2: Specify the name of the target image to be created

We can use the docker images command to view our new image w3cschool/ubuntu:v2:

w3cschool@w3cschool:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
w3cschool/ubuntu       v2                  70bf1840fd7c        15 seconds ago      158.5 MB
ubuntu              14.04               90d5884b1ee0        5 days ago          188 MB
php                 5.6                 f40e9e0f10c8        9 days ago          444.8 MB
nginx               latest              6f8d099c3adc        12 days ago         182.7 MB
mysql               5.6                 f2e8d6c772c0        3 weeks ago         324.6 MB
httpd               latest              02ef73cf1bc0        3 weeks ago         194.4 MB
ubuntu              15.10               4e3b13c8a266        4 weeks ago         136.3 MB
hello-world         latest              690ed74de00f        6 months ago        960 B
training/webapp     latest              6fae60ef3446        12 months ago       348.8 MB

Start a container with our new mirror w3cschool/ubuntu

w3cschool@w3cschool:~$ docker run -t -i w3cschool/ubuntu:v2 /bin/bash                            
root@1a9fbdeb5da3:/#

Build a mirror

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

w3cschool@w3cschool:~$ cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "[email protected]"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd youj
RUN     /bin/echo 'youj:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

Each instruction creates a new layer on the mirror, and the prefix for each instruction must be capital.

The first FROM, which specifies which mirror source to use

The RUN instruction tells docker to execute the command inside the mirror and install what...

We then use the Dockerfile file to build a mirror using the docker build command.

w3cschool@w3cschool:~$ docker build -t youj/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
 ---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "[email protected]"
 ---> Using cache
 ---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
 ---> Using cache
 ---> 0397ce2fbd0a
Step 4 : RUN useradd youj
......

Description of the parameters:

  • -t: Specifies the name of the target image to create

  • . :D directory where the dockfile file is located, you can specify the absolute path of Dockerfile

Images created using docker images are already present in the list, with a mirror ID of 860c279d2fec

w3cschool@w3cschool:~$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
youj/centos       6.7                 860c279d2fec        About a minute ago   190.6 MB
w3cschool/ubuntu       v2                  70bf1840fd7c        17 hours ago         158.5 MB
ubuntu              14.04               90d5884b1ee0        6 days ago           188 MB
php                 5.6                 f40e9e0f10c8        10 days ago          444.8 MB
nginx               latest              6f8d099c3adc        12 days ago          182.7 MB
mysql               5.6                 f2e8d6c772c0        3 weeks ago          324.6 MB
httpd               latest              02ef73cf1bc0        3 weeks ago          194.4 MB
ubuntu              15.10               4e3b13c8a266        5 weeks ago          136.3 MB
hello-world         latest              690ed74de00f        6 months ago         960 B
centos              6.7                 d95b5ca17cc3        6 months ago         190.6 MB
training/webapp     latest              6fae60ef3446        12 months ago        348.8 MB

We can use the new image to create the container

w3cschool@w3cschool:~$ docker run -t -i youj/centos:6.7  /bin/bash
[root@41c28d18b5fb /]# id youj
uid=500(youj) gid=500(youj) groups=500(youj)

See from above that the new image already contains the user youj we created


Set the mirror label

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

w3cschool@w3cschool:~$ docker tag 860c279d2fec youj/centos:dev

Docker tag mirror ID, here is 860c279d2fec, user name, mirror source name (repository name), and new tag name (tag).

Using the docker images command, you can see that the image with an ID of 860c279d2fec has one more label.

w3cschool@w3cschool:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
youj/centos       6.7                 860c279d2fec        5 hours ago         190.6 MB
youj/centos       dev                 860c279d2fec        5 hours ago         190.6 MB
w3cschool/ubuntu       v2                  70bf1840fd7c        22 hours ago        158.5 MB
ubuntu              14.04               90d5884b1ee0        6 days ago          188 MB
php                 5.6                 f40e9e0f10c8        10 days ago         444.8 MB
nginx               latest              6f8d099c3adc        13 days ago         182.7 MB
mysql               5.6                 f2e8d6c772c0        3 weeks ago         324.6 MB
httpd               latest              02ef73cf1bc0        3 weeks ago         194.4 MB
ubuntu              15.10               4e3b13c8a266        5 weeks ago         136.3 MB
hello-world         latest              690ed74de00f        6 months ago        960 B
centos              6.7                 d95b5ca17cc3        6 months ago        190.6 MB
training/webapp     latest              6fae60ef3446        12 months ago       348.8 MB