Dockerfile两种方式对比:
1. 一步步的执行
#### (1) 编写Dockerfile文件
vim Dockerfile
FROM ubuntu:14.04
ENTRYPOINT ["/bin/echo"]
(2)通过Dockerfile文件进行构建镜像
[[email protected] docker]# docker build .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu:14.04
---> 8cef1fa16c77
Step 2/2 : ENTRYPOINT ["/bin/echo"]
---> Running in aaad472bad79
Removing intermediate container aaad472bad79
---> 8142eaef4d70
Successfully built 8142eaef4d70
(3)查看我们构建的镜像
[[email protected] docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 8142eaef4d70 About a minute ago 223MB
(4)运行此容器测试一下
[[email protected] docker]# docker run 814 Hi Docker !
Hi Docker !
注意:当容器执行完命令后,就会进入停止状态,这时如果我们想要运行该容器的话,执行以下命令:[[email protected] docker]# docker run 814
2. 对DockerFile进行改进
(1)查看我们改变的内容
[[email protected] docker]# cat Dockerfile
FROM ubuntu:14.04
ENTRYPOINT ["/bin/echo“ , "Hi Docker !"]
(2)镜像构建
[[email protected] docker]# docker build .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu:14.04
---> 8cef1fa16c77
Step 2/2 : ENTRYPOINT ["/bin/echo" , "Hi Docker !"]
---> Running in 7d9577b1d2e1
Removing intermediate container 7d9577b1d2e1
---> 3b1ac59ac602
(3)运行容器
[[email protected] docker]# docker run 3b1
Hi Docker !
从以上查看镜像时看到,镜像是没有标签的,如果要指定标签,按以下命令行的格式来做。
[[email protected] docker]# docker build -t cookbook:hello .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu:14.04
---> 8cef1fa16c77
Step 2/2 : ENTRYPOINT ["/bin/echo" , "Hi Docker !"]
---> Using cache
---> 3b1ac59ac602
Successfully built 3b1ac59ac602
Successfully tagged cookbook:hello
再次查看镜像:
Dockerfile学习参考地址:
https://docs.docker.com/reference/builder/
https://docs.docker.com/articles/dockerfile_best-practices/
原文地址:http://blog.51cto.com/aishangwei/2120807
时间: 2024-11-06 09:56:33