Docker教程总结七:(dockerfile)
Dockerfile的教程
Dockfile简介:
Dockerfile是docker构建镜像的基础,也是docker区别于其他容器的重要特征,正是有了Dockerfile,docker的自动化和可移植性才成为可能。不论是开发还是运维,学会编写Dockerfile几乎是必备的,这有助于你理解整个容器的运行。
Dockfile常用命令:
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 |
# FROM , 从一个基础镜像构建新的镜像 FROM ubuntu # MAINTAINER , 维护者信息 MAINTAINER William <wlj@nicescale.com> # ENV , 设置环境变量 ENV TEST 1 # RUN , 非交互式运行shell命令 RUN apt-get -y update RUN apt-get -y install nginx # ADD , 将外部文件拷贝到镜像里,src可以为url ADD http://nicescale.com/ /data/nicescale.tgz # WORKDIR /path/to/workdir, 设置工作目录 WORKDIR /var/www # USER , 设置用户ID USER nginx # VULUME <#dir>, 设置volume VOLUME [‘/data’] # EXPOSE , 暴露哪些端口 EXPOSE 80 443 # ENTRYPOINT [‘executable’, ‘param1’,’param2’]执行命令 ENTRYPOINT ["/usr/sbin/nginx"] # CMD [“param1”,”param2”] CMD ["start"] |
注意:docker创建、启动container时执行的命令,如果设置了ENTRYPOINT,则CMD将作为参数,另尽量将一些常用不变的指令放到前面,CMD和ENTRYPOINT尽量使用json数组方式
Nginx的DockerFile例子:
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 |
# # Nginx Dockerfile # # https://github.com/dockerfile/nginx # # Pull base image. FROM dockerfile/ubuntu # Install Nginx. RUN \ add-apt-repository -y ppa:nginx/stable && \ apt-get update && \ apt-get install -y nginx && \ rm -rf /var/lib/apt/lists/* && \ echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \ chown -R www-data:www-data /var/lib/nginx # Define mountable directories. VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"] # Define working directory. WORKDIR /etc/nginx # Define default command. CMD ["nginx"] # Expose ports. EXPOSE 80 EXPOSE 443 |