Docker Image Usage
When running a container with an image that does not exist locally, docker automatically downloads it from the docker image repository, by default from the Docker Hub public image source.
Let’s learn how to.
- managing and using local Docker host images
- Creating mirrors
List mirrors
We can use docker images to list the images on the local host.
apidemos@apidemos:~$ 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 repository source of the mirror
- TAG: the label of the mirror
- IMAGE ID: Mirror ID
- CREATED: mirror creation time
- SIZE: mirror size
The same repository source can have multiple TAGs, representing different versions of this repository source, such as the ubuntu repository source, there are many different versions of 15.10, 14.04, etc., we use REPOSITORY:TAG to define different mirrors.
So, if we want to run the container with the ubuntu system image of version 15.10, the command is as follows
apidemos@apidemos:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/#
Parameter description.
- -i: Interactive operation.
- -t: terminal.
- ubuntu:15.10: This means to start the container with the ubuntu 15.10 image as the base.
- /bin/bash: after the image name is the command, here we want to have an interactive shell, so we use /bin/bash.
If you want to run the container using the ubuntu system image with version 14.04, the command is as follows.
apidemos@apidemos:~$ 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 default to the ubuntu:latest image.
Get a new image
When we use a non-existent image on the local host, Docker will automatically download the image. If we want to pre-download the image, we can use the docker pull command to download it.
Capidemos@apidemos:~$ 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 use this image to run the container directly.
Find Mirror
We can search for mirrors from the Docker Hub website, which is located at https://hub.docker.com/.
We can also use the docker search command to search for mirrors. For example, we need an httpd image for our web service. We can use the docker search command to search for httpd to find the right image for us.
apidemos@apidemos:~$ docker search httpd
NAME: The name of the mirror repository source
DESCRIPTION: Description of the mirror
OFFICIAL: whether it is an official docker release
stars: Similar to star in Github, means like, like.
AUTOMATED: Automatic build.
Drag and drop images
We decided to use the official httpd version of the image above and use the command docker pull to download the image.
apidemos@apidemos:~$ 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 are ready to use the image.
apidemos@apidemos:~$ docker run httpd
Delete mirror
Mirror deletion is done using the docker rmi command, for example we delete the hello-world mirror by.
$ docker rmi hello-world
Creating mirrors
When the image we downloaded from the docker image repository doesn’t meet our needs, we can make changes to the image in the following two ways.
-
- Update the image from an already created container and commit the image
-
- Use the Dockerfile command to create a new image
Update mirror
Before updating the image, we need to create a container using the image.
apidemos@apidemos:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
Use the apt-get update command within the running container to perform the update.
When you are done, enter the exit command to exit the container.
At this point, the container with ID e218edb10161 is the container that has been changed as per our requirements. We can commit a copy of the container by using the command docker commit.
apidemos@apidemos:~$ docker commit -m="has update" -a="apidemos" e218edb10161 apidemos/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
Description of each parameter.
- -m: Description information for the submission
- -a: Specify the mirror author
- e218edb10161: container ID
- apidemos/ubuntu:v2: specifies the name of the target image to be created
We can use the docker images command to view our new image apidemos/ubuntu:v2.
apidemos@apidemos:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apidemos/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 using our new image apidemos/ubuntu
apidemos@apidemos:~$ docker run -t -i apidemos/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 that contains a set of instructions to tell Docker how to build our image.
apidemos@apidemos:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd apidemos
RUN /bin/echo 'apidemos:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
Each command creates a new layer on the mirror, and each command must be prefixed with a capital letter.
The first FROM, specifies which mirror source to use
The RUN directive tells docker what commands to execute inside the image, and what is installed.
Then, we use the Dockerfile file to build an image with the docker build command.
apidemos@apidemos:~$ docker build -t apidemos/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 apidemos
......
Parameter description.
- -t : Specify the name of the target image to be created
- . : the directory where the Dockerfile file is located, you can specify the absolute path of the Dockerfile
Use docker images to see that the created image already exists in the list, the image ID is 860c279d2fec
apidemos@apidemos:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apidemos/centos 6.7 860c279d2fec About a minute ago 190.6 MB
apidemos/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
apidemos@apidemos:~$ docker run -t -i apidemos/centos:6.7 /bin/bash
[root@41c28d18b5fb /]# id apidemos
uid=500(apidemos) gid=500(apidemos) groups=500(apidemos)
From above you can see that the new image already contains the user we created, apidemos.
Setting the image tag
We can use the docker tag command to add a new tag to the image.
apidemos@apidemos:~$ docker tag 860c279d2fec apidemos/centos:dev
docker tag The image ID, in this case 860c279d2fec , the user name, the image repository name, and the new tag name.
Using the docker images command, you can see that the image with ID 860c279d2fec has one more tag.
apidemos@apidemos:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apidemos/centos 6.7 860c279d2fec 5 hours ago 190.6 MB
apidemos/centos dev 860c279d2fec 5 hours ago 190.6 MB
apidemos/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