Easy Tutorial
❮ Docker Logs Command Docker Compose ❯

Docker Image Usage

When running a container, if the image used does not exist locally, Docker will automatically download it from the Docker image repository, with the default being the Docker Hub public image source.

Let's learn:


Listing Image List

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

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

Option descriptions:

The same repository source can have multiple TAGs, representing different versions of the repository source. For example, in the ubuntu repository source, there are multiple versions like 15.10, 14.04, etc. We use REPOSITORY:TAG to define different images.

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

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

Parameter explanations:

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

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

If you do not specify a version tag for an image, such as just using ubuntu, Docker will default to using the ubuntu:latest image.


Getting a New Image

When we use an image that does not exist on the local host, Docker will automatically download that image. If we want to download the image in advance, we can use the docker pull command.

tutorialpro@tutorialpro:~$ 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 downloading, we can directly use this image to run a container.


Finding Images

We can search for images on the Docker Hub website. The Docker Hub URL is: **

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

tutorialpro@tutorialpro:~$  docker search httpd

Click on the image to view it in full size:

NAME: Image repository source name DESCRIPTION: Image description OFFICIAL: Whether it is an official Docker release stars: Similar to stars in Github, it indicates likes or favorites. AUTOMATED: Automated build.


Pulling an Image

We decided to use the official httpd image from the above list, and we use the docker pull command to download the image.

tutorialpro@tutorialpro:~$ 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 downloading, we can use this image.

tutorialpro@tutorialpro:~$ docker run httpd

Deleting an Image

To delete an image, we use the docker rmi command, for example, to delete the hello-world image:

$ docker rmi hello-world

Creating an Image

When the image downloaded from the Docker image repository does not meet our needs, we can modify the image in two ways:

Updating an Image

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

Use the apt-get update command inside the running container to update.

After completing the operations, exit the container by typing exit.

The container with ID e218edb10161 is now modified according to our needs. We can commit the container副本 using the docker commit command.

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

Parameter explanations:

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

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

Use our new image tutorialpro/ubuntu to start a container:

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

Building an Image

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


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

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd tutorialpro
RUN     /bin/echo 'tutorialpro: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 image, and each instruction must be prefixed with a capital letter.

The first FROM specifies which base image to use.

The RUN instruction tells Docker to execute commands inside the image, such as installing software.

We then use the Dockerfile to build an image with the docker build command.

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

Parameter explanation:

Using docker images to view the created image, which is now listed with the image ID 860c279d2fec.

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

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

From the above, we see that the new image includes the user tutorialpro.

Setting Image Tags

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

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

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

Using docker images, we can see that the image with ID 860c279d2fec now has an additional tag.

tutorialpro@tutorialpro:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tutorialpro/centos       6.7                 860c279d2fec        5 hours ago         190.6 MB
tutorialpro/centos       dev                 860c279d2fec        5 hours ago         190.6 MB
tutorialpro/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
❮ Docker Logs Command Docker Compose ❯