理解dockerfile是如何工作的?

理解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:/#
来源于:

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

Docker安装应用(CentOS 6.5_x64)

在 Docker 中使用 MySQL

Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker

Docker安装应用(CentOS 6.5_x64)

Ubuntu 14.04安装Docker 

阿里云CentOS 6.5 模板上安装 Docker

Docker 的详细介绍请点这里
Docker 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5af649089ee97381adeea4d8c8d6e015.html