理解dockerfile是如何工作的?

  1. 云栖社区>
  2. 博客>
  3. 正文

理解dockerfile是如何工作的?

杨振平 2015-02-06 15:19:10 浏览417
展开阅读全文

理解dockerfile是如何工作的?

 

1.下面是一个最简单的dockerfile:

 

FROM baserepo
RUN do something


 

2.这个dockerfile build完等价于下面的命令:

Step 0 : 
sudo docker pull baserepo:latest
sudo docker run --name baserepo_bash01 -i -t baserepo:latest /bin/bash
sudo docker ps -a
b779269d5001

sudo docker commit b779269d5001 imagename01

Step 1:
sudo docker run --name baserepo_bash02 -i -t imagename01 /bin/bash
sudo docker ps -a
b779269d5001
b779269d5002

sudo docker exec -d baserepo_bash02 do something
sudo docker commit b779269d5002 imagename02

sudo docker run --name baserepo_bash -i -t imagename02 /bin/bash
sudo docker ps -a
b779269d5001
b779269d5002
b779269d5003

sudo docker stop b779269d5002
sudo docker rm b779269d5002
sudo docker rmi imagename01

sudo docker ps -a
b779269d5001
b779269d5003

sudo docker images
baserepo:latest
imagename02


 

3.如果看不懂这做的事情,那参照下面的例子理解下

 

Take the following Dockerfile:
FROM base
RUN echo 'foo' > /tmp/foo.txt
RUN echo 'bar' >> /tmp/foo.txt


and build it:
$ docker build --force-rm -t so-26220957 .
Sending build context to Docker daemon 3.584 kB
Sending build context to Docker daemon
Step 0 : FROM base
 ---> b750fe79269d
Step 1 : RUN echo 'foo' > /tmp/foo.txt
 ---> Running in d37d756f6e55
 ---> de1d48805de2
Removing intermediate container d37d756f6e55
Step 2 : RUN echo 'bar' >> /tmp/foo.txt
 ---> Running in a180fdacd268
 ---> 40fd00ee38e1
Removing intermediate container a180fdacd268
Successfully built 40fd00ee38e1


You can now start a new container from b750fe79269d, de1d48805de2 and 40fd00ee38e1:
$ docker run --rm b750fe79269d cat /tmp/foo.txt
cat: /tmp/foo.txt: No such file or directory

$ docker run --rm de1d48805de2 cat /tmp/foo.txt
foo

$ docker run --rm 40fd00ee38e1 cat /tmp/foo.txt
foo
bar


of course you might want to start a shell to explore the filesystem and try out commands:
$ docker run --rm -it de1d48805de2 bash -il
root@ecd3ab97cad4:/# ls -l /tmp
total 4
-rw-r--r-- 1 root root 4 Oct  6 18:34 foo.txt
root@ecd3ab97cad4:/# cat /tmp/foo.txt
foo
root@ecd3ab97cad4:/#
来源于:http://stackoverflow.com/questions/26220957/how-can-i-inspect-the-file-system-of-a-failed-docker-build

 

总结下:dockerfile做的事情其实就是拉镜像,镜像启容器,容器转镜像,镜像再启容器,容器再转镜像,删除中间镜像和容器,大概就是这样的。

 

网友评论

登录后评论
0/500
评论
杨振平
+ 关注