Using Docker images
When running a container, if the image used does not exist locally, docker automatically downloads it from the docker image repository, by default from the public Docker Hub source.
Let's learn the following:
- 1. Manage and use local Docker host images
- 2. Create images
List images
We can use the docker images to list images on the local host.
runoob@runoob:~$ 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 each option:
-
REPOSITORY:Indicates the image's repository source
-
TAG:The image's tag
-
IMAGE ID:Image ID
-
CREATED:Image creation time
-
SIZE:Image size
A single repository source can have multiple TAGs, representing different versions of that source. For example, the ubuntu repository has versions such as 15.10 and 14.04; we use REPOSITORY:TAG to define different images.
Therefore, if we want to use the ubuntu system image of version 15.10 to run a container, the command is as follows:
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/#
Parameter Description:
- -i: Interactive operation.
- -t: Terminal.
- ubuntu:15.10: This means starting a container based on the ubuntu 15.10 image.
- /bin/bash: What follows the image name is the command. Here we want an interactive Shell, so /bin/bash is used.
If you want to use the ubuntu system image of version 14.04 to run a container, the command is as follows:
runoob@runoob:~$ 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 ubuntu:latest image by default.
Fetch a new image
When we use an image that does not exist on the local host, Docker automatically downloads it. If we want to download the image in advance, we can use the docker pull command.
Crunoob@runoob:~$ 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 a container.
Search for images
We can search for images on the Docker Hub website; the Docker Hub URL 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 for our web service, we can use the docker search command to search for httpd to find a suitable image.
runoob@runoob:~$ docker search httpd
Click the image to view a larger version:
**NAME: ** the name of the image repository source
**DESCRIPTION: ** the image's description
**OFFICIAL: ** whether it is officially released by docker
**stars: ** similar to stars on GitHub, indicating likes or favorites.
**AUTOMATED: ** automated build.
Pull an image
We decide to use the official version of the httpd image shown above and use the docker pull command to download the image.
runoob@runoob:~$ 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.
runoob@runoob:~$ docker run httpd
Delete an image
To delete an image, use docker rmi the command; for example, to delete the hello-world image:
$ docker rmi hello-world

Create an image
When images downloaded from the docker image repository cannot meet our needs, we can modify an image in the following two ways.
- 1. Update an image from an already created container and commit that image
- 2. Use Dockerfile instructions to create a new image
Update an image
Before updating an image, we need to use the image to create a container.
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
After entering the container, update the system:
apt-get update
apt-get upgrade -y
After completing the operations, enter the exit command to leave the container.
exit
At this point, the container with ID e218edb10161 has been modified according to our needs. We can commit a copy of the container using the docker commit command.
runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
Description of each parameter:
-
-m: The description information for the commit
-
-a: Specify the image author
-
e218edb10161:Container ID
-
runoob/ubuntu:v2: Specify the target image name to create
We can use the docker images command to view our new image runoob/ubuntu:v2:
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/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 runoob/ubuntu to start a container
runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash
root@1a9fbdeb5da3:/#
Building Images
We use the command docker build , to create a new image from scratch. To do this, we need to create a Dockerfile file containing a set of instructions that tell Docker how to build our image.
runoob@runoob:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd runoob
RUN /bin/echo 'runoob: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 in the image, and the prefix of every instruction must be uppercase.
The first FROM specifies which image source to use
The RUN instruction tells docker to execute commands inside the image and what to install...
Then, we use the Dockerfile and the docker build command to build an image.
runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "fisher@sudops.com"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......
Parameter Description:
-
-t : Specify the target image name to create
-
. : The directory where the Dockerfile is located; you can specify the absolute path of the Dockerfile
Use docker images to see that the created image is already in the list, with image ID 860c279d2fec
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec About a minute ago 190.6 MB
runoob/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
runoob@runoob:~$ docker run -t -i runoob/centos:6.7 /bin/bash
[root@41c28d18b5fb /]# id runoob
uid=500(runoob) gid=500(runoob) groups=500(runoob)
From the above, we can see that the new image already contains the user runoob we created.
Set an image tag
We can use the docker tag command to add a new tag to an image.
runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev
docker tag image ID, here 860c279d2fec, user name, image source name (repository name), and the new tag name (tag).
Using the docker images command, you can see that the image with ID 860c279d2fec has one more tag.
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec 5 hours ago 190.6 MB
runoob/centos dev 860c279d2fec 5 hours ago 190.6 MB
runoob/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
