What is Docker File?

Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

How to use Docker File!

Docker build

  1. Usage: docker build [OPTIONS] PATH | URL | -
  2. Build an image from a Dockerfile
  3. Options:
  4. --add-host list Add a custom host-to-IP mapping (host:ip)
  5. --build-arg list Set build-time variables
  6. --cache-from strings Images to consider as cache sources
  7. --cgroup-parent string Optional parent cgroup for the container
  8. --compress Compress the build context using gzip
  9. --cpu-period int Limit the CPU CFS (Completely Fair
  10. Scheduler) period
  11. --cpu-quota int Limit the CPU CFS (Completely Fair
  12. Scheduler) quota
  13. -c, --cpu-shares int CPU shares (relative weight)
  14. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  15. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  16. --disable-content-trust Skip image verification (default true)
  17. -f, --file string Name of the Dockerfile (Default is
  18. 'PATH/Dockerfile')
  19. --force-rm Always remove intermediate containers
  20. --iidfile string Write the image ID to the file
  21. --isolation string Container isolation technology
  22. --label list Set metadata for an image
  23. -m, --memory bytes Memory limit
  24. --memory-swap bytes Swap limit equal to memory plus swap:
  25. '-1' to enable unlimited swap
  26. --network string Set the networking mode for the RUN
  27. instructions during build (default "default")
  28. --no-cache Do not use cache when building the image
  29. -o, --output stringArray Output destination (format:
  30. type=local,dest=path)
  31. --platform string Set platform if server is multi-platform
  32. capable
  33. --progress string Set type of progress output (auto, plain,
  34. tty). Use plain to show container output
  35. (default "auto")
  36. --pull Always attempt to pull a newer version of
  37. the image
  38. -q, --quiet Suppress the build output and print image
  39. ID on success
  40. --rm Remove intermediate containers after a
  41. successful build (default true)
  42. --secret stringArray Secret file to expose to the build (only
  43. if BuildKit enabled):
  44. id=mysecret,src=/local/secret
  45. --security-opt strings Security options
  46. --shm-size bytes Size of /dev/shm
  47. --squash Squash newly built layers into a single
  48. new layer
  49. --ssh stringArray SSH agent socket or keys to expose to the
  50. build (only if BuildKit enabled) (format:
  51. default|<id>[=<socket>|<key>[,<key>]])
  52. -t, --tag list Name and optionally a tag in the
  53. 'name:tag' format
  54. --target string Set the target build stage to build.
  55. --ulimit ulimit Ulimit options (default [])

Docker 指令

命令 解释
FROM 基础镜镜像,一切从这里开始构建
MAINTAINER 镜像是谁写的,姓名+邮箱
RUN 镜像构建的时候需要运行的命令
ADD 步骤:tomcat镜像,这个tomcat压缩包!添加内容
WORKDIR 镜像的工作目录
VOLUME 挂载的目录
EXPOSE 保留端口配置CMD指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD 当构建一个被继承DockerFile这个时候就会运行ONBUILD的指令。触发指令。
COPY 类似ADD,将我们文件拷贝到镜像中
ENV 构建的时候设置环境变量。

Docker history

查看镜像构建历史

  1. Usage: docker history [OPTIONS] IMAGE
  2. Show the history of an image
  3. Options:
  4. --format string Pretty-print images using a Go template
  5. -H, --human Print sizes and dates in human readable format
  6. (default true)
  7. --no-trunc Don't truncate output
  8. -q, --quiet Only show image IDs

构建自己的CentOS

  1. FROM centos
  2. LABEL author="HelloChen" \
  3. version=1.0 \
  4. description="This text illustrates that label-values can span multiple lines."
  5. ENV MYPATH=/usr/local
  6. WORKDIR $MYPATH
  7. RUN yum -y install vim
  8. RUN yum -y install net-tools
  9. EXPOSE 80
  10. CMD echo ${MYPATH}
  11. CMD /bin/bash