docker 容器入门(三)

写在前面的话

每篇一句

板凳要坐十年冷;文章不写半句空。

上篇通过dockerfile已经构建了自己的镜像,那如何将镜像分享到自己的仓库呢?

首先注册docker hub账号官网地址

构建你的容器仓库,分享你的镜像

通过docker ID登录

我的用户名是zakizqzhang

1
2
3
4
5
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: zakizqzhang
Password:
Login Succeeded

给你的镜像打上标签

通常是username/repository:tag来定义你的一个镜像,tag选项是可选的,但是推荐使用来让其他人明白,最好使用有意义的命名,比如dockerfiletestapp:v1.0,v1.0就是标签,明确知道是第1.0版本,或者使用
dockerfiletestapp:20171028,明确知道是2017年10月28号构建的镜像。

命令语法

1
docker tag image username/repository:tag

查看本地有哪些镜像

其中dockerfiletestapp就是上次我们用dockerfile构建的镜像

1
2
3
4
5
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerfiletestapp latest 6410ee31b207 6 days ago 195MB
python 2.7-slim 9724e90f1f17 2 weeks ago 184MB
hello-world latest 05a3bd381fc2 6 weeks ago 1.84kB

给dockerfiletestapp 镜像打上标签

1
2
3
4
5
6
7
[root@localhost ~]# docker tag dockerfiletestapp zakizqzhang/get-started:20171028
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerfiletestapp latest 6410ee31b207 6 days ago 195MB
zakizqzhang/get-started 20171028 6410ee31b207 6 days ago 195MB
python 2.7-slim 9724e90f1f17 2 weeks ago 184MB
hello-world latest 05a3bd381fc2 6 weeks ago 1.84kB

push 发布上传镜像

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# docker push zakizqzhang/get-started:20171028
The push refers to a repository [docker.io/zakizqzhang/get-started]
71fedff88b96: Pushed
1f3b097ea1a5: Pushed
3a3e7e444f5d: Pushed
f39f8a7b1485: Mounted from library/python
5e4d4a29edae: Mounted from library/python
f46f014db30a: Mounted from library/python
c01c63c6823d: Mounted from library/python
20171028: digest: sha256:208754af6f21fc4a14eacc22b2734f490cb3c3a495c7595a74a79529f68429ee size: 1787

在任何一台联网装有docker引擎的机器上拉取并运行远程仓库镜像

这里为了方便,先将本地dockerfiletestapp镜像删除。注意删除镜像的前置条件是先删除通过该镜像运行过的容器方可删除镜像。

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
[root@localhost ~]# docker image rm zakizqzhang/get-started:20171028
Untagged: zakizqzhang/get-started:20171028
Untagged: zakizqzhang/get-started@sha256:208754af6f21fc4a14eacc22b2734f490cb3c3a495c7595a74a79529f68429ee
[root@localhost ~]# docker image rm dockerfiletestapp
Error response from daemon: conflict: unable to remove repository reference "dockerfiletestapp" (must force) - container 00616249b739 is using its referenced image 6410ee31b207
[root@localhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
032f18cd82ef 6410ee31b207 "python app.py" 2 minutes ago Exited (0) 54 seconds ago flamboyant_lamport
f5295eca69d8 dockerfiletestapp "python app.py" 6 days ago Exited (255) 6 days ago 0.0.0.0:8080->80/tcp optimistic_goldberg
bf03b99b65fc dockerfiletestapp "python app.py" 6 days ago Exited (0) 6 days ago romantic_knuth
00616249b739 dockerfiletestapp "python app.py" 6 days ago Exited (0) 6 days ago priceless_stonebraker
e892562d1fa4 hello-world "/hello" 6 days ago Exited (0) 6 days ago determined_raman
[root@localhost ~]# docker rm 00616249b739 bf03b99b65fc f5295eca69d8
00616249b739
bf03b99b65fc
f5295eca69d8
[root@localhost ~]# docker image rm dockerfiletestapp
Error response from daemon: conflict: unable to remove repository reference "dockerfiletestapp" (must force) - container 032f18cd82ef is using its referenced image 6410ee31b207
[root@localhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
032f18cd82ef 6410ee31b207 "python app.py" 3 minutes ago Exited (0) 2 minutes ago flamboyant_lamport
e892562d1fa4 hello-world "/hello" 6 days ago Exited (0) 6 days ago determined_raman
[root@localhost ~]# docker rm 032f18cd82ef
032f18cd82ef
[root@localhost ~]# docker image rm dockerfiletestapp
Untagged: dockerfiletestapp:latest
Deleted: sha256:6410ee31b207d3cdf8120c69b27de0abf3962e6a06eb339410ba1ca4c51b46f5
Deleted: sha256:c3639bed347aae90e7d81e047384249ff3969725ded6ae9e8aca225a1f734d31
Deleted: sha256:dd270ef27c690dc6491ae844f54bc85a5cffadc4206779a8be48cf93eae0becc
Deleted: sha256:9956c6e6c7c952fcde7b61d98aec4e5b545ec2a9608c0f9def397ba46ba33a59
Deleted: sha256:0ca8e4dc2aa04cfc28294e9679dac0e41b0941ea5decad36dacc68549269760e
Deleted: sha256:046356d78b97a7d14e4bfe360218a0068005eeca0a2931418bc5f3a308367268
Deleted: sha256:3400ce1c487e9213c6be4c6ade646eebbc64bae8666151a208d3179fb04581b4
Deleted: sha256:40fcd39ff30a1569a680fa8ddb9968b7062e0e6f32a58416877e76338e7b0734
Deleted: sha256:e7d7f9d762c90de03b48c583a5304626df173ba2a0e2c6387110f87af7e5d809
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 2.7-slim 9724e90f1f17 2 weeks ago 184MB
hello-world latest 05a3bd381fc2 6 weeks ago 1.84kB

通过拉取远程仓库镜像本地运行容器

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# docker run -p 8080:80 zakizqzhang/get-started:20171028
Unable to find image 'zakizqzhang/get-started:20171028' locally
20171028: Pulling from zakizqzhang/get-started
85b1f47fba49: Already exists
a5e401c4a903: Already exists
9f744658dd62: Already exists
562843cc69f8: Already exists
361ab7a9bb73: Pull complete
72c3035db3bc: Pull complete
68e97c9c12b4: Pull complete
Digest: sha256:208754af6f21fc4a14eacc22b2734f490cb3c3a495c7595a74a79529f68429ee
Status: Downloaded newer image for zakizqzhang/get-started:20171028
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

运行的容器所需的python、flask、redis、应用脚本app.py等依赖全部不用你再考虑了,实现了一次构建,随处运行。

----纸上得来终觉浅绝知此事要躬行----
最好的赞赏是您的阅读!
0%