Skip to content

Commit 50c14b8

Browse files
authored
Restructure docker files for docker distributions (elastic#127960) (elastic#128186)
Restructures docker files for docker distributions - Put Dockerfiles in specific distro specific folders keeping "Dockerfile" naming convention - Allows better ide support - Allows easier renovate integration - Explicitly set base image in dockerfile - simplify renovate configuration - Cleanup DockerBase file to not contain ess fips base image information This lives now in the Dockerfile content directly * Workaround docker test issue * Fix labels for fips image (cherry picked from commit 38c90ca) # Conflicts: # build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DockerBase.java # distribution/docker/src/docker/dockerfiles/cloud_ess_fips/Dockerfile # renovate.json
1 parent 0b793ca commit 50c14b8

File tree

7 files changed

+415
-29
lines changed

7 files changed

+415
-29
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DockerBase.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,27 @@
1414
*/
1515
public enum DockerBase {
1616
// "latest" here is intentional, since the image name specifies "9"
17-
DEFAULT("redhat/ubi9-minimal:latest", "", "microdnf", "Dockerfile.default"),
17+
DEFAULT("redhat/ubi9-minimal:latest", "", "microdnf", "dockerfiles/default/Dockerfile"),
1818

1919
// The Iron Bank base image is UBI (albeit hardened), but we are required to parameterize the Docker build
2020
IRON_BANK("${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG}", "-ironbank", "yum", "Dockerfile"),
2121

2222
// Chainguard based wolfi image with latest jdk
23-
// This is usually updated via renovatebot
24-
// spotless:off
2523
WOLFI(
26-
"docker.elastic.co/wolfi/chainguard-base:latest@sha256:29150cd940cc7f69407d978d5a19c86f4d9e67cf44e4d6ded787a497e8f27c9a",
24+
null,
2725
"-wolfi",
2826
"apk",
29-
"Dockerfile"
27+
"dockerfiles/wolfi/Dockerfile"
3028
),
31-
// spotless:on
3229
// Based on WOLFI above, with more extras. We don't set a base image because
3330
// we programmatically extend from the wolfi image.
3431
CLOUD_ESS(null, "-cloud-ess", "apk", "Dockerfile.cloud-ess"),
3532

3633
CLOUD_ESS_FIPS(
37-
"docker.elastic.co/wolfi/chainguard-base-fips:sha256-ebfc3f1d7dba992231747a2e05ad1b859843e81b5e676ad342859d7cf9e425a7@sha256:ebfc3f1d7dba992231747a2e05ad1b859843e81b5e676ad342859d7cf9e425a7",
34+
null,
3835
"-cloud-ess-fips",
3936
"apk",
40-
"Dockerfile"
37+
"dockerfiles/cloud_ess_fips/Dockerfile"
4138
);
4239

4340
private final String image;

distribution/docker/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,10 @@ void addBuildDockerImageTask(Architecture architecture, DockerBase base) {
467467

468468
baseImages = [baseImage]
469469
buildArgs = buildArgsMap
470-
} else {
470+
} else if(base.image != null) {
471471
baseImages = [base.image]
472+
} else {
473+
baseImages = []
472474
}
473475

474476
Provider<DockerSupportService> serviceProvider = GradleUtils.getBuildService(
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
################################################################################
2+
# This Dockerfile was generated from the template at distribution/src/docker/Dockerfile
3+
#
4+
# Beginning of multi stage Dockerfile
5+
################################################################################
6+
7+
<% /*
8+
This file is passed through Groovy's SimpleTemplateEngine, so dollars and backslashes
9+
have to be escaped in order for them to appear in the final Dockerfile. You
10+
can also comment out blocks, like this one. See:
11+
12+
https://docs.groovy-lang.org/latest/html/api/groovy/text/SimpleTemplateEngine.html
13+
14+
We use control-flow tags in this file to conditionally render the content. The
15+
layout/presentation here has been adjusted so that it looks reasonable when rendered,
16+
at the slight expense of how it looks here.
17+
18+
Note that this file is also filtered to squash together newlines, so we can
19+
add as many newlines here as necessary to improve legibility.
20+
*/ %>
21+
22+
################################################################################
23+
# Build stage 1 `builder`:
24+
# Extract Elasticsearch artifact
25+
################################################################################
26+
27+
FROM docker.elastic.co/wolfi/chainguard-base-fips:latest@sha256:ebfc3f1d7dba992231747a2e05ad1b859843e81b5e676ad342859d7cf9e425a7 AS builder
28+
29+
# Install required packages to extract the Elasticsearch distribution
30+
RUN <%= retry.loop(package_manager, "export DEBIAN_FRONTEND=noninteractive && ${package_manager} update && ${package_manager} update && ${package_manager} add --no-cache curl") %>
31+
32+
RUN mkdir /usr/share/elasticsearch
33+
WORKDIR /usr/share/elasticsearch
34+
35+
# Fetch the appropriate Elasticsearch distribution for this architecture.
36+
# Keep this command on one line - it is replaced with a `COPY` during local builds.
37+
# It uses the `arch` shell command to fetch the correct distro for the build machine,
38+
# which is needed for Docker Hub builds.
39+
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
40+
41+
RUN tar -zxf /tmp/elasticsearch.tar.gz --strip-components=1
42+
43+
# The distribution includes a `config` directory, no need to create it
44+
COPY ${config_dir}/elasticsearch.yml config/
45+
COPY ${config_dir}/log4j2.properties config/log4j2.docker.properties
46+
47+
# 1. Configure the distribution for Docker
48+
# 2. Create required directory
49+
# 3. Move the distribution's default logging config aside
50+
# 4. Move the generated docker logging config so that it is the default
51+
# 5. Reset permissions on all directories
52+
# 6. Reset permissions on all files
53+
# 7. Make CLI tools executable
54+
# 8. Make some directories writable. `bin` must be writable because
55+
# plugins can install their own CLI utilities.
56+
# 9. Make some files writable
57+
RUN sed -i -e 's/ES_DISTRIBUTION_TYPE=tar/ES_DISTRIBUTION_TYPE=docker/' bin/elasticsearch-env && \\
58+
mkdir data && \\
59+
mv config/log4j2.properties config/log4j2.file.properties && \\
60+
mv config/log4j2.docker.properties config/log4j2.properties && \\
61+
find . -type d -exec chmod 0555 {} + && \\
62+
find . -type f -exec chmod 0444 {} + && \\
63+
chmod 0555 bin/* jdk/bin/* jdk/lib/jspawnhelper modules/x-pack-ml/platform/linux-*/bin/* && \\
64+
chmod 0775 bin config config/jvm.options.d data logs plugins && \\
65+
find config -type f -exec chmod 0664 {} +
66+
67+
# Add plugins infrastructure
68+
RUN mkdir -p /opt/plugins/archive
69+
RUN chmod -R 0555 /opt/plugins
70+
71+
RUN mkdir -p /fips/libs
72+
COPY fips/libs/*.jar /fips/libs/
73+
74+
COPY filebeat-${version}.tar.gz metricbeat-${version}.tar.gz /tmp/
75+
RUN set -eux ; \\
76+
for beat in filebeat metricbeat ; do \\
77+
if [ ! -s /tmp/\$beat-${version}.tar.gz ]; then \\
78+
echo "/tmp/\$beat-${version}.tar.gz is empty - cannot uncompress" 2>&1 ; \\
79+
exit 1 ; \\
80+
fi ; \\
81+
if ! tar tf /tmp/\$beat-${version}.tar.gz >/dev/null; then \\
82+
echo "/tmp/\$beat-${version}.tar.gz is corrupt - cannot uncompress" 2>&1 ; \\
83+
exit 1 ; \\
84+
fi ; \\
85+
mkdir -p /opt/\$beat ; \\
86+
tar xf /tmp/\$beat-${version}.tar.gz -C /opt/\$beat --strip-components=1 ; \\
87+
done
88+
89+
COPY plugins/*.zip /opt/plugins/archive/
90+
91+
RUN chown 1000:1000 /opt/plugins/archive/*
92+
RUN chmod 0444 /opt/plugins/archive/*
93+
94+
COPY fips/resources/fips_java.security /usr/share/elasticsearch/config/fips_java.security
95+
COPY fips/resources/fips_java.policy /usr/share/elasticsearch/config/fips_java.policy
96+
97+
WORKDIR /usr/share/elasticsearch/config
98+
99+
################################################################################
100+
# Build stage 2 (the actual Elasticsearch image):
101+
#
102+
# Copy elasticsearch from stage 1
103+
# Add entrypoint
104+
################################################################################
105+
106+
FROM docker.elastic.co/wolfi/chainguard-base-fips:latest@sha256:ebfc3f1d7dba992231747a2e05ad1b859843e81b5e676ad342859d7cf9e425a7
107+
108+
RUN <%= retry.loop(package_manager,
109+
"export DEBIAN_FRONTEND=noninteractive && \n" +
110+
" ${package_manager} update && \n" +
111+
" ${package_manager} upgrade && \n" +
112+
" ${package_manager} add --no-cache \n" +
113+
" bash java-cacerts curl libstdc++ libsystemd netcat-openbsd p11-kit p11-kit-trust posix-libc-utils shadow tini unzip zip zstd && \n" +
114+
" rm -rf /var/cache/apk/* "
115+
) %>
116+
117+
# Set Bash as the default shell for future commands
118+
SHELL ["/bin/bash", "-c"]
119+
120+
RUN groupadd -g 1000 elasticsearch && \
121+
adduser -G elasticsearch -u 1000 elasticsearch -D --home /usr/share/elasticsearch elasticsearch && \
122+
adduser elasticsearch root && \
123+
chown -R 0:0 /usr/share/elasticsearch
124+
125+
ENV ELASTIC_CONTAINER=true
126+
127+
WORKDIR /usr/share/elasticsearch
128+
129+
COPY --from=builder --chown=0:0 /usr/share/elasticsearch /usr/share/elasticsearch
130+
COPY --from=builder --chown=0:0 /fips/libs/*.jar /usr/share/elasticsearch/lib/
131+
COPY --from=builder --chown=0:0 /opt /opt
132+
133+
ENV ES_PLUGIN_ARCHIVE_DIR=/opt/plugins/archive
134+
ENV PATH=/usr/share/elasticsearch/bin:\$PATH
135+
ENV SHELL=/bin/bash
136+
COPY ${bin_dir}/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
137+
138+
# 1. Sync the user and group permissions of /etc/passwd
139+
# 2. Set correct permissions of the entrypoint
140+
# 3. Ensure that there are no files with setuid or setgid, in order to mitigate "stackclash" attacks.
141+
# We've already run this in previous layers so it ought to be a no-op.
142+
# 4. Replace OpenJDK's built-in CA certificate keystore with the one from the OS
143+
# vendor. The latter is superior in several ways.
144+
# REF: https://github.com/elastic/elasticsearch-docker/issues/171
145+
# 5. Tighten up permissions on the ES home dir (the permissions of the contents are handled earlier)
146+
# 6. You can't install plugins that include configuration when running as `elasticsearch` and the `config`
147+
# dir is owned by `root`, because the installed tries to manipulate the permissions on the plugin's
148+
# config directory.
149+
RUN chmod g=u /etc/passwd && \\
150+
chmod 0555 /usr/local/bin/docker-entrypoint.sh && \\
151+
find / -xdev -perm -4000 -exec chmod ug-s {} + && \\
152+
chmod 0775 /usr/share/elasticsearch && \\
153+
chown elasticsearch bin config config/jvm.options.d data logs plugins
154+
155+
RUN ln -sf /etc/ssl/certs/java/cacerts /usr/share/elasticsearch/jdk/lib/security/cacerts
156+
157+
# Convert cacerts (PKCS12) to BCFKS format using POSIX-compatible shell syntax
158+
RUN printf "\\n" | jdk/bin/keytool -importkeystore \
159+
-srckeystore /usr/share/elasticsearch/jdk/lib/security/cacerts \
160+
-srcstoretype PKCS12 \
161+
-destkeystore config/cacerts.bcfks \
162+
-deststorepass passwordcacert \
163+
-deststoretype BCFKS \
164+
-providerclass org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider \
165+
-providerpath lib/bc-fips-1.0.2.5.jar \
166+
-destprovidername BCFIPS
167+
168+
169+
## Add fips specific JVM options
170+
RUN cat <<EOF > /usr/share/elasticsearch/config/jvm.options.d/fips.options
171+
-Djavax.net.ssl.keyStoreType=BCFKS
172+
-Dorg.bouncycastle.fips.approved_only=true
173+
-Djava.security.properties=config/fips_java.security
174+
-Djava.security.policy=config/fips_java.policy
175+
-Djavax.net.ssl.trustStore=config/cacerts.bcfks
176+
-Djavax.net.ssl.trustStorePassword=passwordcacert
177+
EOF
178+
179+
EXPOSE 9200 9300
180+
181+
LABEL org.label-schema.build-date="${build_date}" \\
182+
org.label-schema.license="${license}" \\
183+
org.label-schema.name="Elasticsearch" \\
184+
org.label-schema.schema-version="1.0" \\
185+
org.label-schema.url="https://www.elastic.co/products/elasticsearch" \\
186+
org.label-schema.usage="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\
187+
org.label-schema.vcs-ref="${git_revision}" \\
188+
org.label-schema.vcs-url="https://github.com/elastic/elasticsearch" \\
189+
org.label-schema.vendor="Elastic" \\
190+
org.label-schema.version="${version}" \\
191+
org.opencontainers.image.created="${build_date}" \\
192+
org.opencontainers.image.documentation="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\
193+
org.opencontainers.image.licenses="${license}" \\
194+
org.opencontainers.image.revision="${git_revision}" \\
195+
org.opencontainers.image.source="https://github.com/elastic/elasticsearch" \\
196+
org.opencontainers.image.title="Elasticsearch" \\
197+
org.opencontainers.image.url="https://www.elastic.co/products/elasticsearch" \\
198+
org.opencontainers.image.vendor="Elastic" \\
199+
org.opencontainers.image.version="${version}"
200+
201+
LABEL name="Elasticsearch" \\
202+
maintainer="[email protected]" \\
203+
vendor="Elastic" \\
204+
version="${version}" \\
205+
release="1" \\
206+
summary="Elasticsearch" \\
207+
description="You know, for search."
208+
209+
RUN mkdir /licenses && ln LICENSE.txt /licenses/LICENSE
210+
211+
# Generate a stub command that will be overwritten at runtime
212+
RUN mkdir /app && \\
213+
echo -e '#!/bin/bash\\nexec /usr/local/bin/docker-entrypoint.sh eswrapper' > /app/elasticsearch.sh && \\
214+
chmod 0555 /app/elasticsearch.sh
215+
216+
ENTRYPOINT ["/sbin/tini", "--"]
217+
CMD ["/app/elasticsearch.sh"]
218+
219+
USER 1000:0
220+
221+
################################################################################
222+
# End of multi-stage Dockerfile
223+
################################################################################

distribution/docker/src/docker/Dockerfile.default renamed to distribution/docker/src/docker/dockerfiles/default/Dockerfile

File renamed without changes.

0 commit comments

Comments
 (0)