其他分享
首页 > 其他分享> > Docker(4):Dockerfile

Docker(4):Dockerfile

作者:互联网

容器的生命周期:

镜像制作的途径:

  1、 docker commit 

  2、Dockerfile

Dockerfile is nothing but the source code for building Docker image. Docker can build images automatically by reading the instructions from Dockerfile.

 

Dockerfile 规范:

1、格式:

2、Dockerfile 是按顺序执行 Dockerfile 里的指令集合的(从上到下依次执行)

3、每一个Dockerfile的第一个非注释指令,必须是“From” 指令,用于为镜像文件构建过程中,指定基准镜像,后续的指令运行于此基准镜像所提供的运行环境中

 

4组核心的Dockerfile指令:

USER指令:指明pid=1的那个进程是用哪个用户跑的。 WORKDIR 是工作目录

dockerfile]$ cat Dockerfile 
FROM neomaple/nginx:v1.12.2-with_curl
USER nginx
WORKDIR /usr/share/nginx/html

dockerfile]$ docker build . -t neomaple/nginx:v1.12.2_with_user_workdir  # build . 表示构建本目录下的 Dockerfile。-t 表示加一个 tag
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM neomaple/nginx:v1.12.2-with_curl
 ---> bed9d53d040f
Step 2/3 : USER nginx
 ---> Running in 61ddd6262e79
Removing intermediate container 61ddd6262e79
 ---> 913dfc82717b
Step 3/3 : WORKDIR /usr/share/nginx/html
 ---> Running in 04f2d453b212
Removing intermediate container 04f2d453b212
 ---> 7a452945f33f
Successfully built 7a452945f33f
Successfully tagged neomaple/nginx:v1.12.2_with_user_workdir

dockerfile]$ docker run --rm -ti --name nginx123 neomaple/nginx:v1.12.2_with_user_workdir /bin/bash  # 加了 -ti 后面就需要加 /bin/sh 之类的交互式的方式
nginx@ce0784f01a71:/usr/share/nginx/html$ 
nginx@ce0784f01a71:/usr/share/nginx/html$ whoami
nginx
nginx@ce0784f01a71:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
nginx@ce0784f01a71:/usr/share/nginx/html$ exit
exit

 

ADD/EXPOSE指令:

ADD指令类似于  COPY指令,但ADD指令要比COPY指令要广一些,ADD也适用于 tar包和 url;EXPOSE指令定义容器内哪个端口被暴露出来。

ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录、以及一个URL标记的文件 拷贝到镜像中。 其格式是: ADD  源路径  目标路径

dockerfile]$ cat Dockerfile
FROM neomaple/nginx:v1.12.2-with_curl
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80

dockerfile]$ docker build . -t neomaple/nginx:v1.12.2_with_add_expose
Sending build context to Docker daemon   5.12kB
Step 1/3 : FROM neomaple/nginx:v1.12.2-with_curl
 ---> bed9d53d040f
Step 2/3 : ADD index.html /usr/share/nginx/html/index.html
 ---> 037d71199706
Step 3/3 : EXPOSE 80
 ---> Running in 582258e6542d
Removing intermediate container 582258e6542d
 ---> 9e04822eec13
Successfully built 9e04822eec13
Successfully tagged neomaple/nginx:v1.12.2_with_add_expose

dockerfile]$ docker run -d -P neomaple/nginx:v1.12.2_with_add_expose    // -P 表示在宿主机上随机起一个端口把容器内的 80 端口暴露出来 
31e492b1adfd764bfcf852cc62f5969332a6d466b7405228b09df4508e2b71a1
[ec2-user@ip-172-31-47-18 dockerfile]$ docker ps -a
CONTAINER ID   IMAGE                                    COMMAND                  CREATED         STATUS                   PORTS                   NAMES
31e492b1adfd   neomaple/nginx:v1.12.2_with_add_expose   "nginx -g 'daemon of…"   4 seconds ago   Up 3 seconds             0.0.0.0:49153->80/tcp   jolly_fermi

 dockerfile]$ netstat -lntup
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:49153           0.0.0.0:*               LISTEN      -    

 

 

 

 

 

 

 

 

 

end ...

 

标签:v1.12,html,nginx,指令,neomaple,Docker,Dockerfile
来源: https://www.cnblogs.com/neozheng/p/15221447.html