Docker Image Usage
When running a container, if the image used does not exist locally, Docker will automatically download it from a Docker image repository. By default, it downloads from the public Docker Hub image source.
Below, we will learn:
* 1. Managing and using local Docker host images
* 2. Creating images
* * *
## Listing Images
We can use **docker images** to list images on the local host.
tutorial@tutorial:~$ 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
Explanation of each option:
* **REPOSITORY:** Indicates the image's repository source.
* **TAG:** The image tag.
* **IMAGE ID:** The image ID.
* **CREATED:** The time the image was created.
* **SIZE:** The image size.
A single repository source can have multiple TAGs, representing different versions of that repository source. For example, the ubuntu repository source has multiple versions like 15.10 and 14.04. We use REPOSITORY:TAG to define different images.
Therefore, if we want to use the ubuntu 15.10 system image to run a container, the command is as follows:
tutorial@tutorial:~$ docker run -t -i ubuntu:15.10 /bin/bash root@d77ccb2e5cca:/#
Parameter explanation:
* **-i**: Interactive operation.
* **-t**: Terminal.
* **ubuntu:15.10**: This specifies using the ubuntu 15.10 version image as the base to start the container.
* **/bin/bash**: Placed after the image name is the command. Here, we want an interactive Shell, so we use /bin/bash.
If you want to use the ubuntu 14.04 system image to run a container, the command is as follows:
tutorial@tutorial:~$ 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 just use ubuntu, Docker will default to using the ubuntu:latest image.
* * *
## Pulling a New Image
When we use an image that does not exist on the local host, Docker will automatically download it. If we want to pre-download an image, we can use the docker pull command to download it.
tutorial@tutorial:~$ docker pull ubuntu:13.1013.10: Pulling from library/ubuntu 6599cadaf950: Pull complete 23eda618d451: Pull complete f0be3084efe9: Pull complete 52de432f084b: Pull complete a3ed95caeb02: Pull complete Digest: sha256:15b79a6654811c8d992ebacdfbd5152fcf3d165e374e264076aa435214a947a3Status: Downloaded newer image for ubuntu:13.10
After the download is complete, we can directly use this image to run a container.
* * *
## Searching for Images
We can search for images on the Docker Hub website. The Docker Hub URL is: **[https://hub.docker.com/](https://hub.docker.com/)**
We can also use the docker search command to search for images. For example, we need an httpd image for our web service. We can search for httpd using the docker search command to find a suitable image.
tutorial@tutorial:~$ docker search httpd
Click the image to view a larger version:
[!(#)](#)
**NAME:** The name of the image repository source.
**DESCRIPTION:** The description of the image.
**OFFICIAL:** Whether it is an official Docker release.
**stars:** Similar to Github stars, indicating likes or favorites.
**AUTOMATED:** Automated build.
* * *
## Pulling an Image
We decide to use the official httpd image from the image above. Use the command docker pull to download the image.
tutorial@tutorial:~$ 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.
tutorial@tutorial:~$ docker run httpd
* * *
## Deleting an Image
Image deletion uses the **docker rmi** command. For example, to delete the hello-world image:
$ docker rmi hello-world
!(#)
* * *
## Creating an Image
When the images downloaded from the Docker image repository do not meet our needs, we can modify the image in the following two ways:
* 1. Update the image from an already created container and commit this image.
* 2. Use Dockerfile instructions to create a new image.
### Updating an Image
Before updating an image, we need to use the image to create a container.
tutorial@tutorial:~$ 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, type the exit command to exit this container.
exit
At this point, the container with ID e218edb10161 is the container modified according to our requirements. We can commit a copy of the container using the docker commit command.
tutorial@tutorial:~$ docker commit -m="has update" -a="tutorial" e218edb10161 tutorial/ubuntu:v2 sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
Explanation of each parameter:
* **-m:** Description information for the commit.
* **-a:** Specify the image author.
* **e218edb10161:** Container ID.
* **tutorial/ubuntu:v2:** Specify the target image name to be created.
We can use the **docker images** command to view our new image **tutorial/ubuntu:v2**:
tutorial@tutorial:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tutorial/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 **tutorial/ubuntu** to start a container.
tutorial@tutorial:~$ docker run -t -i tutorial/ubuntu:v2 /bin/bash root@1a9fbdeb5da3:/#
### Building an Image
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.
tutorial@tutorial:~$ cat Dockerfile FROM centos:6.7 MAINTAINER Fisher "fisher@sudops.com" RUN /bin/echo 'root:123456' |chpasswd RUN useradd tutorial RUN /bin/echo 'tutorial: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 the prefix of each instruction must be uppercase.
The first instruction, FROM, specifies which image source to use.
The RUN instruction tells Docker to execute commands within the image, such as what to install...
Then, we use the Dockerfile file to build an image with the docker build command.
tutorial@tutorial:~$ docker build -t tutorial/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 ---> 0c92299c6f03Step 3 : RUN /bin/echo 'root:123456' |chpasswd ---> Using cache ---> 0397ce2fbd0aStep 4 : RUN useradd tutorial ......
Parameter explanation:
* **-t**: Specify the target image name to be created.
* **.**: The directory where the Dockerfile file is located. You can specify the absolute path to the Dockerfile.
Use docker images to check that the created image is now in the list, with image ID 860c279d2fec.
tutorial@tutorial:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tutorial/centos 6.7 860c279d2fec About a minute ago 190.6 MB tutorial/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.
tutorial@tutorial:~$ docker run -t -i tutorial/centos:6.7 /bin/bash [root@41c28d18b5fb /]# id tutorial uid=500(tutorial) gid=500(tutorial) groups=500(tutorial)
From the above, we can see the new image already contains the user tutorial we created.
### Setting an Image Tag
We can use the docker tag command to add a new tag to an image.
tutorial@tutorial:~$ docker tag 860c279d2fec tutorial/centos:dev
docker tag , here it is 860c279d2fec, , , and .
Using the docker images command, we can see that the image with ID 860c279d2fec has an additional tag.
tutorial@tutorial:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tutorial/centos 6.7 860c279d2fec 5 hours ago 190.6 MB tutorial/centos dev 860c279d2fec 5 hours ago 190.6 MB tutorial/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
YouTip