English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Docker Image Usage

When running a container, if the image used is not present locally, Docker will automatically download it from the Docker image repository, by default from the public Docker Hub image source.

Let's learn below:

  • 1, manage and use local Docker host images

  • 2, create image

List image list

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

w3codebox@w3codebox:~$ 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 various options:

  • REPOSITORY:Indicates the image repository

  • TAG:Image tag

  • IMAGE ID:Image ID

  • CREATED:Image creation time

  • SIZE:Image size

The same repository can have multiple TAGs, representing different versions of the repository, such as in the ubuntu repository, there are 15.10,14.04 and so on, we use REPOSITORY:TAG to define different images.

So, if we want to use versions such as15.10To run containers using the ubuntu system image, the command is as follows:

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

Parameter description:

  • -iInteractive operation.

  • -tTerminal.

  • ubuntu:15.10This refers to using ubuntu 15.10 to start the container with the version image as the base.

  • /bin/bashThe command placed after the image name is, here we want an interactive Shell, so we use /bin/bash.

If you want to use a version of 14.04 To run containers using the ubuntu system image, the command is as follows:

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

If you do not specify a version tag for an image, for example, if you only use 'ubuntu', Docker will use the default 'ubuntu:latest' image.

Get a new image

When we use a non-existent image on the local host, Docker will automatically download this image. If we want to download this image in advance, we can use the docker pull command to download it.

Cw3codebox@w3codebox:~$ 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

After the download is complete, we can directly use this image to run the container.

Search image

We can search for images from the Docker Hub website, the URL of which is:https://hub.docker.com/

We can also use the docker search command to search for images. For example, if we need an httpd image as our web service, we can search for httpd using the docker search command to find an image suitable for us.

w3codebox@w3codebox:~$ docker search httpd

NAME: Name of the image repository source

DESCRIPTION: Description of the image

OFFICIAL: Whether it is released by Docker official

stars: Similar to the star in Github, it means like or favorite.

AUTOMATED: Automatically build.

Pulling image

We decide to use the official version of httpd in the figure above, and download the image using the command docker pull.

w3codebox@w3codebox:~$ 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

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

w3codebox@w3codebox:~$ docker run httpd

Delete image

is used for image deletion docker rmi command, such as deleting hello-world image:

$ docker rmi hello-world

Create image

When the image downloaded from the docker image repository does not meet our needs, we can modify the image in the following two ways.

  • 1, update the image from the already created container and submit this image

  • 2, use Dockerfile instructions to create a new image

Update image

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

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

to use inside a running container apt-get update command to update.

After completing the operation, enter the exit command to exit this container.

Now the ID is e218edb10161 container, is the container modified according to our needs. We can submit a container copy by using the command docker commit.

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

Description of various parameters:

  • -m: submitted description information

  • -a: to specify the image author

  • e218edb10161:container ID

  • w3codebox/ubuntu:v2: to specify the target image name to be created

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

w3codebox@w3codebox:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
w3codebox/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

using our new image w3codebox/ubuntu to start a container

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

to build the image

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

w3codebox@w3codebox:~$ cat Dockerfile 
FROM centos:6.7
MAINTAINER Fisher "[email protected]"
RUN     /bin/echo 'root:123456' |chpasswd
RUN useradd w3codebox
RUN     /bin/echo 'w3codebox: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 will create a new layer on the image, and the prefix of each instruction must be uppercase.

The first FROM specifies which image source to use

The RUN instruction tells docker to execute commands within the image, what is installed and so on...

Then, we use the Dockerfile file to build an image through the 'docker build' command.

w3codebox@w3codebox:~$ docker build -t w3codebox/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 w3codebox
......

Parameter description:

  • -t :Specify the target image name to be created

  • . :Directory where the Dockerfile file is located, can specify the absolute path of Dockerfile

Using 'docker images' to view, the created image already exists in the list, with image ID860c279d2fec

w3codebox@w3codebox:~$ docker images 
REPOSITORY                               TAG                                                   IMAGE ID                                           CREATED                                           SIZE
w3codebox/centos       6.7                 860c279d2fec                               About a minute ago   190.6 MB
w3codebox/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 a container

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

As seen above, the new image already includes the user w we created3codebox

Set image tag

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

w3codebox@w3codebox:~$ docker tag 860c279d2fec w3codebox/centos:dev

docker tag image ID, here is 860c279d2fec, username, repository name (repository name), and the new tag name (tag).

You can see from the 'docker images' command that the ID is860c279d2The fec image has an additional tag.

w3codebox@w3codebox:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
w3codebox/centos       6.7                 860c279d2fec        5 hours ago         190.6 MB
w3codebox/centos dev                 860c279d2fec        5 hours ago         190.6 MB
w3codebox/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