Dockerfile基础知识点

  1. #第一行必须指令基于的基础镜像
  2. From ubutu
  3. #维护者信息
  4. MAINTAINER heyazhou heyazhou@leconginfo.com
  5. #给镜像添加信息。使用docker inspect可查看镜像的相关信息
  6. LABEL <key>=<value> <key>=<value> <key>=<value> ...
  7. #指定一个环节变量
  8. ENV <key> <value> # 只能设置一个变量
  9. ENV <key>=<value> ... # 允许一次设置多个变量
  10. #ADD复制本地主机文件、目录或者远程文件 URLS 从 并且添加到容器指定路径中
  11. ADD <src>... <dest>
  12. #COPY复制新文件或者目录从 并且添加到容器指定路径中 。用法同ADD,唯一的不同是不能指定远程文件 URLS
  13. COPY <src>... <dest>
  14. #RUN在镜像构建之前执行任何命令,并把执行的结果传递给下一步使用
  15. RUN <command>
  16. RUN ["executable", "param1", "param2"]
  17. #VOLUME将宿主机上的目录文件挂在到对应的容器中
  18. VOLUME ["/data"]
  19. #ENTRYPOINT在镜像构建成功之后执行任何命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的。
  20. ENTRYPOINT ["executable", "param1", "param2"]
  21. ENTRYPOINT command param1 param2
  22. #EXPOSE 容器在运行中对外提供的端口能力,默认是TCP协议,可以指定UDP
  23. EXPOSE 80/tcp
  24. #CMD在镜像构建成功之后执行任何命令
  25. CMD ["executable","param1","param2"]
  26. CMD ["param1","param2"]
  27. CMD command param1 param2

构建镜像

编写Dockerfile文件,符合要求规范。Dockerfile文件命名为“Dockerfile”
在Dockerfile所在目录执行构建命令,sudo docker build ``-t 镜像名称:版本号`` ``.

构建初始化SQL脚本的MySQL镜像

Dockerfile内容

  1. #基础镜像
  2. FROM mysql:latest
  3. #作者
  4. MAINTAINER heyazhou <heyazhou@leconginfo.com>
  5. #定义会被容器自动执行的目录
  6. ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
  7. #定义sql文件名
  8. ENV INIT_FILE init_db.sql
  9. #把要执行的shell文件放到/docker-entrypoint-initdb.d/目录下,容器会自动执行这个shell
  10. COPY ./$INIT_FILE $AUTO_RUN_DIR/
  11. #给执行文件增加可执行权限
  12. RUN chmod a+x $AUTO_RUN_DIR/$INIT_FILE
  13. CMD ["mysqld"]

构建镜像

  1. sudo docker build -t init-mysql .

构建初始化SQL脚本的MySQL+Nginx镜像

Dockerfile内容

  1. #采用构建初始化SQL脚本的MySQL镜像
  2. FROM init-mysql:latest
  3. LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
  4. ENV NGINX_VERSION 1.13.12-1~stretch
  5. ENV NJS_VERSION 1.13.12.0.2.0-1~stretch
  6. RUN set -x \
  7. && apt-get update \
  8. && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 apt-transport-https ca-certificates \
  9. && \
  10. NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
  11. found=''; \
  12. for server in \
  13. ha.pool.sks-keyservers.net \
  14. hkp://keyserver.ubuntu.com:80 \
  15. hkp://p80.pool.sks-keyservers.net:80 \
  16. pgp.mit.edu \
  17. ; do \
  18. echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
  19. apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
  20. done; \
  21. test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
  22. apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
  23. && dpkgArch="$(dpkg --print-architecture)" \
  24. && nginxPackages=" \
  25. nginx=${NGINX_VERSION} \
  26. nginx-module-xslt=${NGINX_VERSION} \
  27. nginx-module-geoip=${NGINX_VERSION} \
  28. nginx-module-image-filter=${NGINX_VERSION} \
  29. nginx-module-njs=${NJS_VERSION} \
  30. " \
  31. && case "$dpkgArch" in \
  32. amd64|i386) \
  33. # arches officialy built by upstream
  34. echo "deb https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list \
  35. && apt-get update \
  36. ;; \
  37. *) \
  38. # we're on an architecture upstream doesn't officially build for
  39. # let's build binaries from the published source packages
  40. echo "deb-src https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list \
  41. \
  42. # new directory for storing sources and .deb files
  43. && tempDir="$(mktemp -d)" \
  44. && chmod 777 "$tempDir" \
  45. # (777 to ensure APT's "_apt" user can access it too)
  46. \
  47. # save list of currently-installed packages so build dependencies can be cleanly removed later
  48. && savedAptMark="$(apt-mark showmanual)" \
  49. \
  50. # build .deb files from upstream's source packages (which are verified by apt-get)
  51. && apt-get update \
  52. && apt-get build-dep -y $nginxPackages \
  53. && ( \
  54. cd "$tempDir" \
  55. && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" \
  56. apt-get source --compile $nginxPackages \
  57. ) \
  58. # we don't remove APT lists here because they get re-downloaded and removed later
  59. \
  60. # reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
  61. # (which is done after we install the built packages so we don't have to redownload any overlapping dependencies)
  62. && apt-mark showmanual | xargs apt-mark auto > /dev/null \
  63. && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \
  64. \
  65. # create a temporary local APT repo to install from (so that dependency resolution can be handled by APT, as it should be)
  66. && ls -lAFh "$tempDir" \
  67. && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) \
  68. && grep '^Package: ' "$tempDir/Packages" \
  69. && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list \
  70. # work around the following APT issue by using "Acquire::GzipIndexes=false" (overriding "/etc/apt/apt.conf.d/docker-gzip-indexes")
  71. # Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied)
  72. # ...
  73. # E: Failed to fetch store:/var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied)
  74. && apt-get -o Acquire::GzipIndexes=false update \
  75. ;; \
  76. esac \
  77. \
  78. && apt-get install --no-install-recommends --no-install-suggests -y \
  79. $nginxPackages \
  80. gettext-base \
  81. && apt-get remove --purge --auto-remove -y apt-transport-https ca-certificates && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \
  82. \
  83. # if we have leftovers from building, let's purge them (including extra, unnecessary build deps)
  84. && if [ -n "$tempDir" ]; then \
  85. apt-get purge -y --auto-remove \
  86. && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \
  87. fi
  88. # forward request and error logs to docker log collector
  89. RUN ln -sf /dev/stdout /var/log/nginx/access.log \
  90. && ln -sf /dev/stderr /var/log/nginx/error.log
  91. EXPOSE 80
  92. STOPSIGNAL SIGTERM
  93. CMD ["nginx", "-g", "daemon off;"]
  94. CMD ["mysqld"]


构建镜像

  1. sudo docker build -t init-mysql-nginx .