Dockerfile making

changhao 2021-01-21 11:09:19
dockerfile making

edition 1.0
The team Beixin Zhonghe Technology Group
Make <>
Time 2021/01/21

Knowledge point

  • Dockerfile Make

    • Dockerfile The basic structure
    • Dockerfile The document states
    • Dockerfile Case a
    • Dockerfile build Use

Dockerfile Make

Dockerfile The basic structure

  • Base image information
  • Maintainer information
  • Mirror operation instruction
  • The container executes instructions when it starts

Dockerfile The document states

FROM: Specify base image , Must be the first command

 Format :
FROM <image>
FROM <image>:<tag>
FROM <image>@<digest>
Example : FROM mysql:5.6
notes : tag or digest It's optional , If these two values are not used , Will use latest The basic image of the version

MAINTAINER: Maintainer information

 Format :
Example :
MAINTAINER changhao <>

RUN: The command to execute when building the image

RUN Used to execute commands in the mirror container , There are two ways to execute commands :
shell perform
Format :
RUN <command>
exec perform
Format :
RUN ["executable", "param1", "param2"]
Example :
RUN ["executable", "param1", "param2"]
RUN apk update
RUN ["/etc/execfile", "arg1", "arg1"]
notes :
RUN The intermediate image created by the instruction will be cached , And will use... In the next build . If you don't want to use these cache images , Can be specified at build time --no-cache Parameters , Such as :docker build --no-cache

ADD: Add the local file to the container , tar Type files are automatically decompressed , Access to network resources , similar wget

 Format :
ADD <src>... <dest>
ADD ["<src>",... "<dest>"] Path to support spaces
Example :
ADD hom* /mydir/ # Add all to "hom" Opening file
ADD hom?.txt /mydir/ # ? Replace a single character , for example :"home.txt"
ADD test relativeDir/ # add to "test" To `WORKDIR`/relativeDir/
ADD test /absoluteDir/ # add to "test" To /absoluteDir/

COPY: The function is similar to ADD, But it doesn't automatically compress files , You can't access network resources

CMD: Call after building container , That is, it is called only when the container starts

 Format :
CMD ["executable","param1","param2"] ( Execute executable , first )
CMD ["param1","param2"] ( Set up ENTRYPOINT, Call directly ENTRYPOINT Add parameter )
CMD command param1 param2 ( perform shell Internal orders )
Example :
CMD echo "This is a test." | wc -
CMD ["/usr/bin/wc","--help"]
notes :
CMD differ RUN,CMD Specifies the command to execute when the container starts , and RUN Used to specify the command to be executed when the image is built .

ENTRYPOINT: Configure container , Make it executable . coordination CMD Can be omitted "application", Use parameters only

 Format :
ENTRYPOINT ["executable", "param1", "param2"] ( Executable file , first )
ENTRYPOINT command param1 param2 (shell Internal orders )
Example :
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
notes :
ENTRYPOINT And CMD Very similar , The difference is through docker run The command executed will not override ENTRYPOINT, and docker run Any parameter specified in the command , Will be passed to the ENTRYPOINT.Dockerfile Only one is allowed in ENTRYPOINT command , Multiple assignments override previous settings , Only the last ENTRYPOINT Instructions .

LABEL: Used to add metadata to the image

 Format :
LABEL <key>=<value> <key>=<value> <key>=<value> ...
Example :
LABEL version="1.0" description=" This is a Web The server " by="IT record "
notes :
Use LABEL When specifying metadata , One LABEL Specifies that one or more metadata can be specified , When multiple pieces of metadata are specified, different metadata are separated by spaces . It is recommended to pass all metadata through one LABEL Instruction assignment , Avoid generating too many intermediate images .

ENV: Set the environment variable

 Format :
ENV <key> <value> #<key> All subsequent content will be treated as <value> Component part , therefore , Only one variable can be set at a time
ENV <key>=<value> ... # Multiple variables can be set , One for each variable "<key>=<value>" The key/value pair , If <key> Contains Spaces , have access to \ To escape , It can also be done through "" To mark ; in addition , Backslashes can also be used for continuation lines
Example :
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat=fluffy

EXPOSE: Specify the port for external interaction

 Format :
EXPOSE <port> [<port>...]
Example :
EXPOSE 80 443
EXPOSE 11211/tcp 11211/udp
notes :
EXPOSE Does not allow the port of the container to access the host . To make it accessible , Need to be in docker run Pass when running container -p To publish these ports , Or through -P Parameters to publish EXPOSE All ports exported 

VOLUME: Used to specify the persistence directory

 Format :
VOLUME ["/path/to/dir"]
Example :
VOLUME ["/data"]
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"
notes :
A volume can exist in a specified directory of one or more containers , This directory bypasses the federated file system , And has the following functions
1 Volumes can be shared and reused between containers
2 Containers do not have to share volumes with other containers
3 Changes to volumes take effect immediately
4 Changes to the volume do not affect the mirror
5 The volume will always be there , Until no container is using it 

WORKDIR: working directory , Be similar to cd command

 Format :
WORKDIR /path/to/workdir
Example :
WORKDIR /a ( The working directory is /a)
WORKDIR b ( The working directory is /a/b)
WORKDIR c ( The working directory is /a/b/c)
notes :
adopt WORKDIR After setting the working directory ,Dockerfile Command after RUN、CMD、ENTRYPOINT、ADD、COPY And other commands will be executed in this directory . In the use of docker run When running the container , Can pass -w Parameters override the working directory set at build time .

USER: Specifies the user name or when the container is run UID, Follow up RUN The specified user is also used . Use USER When specifying users , User name is available 、UID or GID, Or a combination of the two . When the service does not require administrator privileges , This command lets you specify the run user . And you can create the users you need before

 Format :
USER user
USER user:group
USER uid
USER uid:gid
USER user:gid
USER uid:group
Example :
USER www
notes :
Use USER After specifying users ,Dockerfile Command after RUN、CMD、ENTRYPOINT Will use this user . After the mirror is built , adopt docker run When running the container , Can pass -u Parameter to override the specified user .

ARG: Used to specify variables passed to the build runtime

 Format :
ARG <name>[=<default value>]
Example :
ARG site
ARG build_user=www

ONBUILD: Used to set mirror trigger

 Format :
Example :
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
notes :
When the built image is used as the basic image of other images , The trigger in the image will be triggered by the key 

A diagram explains the meaning of common instructions


Dockerfile Case a

# This my first nginx Dockerfile
# Version 1.0
# Base images base image
FROM centos
#MAINTAINER Maintainer information
MAINTAINER tianfeiyu
#ENV Set the environment variable
ENV PATH /usr/local/nginx/sbin:$PATH
#ADD File in current directory , Copying the past will automatically decompress
ADD nginx-1.8.0.tar.gz /usr/local/
ADD epel-release-latest-7.noarch.rpm /usr/local/
#RUN Execute the following command
RUN rpm -ivh /usr/local/epel-release-latest-7.noarch.rpm
RUN yum install -y wget lftp gcc gcc-c++ make openssl-devel pcre-devel pcre && yum clean all
RUN useradd -s /sbin/nologin -M www
#WORKDIR amount to cd
WORKDIR /usr/local/nginx-1.8.0
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre && make && make install
RUN echo "daemon off;" >> /etc/nginx.conf
#EXPOSE Port mapping
#CMD Run the following command
CMD ["nginx"]

Dockerfile build Use

Ready to file , artifacts docker Mirror image

# stay /opt/DjangoProject Next new one dockerfile-test Folder
# ls
CentOS-Base.repo Dockerfile epel.repo
# artifacts docker Mirror command , The relative path is Dockerfile Execution under the same level
docker build .
# After building the image , View image file
docker images
# The generated iamges name
docker tag 6f0 flask-docker
# Run the image and access the test
docker run -d -p 7777:8080 6f0 # -p You can specify ports to map

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云