|
| 1 | +<% /* |
| 2 | + This file is passed through Groovy's SimpleTemplateEngine, so dollars and backslashes |
| 3 | + have to be escaped in order for them to appear in the final Dockerfile. You |
| 4 | + can also comment out blocks, like this one. See: |
| 5 | +
|
| 6 | + https://docs.groovy-lang.org/latest/html/api/groovy/text/SimpleTemplateEngine.html |
| 7 | +
|
| 8 | + We use control-flow tags in this file to conditionally render the content. The |
| 9 | + layout/presentation here has been adjusted so that it looks reasonable when rendered, |
| 10 | + at the slight expense of how it looks here. |
| 11 | +
|
| 12 | + Note that this file is also filtered to squash together newlines, so we can |
| 13 | + add as many newlines here as necessary to improve legibility. |
| 14 | +*/ %> |
| 15 | +
|
| 16 | +################################################################################ |
| 17 | +# Build stage 1 `builder`: |
| 18 | +# Extract Elasticsearch artifact |
| 19 | +################################################################################ |
| 20 | +
|
| 21 | +FROM ${base_image} AS builder |
| 22 | +
|
| 23 | +# Install required packages to extract the Elasticsearch distribution |
| 24 | +RUN apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get install -y curl |
| 25 | +
|
| 26 | +# `tini` is a tiny but valid init for containers. This is used to cleanly |
| 27 | +# control how ES and any child processes are shut down. |
| 28 | +# |
| 29 | +# The tini GitHub page gives instructions for verifying the binary using |
| 30 | +# gpg, but the keyservers are slow to return the key and this can fail the |
| 31 | +# build. Instead, we check the binary against the published checksum. |
| 32 | +RUN set -eux ; \\ |
| 33 | + tini_bin="" ; \\ |
| 34 | + case "\$(arch)" in \\ |
| 35 | + aarch64) tini_bin='tini-arm64' ;; \\ |
| 36 | + x86_64) tini_bin='tini-amd64' ;; \\ |
| 37 | + *) echo >&2 ; echo >&2 "Unsupported architecture \$(arch)" ; echo >&2 ; exit 1 ;; \\ |
| 38 | + esac ; \\ |
| 39 | + curl --retry 10 -S -L -O https://github.com/krallin/tini/releases/download/v0.19.0/\${tini_bin} ; \\ |
| 40 | + curl --retry 10 -S -L -O https://github.com/krallin/tini/releases/download/v0.19.0/\${tini_bin}.sha256sum ; \\ |
| 41 | + sha256sum -c \${tini_bin}.sha256sum ; \\ |
| 42 | + rm \${tini_bin}.sha256sum ; \\ |
| 43 | + mv \${tini_bin} /bin/tini ; \\ |
| 44 | + chmod 0555 /bin/tini |
| 45 | +
|
| 46 | +RUN mkdir /usr/share/elasticsearch |
| 47 | +WORKDIR /usr/share/elasticsearch |
| 48 | +
|
| 49 | +RUN curl --retry 10 -S -L --output /tmp/elasticsearch.tar.gz https://artifacts-no-kpi.elastic.co/downloads/elasticsearch/elasticsearch-${version}-linux-\$(arch).tar.gz |
| 50 | +
|
| 51 | +RUN tar -zxf /tmp/elasticsearch.tar.gz --strip-components=1 |
| 52 | +
|
| 53 | +# The distribution includes a `config` directory, no need to create it |
| 54 | +COPY ${config_dir}/elasticsearch.yml config/ |
| 55 | +COPY ${config_dir}/log4j2.properties config/log4j2.docker.properties |
| 56 | +
|
| 57 | +# 1. Configure the distribution for Docker |
| 58 | +# 2. Create required directory |
| 59 | +# 3. Move the distribution's default logging config aside |
| 60 | +# 4. Move the generated docker logging config so that it is the default |
| 61 | +# 5. Reset permissions on all directories |
| 62 | +# 6. Reset permissions on all files |
| 63 | +# 7. Make CLI tools executable |
| 64 | +# 8. Make some directories writable. `bin` must be writable because |
| 65 | +# plugins can install their own CLI utilities. |
| 66 | +# 9. Make some files writable |
| 67 | +RUN sed -i -e 's/ES_DISTRIBUTION_TYPE=tar/ES_DISTRIBUTION_TYPE=docker/' bin/elasticsearch-env && \\ |
| 68 | + mkdir data && \\ |
| 69 | + mv config/log4j2.properties config/log4j2.file.properties && \\ |
| 70 | + mv config/log4j2.docker.properties config/log4j2.properties && \\ |
| 71 | + find . -type d -exec chmod 0555 {} + && \\ |
| 72 | + find . -type f -exec chmod 0444 {} + && \\ |
| 73 | + chmod 0555 bin/* jdk/bin/* jdk/lib/jspawnhelper modules/x-pack-ml/platform/linux-*/bin/* && \\ |
| 74 | + chmod 0775 bin config config/jvm.options.d data logs plugins && \\ |
| 75 | + find config -type f -exec chmod 0664 {} + |
| 76 | + |
| 77 | + |
| 78 | +################################################################################ |
| 79 | +# Build stage 2 (the actual Elasticsearch image): |
| 80 | +# |
| 81 | +# Copy elasticsearch from stage 1 |
| 82 | +# Add entrypoint |
| 83 | +################################################################################ |
| 84 | + |
| 85 | +FROM ${base_image} |
| 86 | + |
| 87 | +# Change default shell to bash, then install required packages with retries. |
| 88 | +RUN yes no | dpkg-reconfigure dash && \\ |
| 89 | +<%= retry.loop( |
| 90 | +package_manager, |
| 91 | + "export DEBIAN_FRONTEND=noninteractive && \n" + |
| 92 | + " ${package_manager} update && \n" + |
| 93 | + " ${package_manager} upgrade -y && \n" + |
| 94 | + " ${package_manager} install -y --no-install-recommends \n" + |
| 95 | + " ca-certificates curl netcat p11-kit unzip zip ${docker_base == 'cloud' ? 'wget' : '' } && \n" + |
| 96 | + " ${package_manager} clean && \n" + |
| 97 | + " rm -rf /var/lib/apt/lists/*" |
| 98 | +) %> |
| 99 | + |
| 100 | +RUN groupadd -g 1000 elasticsearch && \\ |
| 101 | + adduser --uid 1000 --gid 1000 --home /usr/share/elasticsearch elasticsearch && \\ |
| 102 | + adduser elasticsearch root && \\ |
| 103 | + chown -R 0:0 /usr/share/elasticsearch |
| 104 | + |
| 105 | +ENV ELASTIC_CONTAINER=true |
| 106 | + |
| 107 | +WORKDIR /usr/share/elasticsearch |
| 108 | + |
| 109 | +COPY --from=builder --chown=0:0 /usr/share/elasticsearch /usr/share/elasticsearch |
| 110 | +COPY --from=builder --chown=0:0 /bin/tini /bin/tini |
| 111 | + |
| 112 | +ENV PATH=/usr/share/elasticsearch/bin:\$PATH |
| 113 | +ENV SHELL=/bin/bash |
| 114 | +COPY ${bin_dir}/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh |
| 115 | + |
| 116 | +# 1. Sync the user and group permissions of /etc/passwd |
| 117 | +# 2. Set correct permissions of the entrypoint |
| 118 | +# 3. Ensure that there are no files with setuid or setgid, in order to mitigate "stackclash" attacks. |
| 119 | +# We've already run this in previous layers so it ought to be a no-op. |
| 120 | +# 4. Replace OpenJDK's built-in CA certificate keystore with the one from the OS |
| 121 | +# vendor. The latter is superior in several ways. |
| 122 | +# REF: https://github.com/elastic/elasticsearch-docker/issues/171 |
| 123 | +# 5. Tighten up permissions on the ES home dir (the permissions of the contents are handled earlier) |
| 124 | +# 6. You can't install plugins that include configuration when running as `elasticsearch` and the `config` |
| 125 | +# dir is owned by `root`, because the installed tries to manipulate the permissions on the plugin's |
| 126 | +# config directory. |
| 127 | +RUN chmod g=u /etc/passwd && \\ |
| 128 | + chmod 0555 /usr/local/bin/docker-entrypoint.sh && \\ |
| 129 | + find / -xdev -perm -4000 -exec chmod ug-s {} + && \\ |
| 130 | + chmod 0775 /usr/share/elasticsearch && \\ |
| 131 | + chown elasticsearch bin config config/jvm.options.d data logs plugins |
| 132 | + |
| 133 | +# Update "cacerts" bundle to use Ubuntu's CA certificates (and make sure it |
| 134 | +# stays up-to-date with changes to Ubuntu's store) |
| 135 | +COPY bin/docker-openjdk /etc/ca-certificates/update.d/docker-openjdk |
| 136 | +RUN /etc/ca-certificates/update.d/docker-openjdk |
| 137 | + |
| 138 | +EXPOSE 9200 9300 |
| 139 | + |
| 140 | +LABEL org.label-schema.build-date="${build_date}" \\ |
| 141 | + org.label-schema.license="${license}" \\ |
| 142 | + org.label-schema.name="Elasticsearch" \\ |
| 143 | + org.label-schema.schema-version="1.0" \\ |
| 144 | + org.label-schema.url="https://www.elastic.co/products/elasticsearch" \\ |
| 145 | + org.label-schema.usage="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\ |
| 146 | + org.label-schema.vcs-ref="${git_revision}" \\ |
| 147 | + org.label-schema.vcs-url="https://github.com/elastic/elasticsearch" \\ |
| 148 | + org.label-schema.vendor="Elastic" \\ |
| 149 | + org.label-schema.version="${version}" \\ |
| 150 | + org.opencontainers.image.created="${build_date}" \\ |
| 151 | + org.opencontainers.image.documentation="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\ |
| 152 | + org.opencontainers.image.licenses="${license}" \\ |
| 153 | + org.opencontainers.image.revision="${git_revision}" \\ |
| 154 | + org.opencontainers.image.source="https://github.com/elastic/elasticsearch" \\ |
| 155 | + org.opencontainers.image.title="Elasticsearch" \\ |
| 156 | + org.opencontainers.image.url="https://www.elastic.co/products/elasticsearch" \\ |
| 157 | + org.opencontainers.image.vendor="Elastic" \\ |
| 158 | + org.opencontainers.image.version="${version}" |
| 159 | + |
| 160 | +# Our actual entrypoint is `tini`, a minimal but functional init program. It |
| 161 | +# calls the entrypoint we provide, while correctly forwarding signals. |
| 162 | +ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"] |
| 163 | +# Dummy overridable parameter parsed by entrypoint |
| 164 | +CMD ["eswrapper"] |
| 165 | + |
| 166 | +USER 1000:0 |
| 167 | + |
| 168 | +################################################################################ |
| 169 | +# End of multi-stage Dockerfile |
| 170 | +################################################################################ |
0 commit comments