docker新手入门(3)之镜像使用

运行容器的时候如果镜像本地不存在,docker就会从docker镜像仓库里面去下载,默认docker hub公共镜像源 这一篇主要是怎么管理使用本地镜像以及创建镜像

列出镜像列表

命令:docker images

1
2
3
4
5
root@blacsheep-myubuntu:/home/blacsheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.10 7d3f705d307c 6 months ago 107MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
root@blacsheep-myubuntu:/home/blacsheep#

其中 repository:镜像的仓库源 tag:镜像的标签 image id:镜像id created:创建时间 size:大小 一个仓库源可以有多个tag,比如一个ubuntu的仓库源里面可以有14.04,15.10等等版本,我们可以用repository:tag来定义不同的镜像,比如我们要用16.10的ubuntu来运行容器的时候,就是使用

1
2
root@blacsheep-myubuntu:/home/blacsheep# docker run -t -i ubuntu:16.10 /bin/bash 
root@68cc92ce5dee:/#

如果不去指定镜像的标签的话,比如只用ubuntu,docker就会默认使用ubuntu:latest的镜像

获取镜像

使用docker pull,同时说明不带标签的话使用的是latest的标签

1
2
3
4
5
6
7
8
9
10
11
root@blacsheep-myubuntu:/home/blacsheep# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
1be7f2b886e8: Pull complete
6fbc4a21b806: Pull complete
c71a6f8e1378: Pull complete
4be3072e5a37: Pull complete
06c6d2f59700: Pull complete
Digest: sha256:e46251ecd3548065ca203df31bdcf25e53b530b9b39bd3a86ac8147b347bb7ac
Status: Downloaded newer image for ubuntu:latest
root@blacsheep-myubuntu:/home/blacsheep#

下载完成之后docker images查看

1
2
3
4
5
6
root@blacsheep-myubuntu:/home/blacsheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 0458a4468cbc 2 days ago 112MB
ubuntu 16.10 7d3f705d307c 6 months ago 107MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
root@blacsheep-myubuntu:/home/blacsheep#

查找镜像

可以从docker hub的网站来搜索镜像,网址为https://hub.docker.com/ 如果想用命令行,可以用docker search xxx,比如我们想找一个httpd的镜像作为web服务,可以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
root@blacsheep-myubuntu:/home/blacsheep# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 1470 [OK]
hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with a … 40
centos/httpd 15 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 8
armhf/httpd The Apache HTTP Server Project 8
macadmins/netboot-httpd use in combination with bruienne/bsdpy 4 [OK]
lolhens/httpd Apache httpd 2 Server 2 [OK]
salim1983hoop/httpd24 Dockerfile running apache config 2 [OK]
fboaventura/dckr-httpd Small footprint http server to use with othe… 1 [OK]
rgielen/httpd-image-simple Docker image for simple Apache httpd based o… 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
epflidevelop/os-wp-httpd WP httpd 1 [OK]
rgielen/httpd-image-php5 Docker image for Apache httpd with PHP 5 bas… 1 [OK]
publici/httpd httpd:latest 0 [OK]
tplatform/aws-linux-httpd24-php70 aws-linux-httpd24-php70 0 [OK]
dockerpinata/httpd 0
trollin/httpd 0
manasip/httpd 0
manageiq/httpd Container with httpd, built on CentOS for Ma… 0 [OK]
jbpt/httpd 0 [OK]
fintaffy/fintaffy-httpd 0
efrecon/httpd A micro-sized httpd. Start serving files in … 0 [OK]
ppc64le/httpd The Apache HTTP Server Project 0
sbutler/pie-httpd PIE httpd server 0
camptocamp/os-wp-httpd Wordpress httpd 0 [OK]
root@blacsheep-myubuntu:/home/blacsheep#

name:表示镜像仓库源的名称 description:镜像描述 official:是否是docker官方发布的

托取镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@blacsheep-myubuntu:/home/blacsheep# docker pull httpd

Using default tag: latest
latest: Pulling from library/httpd
f49cf87b52c1: Pull complete
02ca099fb6cd: Pull complete
de7acb18da57: Pull complete
770c8edb393d: Pull complete
0e252730aeae: Pull complete
6288e83d58fa: Pull complete
a91ad03b2178: Pull complete
Digest: sha256:6261d80fe154a7d7f1cbb0ca90eb344fafb4511afd06ebfc2d41915367ce25ad
Status: Downloaded newer image for httpd:latest
root@blacsheep-myubuntu:/home/blacsheep#

之后docker run httpd就可以了

创建镜像(重点)

如果我们从docker镜像仓库里面下载的镜像不能满足我们的需求的话,有两种方式可以对镜像进行更改 - 1.从已经创建的容器中更新镜像,并且提交这个镜像 - 2.使用docker file来创建一个新的镜像

更新镜像

更新之前先用镜像创建一个容器

1
root@blacsheep-myubuntu:/home/blacsheep# docker run -t -i ubuntu:16.10 /bin/bashroot@000b42c94ddd:/#

容器内使用apt-get update更新一下 完成之后exit退出 这个容器是按我们的需求修改的容器,可以使用docker commit来提交容器副本

1
2
3
root@blacsheep-myubuntu:/home/blacsheep# docker commit -m="update ubuntu" -a="blacsheep" 000b42c94ddd blacsheep/ubuntu:v1
sha256:3846343d3b39a77b3dbadb32cdef5a3669560bbb96d52d639f24cc06f8d2655e
root@blacsheep-myubuntu:/home/blacsheep#

-m:提交的描述 -a:镜像作者 000b42c94ddd:容器id blacsheep/ubuntu:v1:镜像名:标签 然后docker images查看

1
2
3
4
5
6
7
8
root@blacsheep-myubuntu:/home/blacsheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
blacsheep/ubuntu v1 3846343d3b39 About a minute ago 107MB
httpd latest 2e202f453940 2 days ago 179MB
ubuntu latest 0458a4468cbc 2 days ago 112MB
ubuntu 16.10 7d3f705d307c 6 months ago 107MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
root@blacsheep-myubuntu:/home/blacsheep#

这样就可以使用新的镜像来创建容器了

1
2
root@blacsheep-myubuntu:/home/blacsheep# docker run -t -i blacsheep/ubuntu:v1 /bin/bash
root@02b45e36003d:/#
dockerfile创建镜像

使用docker build可以从零开始创建一个镜像。但是我们必须创建一个Dockerfile来告诉docker怎么来创建我们的镜像 dockerfile自己写 写完了大概就是

1
2
3
4
5
6
7
8
9
root@blacsheep-myubuntu:/home/blacsheep# cat Dockerfile
From ubuntu:16.04
RUN /bin/echo 'root:123456' chpasswd
RUN useradd blacsheep
RUN /bin/echo 'blacsheep:123456' chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D

From:表示使用哪个镜像源 RUN:表示告诉docker在镜像里面执行什么命令 然后使用docker build -t xxx .来创建 注意,不要忘记指令最后的一个点,点表示dockerfile在当前目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
root@blacsheep-myubuntu:/home/blacsheep# docker build -t blacsheep/ubuntu:dockerfile .
ERRO[0036] Can't add file /home/blacsheep/.gnupg/S.gpg-agent to tar: archive/tar: sockets not supported
Sending build context to Docker daemon 2.19GB
Step 1/8 : From ubuntu:16.04
16.04: Pulling from library/ubuntu
1be7f2b886e8: Already exists
6fbc4a21b806: Already exists
c71a6f8e1378: Already exists
4be3072e5a37: Already exists
06c6d2f59700: Already exists
Digest: sha256:2b80e7fbfd1b0a3784dd0e55fb6f2750ebc33e596c2ccee5201ecf018ac5ae05
Status: Downloaded newer image for ubuntu:16.04
---> 0458a4468cbc
Step 2/8 : RUN /bin/echo 'root:123456' chpasswd
---> Running in b3d12aa12f6c
Removing intermediate container b3d12aa12f6c
---> 99c6e9ccdd81
Step 3/8 : RUN useradd blacsheep
---> Running in fe025de84d56
Removing intermediate container fe025de84d56
---> e5180f2047b3
Step 4/8 : RUN /bin/echo 'blacsheep:123456' chpasswd
---> Running in aa3c30d8f07b
Removing intermediate container aa3c30d8f07b
---> 9bda89fa8c14
Step 5/8 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
---> Running in d63a51e6d430
Removing intermediate container d63a51e6d430
---> a3b879d7090e
Step 6/8 : EXPOSE 22
---> Running in d2f15040553b
Removing intermediate container d2f15040553b
---> 65916c8dda3e
Step 7/8 : EXPOSE 80
---> Running in 62dde9991ee4
Removing intermediate container 62dde9991ee4
---> 5bf05e8d377f
Step 8/8 : CMD /usr/sbin/sshd -D
---> Running in 62528b637ebc
Removing intermediate container 62528b637ebc
---> 7ba34ce933fb
Successfully built 7ba34ce933fb
Successfully tagged blacsheep/ubuntu:dockerfile
root@blacsheep-myubuntu:/home/blacsheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
blacsheep/ubuntu dockerfile 7ba34ce933fb About a minute ago 112MB
blacsheep/ubuntu v1 3846343d3b39 22 minutes ago 107MB
httpd latest 2e202f453940 2 days ago 179MB
ubuntu 16.04 0458a4468cbc 2 days ago 112MB
ubuntu latest 0458a4468cbc 2 days ago 112MB
ubuntu 16.10 7d3f705d307c 6 months ago 107MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
root@blacsheep-myubuntu:/home/blacsheep#

最后的docker images也可以看出镜像安装成功了 然后至于用户

1
2
3
4
root@blacsheep-myubuntu:/home/blacsheep# docker run -i -t blacsheep/ubuntu:dockerfile /bin/bash
root@1b52c348bf31:/# id blacsheep
uid=1000(blacsheep) gid=1000(blacsheep) groups=1000(blacsheep)
root@1b52c348bf31:/#

设置镜像标签

使用docker tag命令 比如

1
2
3
4
5
6
7
8
9
10
root@blacsheep-myubuntu:/home/blacsheep# docker tag 7ba34ce933fb blacsheep/ubuntu:practice
root@blacsheep-myubuntu:/home/blacsheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
blacsheep/ubuntu dockerfile 7ba34ce933fb 19 minutes ago 112MB
blacsheep/ubuntu practice 7ba34ce933fb 19 minutes ago 112MB
blacsheep/ubuntu v1 3846343d3b39 40 minutes ago 107MB
httpd latest 2e202f453940 2 days ago 179MB
ubuntu 16.10 7d3f705d307c 6 months ago 107MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
root@blacsheep-myubuntu:/home/blacsheep#

可以看到blacsheep/ubuntu多了一个practice标签.