1. FROM debian:bullseye-slim
    2. # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
    3. RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis
    4. # grab gosu for easy step-down from root
    5. # https://github.com/tianon/gosu/releases
    6. ENV GOSU_VERSION 1.14
    7. RUN set -eux; \
    8. savedAptMark="$(apt-mark showmanual)"; \
    9. apt-get update; \
    10. apt-get install -y --no-install-recommends ca-certificates dirmngr gnupg wget; \
    11. rm -rf /var/lib/apt/lists/*; \
    12. dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
    13. wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
    14. wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
    15. export GNUPGHOME="$(mktemp -d)"; \
    16. gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
    17. gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
    18. gpgconf --kill all; \
    19. rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
    20. apt-mark auto '.*' > /dev/null; \
    21. [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
    22. apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    23. chmod +x /usr/local/bin/gosu; \
    24. gosu --version; \
    25. gosu nobody true
    26. ENV REDIS_VERSION 7.0-rc3
    27. ENV REDIS_DOWNLOAD_URL https://github.com/redis/redis/archive/7.0-rc3.tar.gz
    28. ENV REDIS_DOWNLOAD_SHA a3775c84e2c57d78b8dbef539db4340f69d9af35ebfea7bba5ca528509338975
    29. RUN set -eux; \
    30. \
    31. savedAptMark="$(apt-mark showmanual)"; \
    32. apt-get update; \
    33. apt-get install -y --no-install-recommends \
    34. ca-certificates \
    35. wget \
    36. \
    37. dpkg-dev \
    38. gcc \
    39. libc6-dev \
    40. libssl-dev \
    41. make \
    42. ; \
    43. rm -rf /var/lib/apt/lists/*; \
    44. \
    45. wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"; \
    46. echo "$REDIS_DOWNLOAD_SHA *redis.tar.gz" | sha256sum -c -; \
    47. mkdir -p /usr/src/redis; \
    48. tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1; \
    49. rm redis.tar.gz; \
    50. \
    51. # disable Redis protected mode [1] as it is unnecessary in context of Docker
    52. # (ports are not automatically exposed when running inside Docker, but rather explicitly by specifying -p / -P)
    53. # [1]: https://github.com/redis/redis/commit/edd4d555df57dc84265fdfb4ef59a4678832f6da
    54. grep -E '^ *createBoolConfig[(]"protected-mode",.*, *1 *,.*[)],$' /usr/src/redis/src/config.c; \
    55. sed -ri 's!^( *createBoolConfig[(]"protected-mode",.*, *)1( *,.*[)],)$!\10\2!' /usr/src/redis/src/config.c; \
    56. grep -E '^ *createBoolConfig[(]"protected-mode",.*, *0 *,.*[)],$' /usr/src/redis/src/config.c; \
    57. # for future reference, we modify this directly in the source instead of just supplying a default configuration flag because apparently "if you specify any argument to redis-server, [it assumes] you are going to specify everything"
    58. # see also https://github.com/docker-library/redis/issues/4#issuecomment-50780840
    59. # (more exactly, this makes sure the default behavior of "save on SIGTERM" stays functional by default)
    60. \
    61. # https://github.com/jemalloc/jemalloc/issues/467 -- we need to patch the "./configure" for the bundled jemalloc to match how Debian compiles, for compatibility
    62. # (also, we do cross-builds, so we need to embed the appropriate "--build=xxx" values to that "./configure" invocation)
    63. gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
    64. extraJemallocConfigureFlags="--build=$gnuArch"; \
    65. # https://salsa.debian.org/debian/jemalloc/-/blob/c0a88c37a551be7d12e4863435365c9a6a51525f/debian/rules#L8-23
    66. dpkgArch="$(dpkg --print-architecture)"; \
    67. case "${dpkgArch##*-}" in \
    68. amd64 | i386 | x32) extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-page=12" ;; \
    69. *) extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-page=16" ;; \
    70. esac; \
    71. extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-hugepage=21"; \
    72. grep -F 'cd jemalloc && ./configure ' /usr/src/redis/deps/Makefile; \
    73. sed -ri 's!cd jemalloc && ./configure !&'"$extraJemallocConfigureFlags"' !' /usr/src/redis/deps/Makefile; \
    74. grep -F "cd jemalloc && ./configure $extraJemallocConfigureFlags " /usr/src/redis/deps/Makefile; \
    75. \
    76. export BUILD_TLS=yes; \
    77. make -C /usr/src/redis -j "$(nproc)" all; \
    78. make -C /usr/src/redis install; \
    79. \
    80. # TODO https://github.com/redis/redis/pull/3494 (deduplicate "redis-server" copies)
    81. serverMd5="$(md5sum /usr/local/bin/redis-server | cut -d' ' -f1)"; export serverMd5; \
    82. find /usr/local/bin/redis* -maxdepth 0 \
    83. -type f -not -name redis-server \
    84. -exec sh -eux -c ' \
    85. md5="$(md5sum "$1" | cut -d" " -f1)"; \
    86. test "$md5" = "$serverMd5"; \
    87. ' -- '{}' ';' \
    88. -exec ln -svfT 'redis-server' '{}' ';' \
    89. ; \
    90. \
    91. rm -r /usr/src/redis; \
    92. \
    93. apt-mark auto '.*' > /dev/null; \
    94. [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
    95. find /usr/local -type f -executable -exec ldd '{}' ';' \
    96. | awk '/=>/ { print $(NF-1) }' \
    97. | sort -u \
    98. | xargs -r dpkg-query --search \
    99. | cut -d: -f1 \
    100. | sort -u \
    101. | xargs -r apt-mark manual \
    102. ; \
    103. apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    104. \
    105. redis-cli --version; \
    106. redis-server --version
    107. RUN mkdir /data && chown redis:redis /data
    108. VOLUME /data
    109. WORKDIR /data
    110. COPY docker-entrypoint.sh /usr/local/bin/
    111. ENTRYPOINT ["docker-entrypoint.sh"]
    112. EXPOSE 6379
    113. CMD ["redis-server"]

    指令图:
    DockerFile结构、保留字指令 - 图1
    解释补充:
    ENV:
    ENV MY_PATH /usr/mytest
    这个环境变量可以在后续的任何RUN指令 中使用,这就如同在命令的前面指定了环境变量前缀一样
    也可以在其他指令中直接使用这些环境变量,比如:WORKDIR $MY_PATH

    CMD:
    CMD指令的格式和RUN相似,也是两种格式
    shell格式:CMD <命令>
    exec格式:CMD [“可执行文件”,”参数1”,”参数2”…] 必须使用双引号
    参数列表格式:CMD [“参数1”,”参数2”…]。在指定了ENTRYPOINT指令后,用CMD指定具体的参数。

    CMD会被docker run的参数覆盖,ENTRYPOINT不会被覆盖而是被追加