[关闭]
@JunQiu 2018-11-28T10:54:20.000000Z 字数 5517 阅读 1237

docker_(dockerFile、composeFile)

summary_2018/07 docker


1、日常工作

1.1、docker:dockerFile document
1.2、docker:composeFile document

2、技术学习

2.1、docker:dockerFile document
  1. ## .docekrigore
  2. docker CLI将上下文发送到docker守护程序之前,它会查找.dockerignore在上下文的根目录中指定的文件。如果此文件存在,CLI将修改上下文以排除与其中的模式匹配的文件和目录。这有助于避免不必要地将大型或敏感文件和目录发送到守护程序.
  3. 匹配是使用Go filepath.Match规则完成的。
  4. ## Example
  5. # Use an official Python runtime as a parent image
  6. FROM python:2.7-slim
  7. # Set the working directory to /app
  8. WORKDIR /app
  9. # Copy the current directory contents into the container at /app
  10. ADD . /app
  11. # Install any needed packages specified in requirements.txt
  12. // requirements.txt python的依赖管理文件,类似于node package.json文件
  13. RUN pip install --trusted-host pypi.python.org -r requirements.txt
  14. # Make port 80 available to the world outside this container
  15. EXPOSE 80/udp
  16. EXPOSE 80/tcp
  17. # Define environment variable
  18. ENV NAME World
  19. # Run app.py when the container launches
  20. CMD ["python", "app.py"]
  21. 一些常见的指令,其余见原文
  22. ## WORKDIR
  23. The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile(为dockerfile中的这些指令设置工作目录,b不设置在根目录在/)
  24. For example:
  25. WORKDIR /a
  26. WORKDIR b
  27. WORKDIR c
  28. RUN pwd
  29. The output of the final pwd command in this Dockerfile would be /a/b/c.
  30. ## FROM
  31. FROM指令初始化新的构建阶段并为后续指令设置基本映像。
  32. 指令:
  33. FROM <image> [AS <name>]
  34. FROM <image>[:<tag>] [AS <name>]
  35. FROM <image>[@<digest>] [AS <name>]
  36. ARGARG指令定义了一个变量,用户可以docker build使用该--build-arg <varname>=<value> 标志在构建时将该变量传递给构建器。
  37. ARG <name>[=<default value>]
  38. //Docker有一组预定义ARG变量,您可以ARG在Dockerfile中使用相应的指令。
  39. //tag或digest值是可选的。如果省略其中任何一个,则构建器默认采用latest标记。如果找不到tag值,构建器将返回错误
  40. ## ENV
  41. The first form, ENV <key> <value>, will set a single variable to a value. The entire string after the first space will be treated as the <value> - including whitespace characters.
  42. The second form, ENV <key>=<value> ..., allows for multiple variables to be set at one time.
  43. Environment variables are notated in the Dockerfile either with $variable_name or ${variable_name}.
  44. Environment variable substitution will use the same value for each variable throughout the entire instruction.
  45. ENV abc=hello
  46. ENV abc=bye def=$abc #def=hello,it is part of the same instruction that set abc to bye.
  47. ENV ghi=$abc # ghi=bye ,it is not part of the same instruction that set abc to bye.
  48. # abc=bye
  49. ## RUN:在构建image时运行
  50. RUN has 2 forms:
  51. RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  52. RUN ["executable", "param1", "param2"] (execform)
  53. //To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell.
  54. For example, RUN ["/bin/bash", "-c", "echo hello"]
  55. TipsThe exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).
  56. ## CMD:ENTRYPOINT和CMD在容器运行(run、start)时运行。
  57. CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  58. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  59. CMD command param1 param2 (shell form)
  60. ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
  61. ENTRYPOINT command param1 param2 (shell form)
  62. ## ADD(比COPY强大)
  63. The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.
  64. ADD [--chown=<user>:<group>] <src>... <dest>
  65. ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]
  66. ADD obeys the following rules:
  67. 如果<src>是URL并且<dest>不以尾部斜杠结尾,则从URL下载文件并将其复制到<dest>。
  68. 如果<src>是URL并且<dest>以尾部斜杠结尾,则从URL推断文件名并将文件下载到 <dest>/<filename>。
  69. 如果<src>是目录,则复制目录的全部内容,包括文件系统元数据。(不复制目录本身,和copy相同)
  70. ## COPY
  71. COPY指令从中复制新文件或目录<src>,并将它们添加到路径中容器的文件系统中<dest>。
  72. COPY [--chown=<user>:<group>] <src>... <dest>
  73. COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]
  74. COPY obeys the following rules:
  75. The <src> path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
  76. If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.
  77. ## EXPOSE
  78. The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
  79. EXPOSE <port> [<port>/<protocol>...]
  80. //To expose on both TCP and UDP, include two lines:
  81. EXPOSE 80/tcp
  82. EXPOSE 80/udp
  83. In this case, if you use -P with docker run, the port will be exposed once for TCP and once for UDP.
  84. //Regardless of the EXPOSE settings, you can override them at runtime by using the -p flag. For example
  85. docker run -p 80:80/tcp -p 80:80/udp ...
  86. Tips:其实expose只有两个作用,一是让使用的镜像的人明白服务监听的端口,第二个在运行时使用随机端口映射,也就是 docker run -P 时,会自动随机映射 EXPOSE 的端口。但是端口一般都是抽离出来以环境变量的方式注入,没有很大的实际意义。
2.2、docker:composeFile document
  1. ## 我只了解一些常见的字段
  2. Example:
  3. version: "3"
  4. services:
  5. redis:
  6. image: redis:alpine # image:image URL
  7. ports:
  8. - "6379"
  9. networks:
  10. - frontend
  11. deploy: # 指定与部署和运行服务相关的配置。
  12. replicas: 2 # mode(mode: global):Either global (exactly one container per swarm node) or replicated (a specified number of containers). The default is replicated.
  13. update_config:
  14. parallelism: 2
  15. delay: 10s
  16. restart_policy: # 重启策略condition: One of none, on-failure or any (default: any).
  17. condition: on-failure
  18. delay: 5s # 间隔时间 (默认值:0)
  19. max_attempts: 3 # 尝试次数(默认值:永不放弃)
  20. window: 120s # 在决定重启是否成功之前等待多长时间(默认值:立即决定)
  21. db:
  22. image: postgres:9.4
  23. volumes:
  24. - db-data:/var/lib/postgresql/data
  25. networks:
  26. - backend
  27. deploy:
  28. resources: # 资源配置
  29. limits: # cpu和内存限制
  30. cpus: '0.50'
  31. memory: 50M
  32. reservations: # 保留20M了内存和0.25CPU时间(始终可用)
  33. cpus: '0.25'
  34. memory: 20M
  35. placement:
  36. constraints: [node.role == manager]
  37. vote:
  38. image: dockersamples/examplevotingapp_vote:before
  39. ports:
  40. - "5000:80" # 端口映射
  41. networks:
  42. - frontend
  43. depends_on: # 依赖项,比如先启动依赖
  44. - redis
  45. deploy:
  46. replicas: 2
  47. update_config:
  48. parallelism: 2
  49. restart_policy:
  50. condition: on-failure
  51. networks:
  52. frontend:
  53. backend:
  54. volumes:
  55. db-data:
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注