Skip to content

Commit 1723c7d

Browse files
committed
Push arch-specific logic from Gradle into Docker (#56503)
Docker informed us that for official multi-arch Docker builds, there needs to be a single Dockerfile and build context that can be used for each supported architecture. Therefore, rework the build to move the relevant architecture logic into the Dockerfile, and merge the aarch64 / x64 docker context builds.
1 parent 2fed50b commit 1723c7d

File tree

6 files changed

+93
-88
lines changed

6 files changed

+93
-88
lines changed

distribution/docker/build.gradle

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,47 @@ dependencies {
2828
}
2929

3030
ext.expansions = { architecture, oss, local ->
31-
String base_image = null
32-
String tini_arch = null
33-
String classifier = null
34-
switch (architecture) {
35-
case "aarch64":
36-
base_image = "arm64v8/centos:7"
37-
tini_arch = "arm64"
38-
classifier = "linux-aarch64"
39-
break;
40-
case "x64":
41-
base_image = "amd64/centos:7"
42-
tini_arch = "amd64"
43-
classifier = "linux-x86_64"
44-
break;
45-
default:
46-
throw new IllegalArgumentException("unrecongized architecture [" + architecture + "], must be one of (aarch64|x64)")
31+
String classifier
32+
if (local) {
33+
switch (architecture) {
34+
case "aarch64":
35+
classifier = "linux-aarch64"
36+
break
37+
case "x64":
38+
classifier = "linux-x86_64"
39+
break
40+
default:
41+
throw new IllegalArgumentException("Unrecognized architecture [" + architecture + "], must be one of (aarch64|x64)")
42+
}
43+
} else {
44+
/* When sourcing the Elasticsearch build remotely, the same Dockerfile needs
45+
* to be able to fetch the artifact for any supported platform. We can't make
46+
* the decision here. Bash will interpolate the `arch` command for us. */
47+
classifier = "linux-\$(arch)"
48+
}
49+
50+
final String elasticsearch = "elasticsearch-${oss ? 'oss-' : ''}${VersionProperties.elasticsearch}-${classifier}.tar.gz"
51+
52+
/* Both the following Dockerfile commands put the resulting artifact at
53+
* the same location, regardless of classifier, so that the commands that
54+
* follow in the Dockerfile don't have to know about the runtime
55+
* architecture. */
56+
String sourceElasticsearch
57+
if (local) {
58+
sourceElasticsearch = "COPY $elasticsearch /opt/elasticsearch.tar.gz"
59+
} else {
60+
sourceElasticsearch = """
61+
RUN curl --retry 8 -S -L \\
62+
--output /opt/elasticsearch.tar.gz \\
63+
https://artifacts.elastic.co/downloads/elasticsearch/$elasticsearch
64+
"""
4765
}
48-
final String elasticsearch = oss ? "elasticsearch-oss-${VersionProperties.elasticsearch}-${classifier}.tar.gz" : "elasticsearch-${VersionProperties.elasticsearch}-${classifier}.tar.gz"
66+
4967
return [
50-
'base_image' : base_image,
5168
'build_date' : BuildParams.buildDate,
52-
'elasticsearch' : elasticsearch,
5369
'git_revision' : BuildParams.gitRevision,
5470
'license' : oss ? 'Apache-2.0' : 'Elastic-License',
55-
'source_elasticsearch': local ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
56-
'tini_arch' : tini_arch,
71+
'source_elasticsearch': sourceElasticsearch,
5772
'version' : VersionProperties.elasticsearch
5873
]
5974
}

distribution/docker/docker-aarch64-build-context/build.gradle

Lines changed: 0 additions & 11 deletions
This file was deleted.

distribution/docker/docker-build-context/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ task buildDockerBuildContext(type: Tar) {
55
compression = Compression.GZIP
66
archiveClassifier = "docker-build-context"
77
archiveBaseName = "elasticsearch"
8-
with dockerBuildContext("x64", false, false)
8+
// Non-local builds don't need to specify an architecture.
9+
// Make this explicit via the string value.
10+
with dockerBuildContext("<remote>", false, false)
911
}
1012

1113
assemble.dependsOn buildDockerBuildContext

distribution/docker/oss-docker-aarch64-build-context/build.gradle

Lines changed: 0 additions & 11 deletions
This file was deleted.

distribution/docker/oss-docker-build-context/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ task buildOssDockerBuildContext(type: Tar) {
55
compression = Compression.GZIP
66
archiveClassifier = "docker-build-context"
77
archiveBaseName = "elasticsearch-oss"
8-
with dockerBuildContext("x64", true, false)
8+
// Non-local builds don't need to specify an architecture.
9+
// Make this explicit via the string value.
10+
with dockerBuildContext("<remote>", true, false)
911
}
1012

1113
assemble.dependsOn buildOssDockerBuildContext

distribution/docker/src/docker/Dockerfile

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,53 @@
33
#
44
# Beginning of multi stage Dockerfile
55
################################################################################
6+
<% /*
7+
This file is passed through Groovy's SimpleTemplateEngine, so dollars and backslashes
8+
have to be escaped in order for them to appear in the final Dockerfile. You
9+
can also comment out blocks, like this one. See:
610
11+
https://docs.groovy-lang.org/latest/html/api/groovy/text/SimpleTemplateEngine.html
12+
*/ %>
713
################################################################################
814
# Build stage 0 `builder`:
915
# Extract elasticsearch artifact
10-
# Install required plugins
1116
# Set gid=0 and make group perms==owner perms
1217
################################################################################
1318
14-
FROM ${base_image} AS builder
15-
16-
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \
17-
yum install --setopt=tsflags=nodocs -y wget gzip shadow-utils tar && \
18-
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \
19-
(exit \$exit_code)
19+
FROM centos:7 AS builder
2020
2121
# `tini` is a tiny but valid init for containers. This is used to cleanly
2222
# control how ES and any child processes are shut down.
2323
#
2424
# The tini GitHub page gives instructions for verifying the binary using
2525
# gpg, but the keyservers are slow to return the key and this can fail the
26-
# build. Instead, we check the binary against a checksum that they provide.
27-
RUN wget --no-cookies --quiet https://github.com/krallin/tini/releases/download/v0.19.0/tini-${tini_arch} \
28-
&& wget --no-cookies --quiet https://github.com/krallin/tini/releases/download/v0.19.0/tini-${tini_arch}.sha256sum \
29-
&& sha256sum -c tini-${tini_arch}.sha256sum \
30-
&& mv tini-${tini_arch} /tini \
31-
&& chmod +x /tini
26+
# build. Instead, we check the binary against the published checksum.
27+
RUN set -eux ; \\
28+
\\
29+
tini_bin="" ; \\
30+
case "\$(arch)" in \\
31+
aarch64) tini_bin='tini-arm64' ;; \\
32+
x86_64) tini_bin='tini-amd64' ;; \\
33+
*) echo >&2 ; echo >&2 "Unsupported architecture \$(arch)" ; echo >&2 ; exit 1 ;; \\
34+
esac ; \\
35+
curl --retry 8 -S -L -O https://github.com/krallin/tini/releases/download/v0.19.0/\${tini_bin} ; \\
36+
curl --retry 8 -S -L -O https://github.com/krallin/tini/releases/download/v0.19.0/\${tini_bin}.sha256sum ; \\
37+
sha256sum -c \${tini_bin}.sha256sum ; \\
38+
rm \${tini_bin}.sha256sum ; \\
39+
mv \${tini_bin} /tini ; \\
40+
chmod +x /tini
3241
3342
ENV PATH /usr/share/elasticsearch/bin:\$PATH
3443
35-
RUN groupadd -g 1000 elasticsearch && \
44+
RUN groupadd -g 1000 elasticsearch && \\
3645
adduser -u 1000 -g 1000 -d /usr/share/elasticsearch elasticsearch
3746
3847
WORKDIR /usr/share/elasticsearch
3948
4049
${source_elasticsearch}
4150
42-
RUN tar zxf /opt/${elasticsearch} --strip-components=1
43-
RUN grep ES_DISTRIBUTION_TYPE=tar /usr/share/elasticsearch/bin/elasticsearch-env \
44-
&& sed -i -e 's/ES_DISTRIBUTION_TYPE=tar/ES_DISTRIBUTION_TYPE=docker/' /usr/share/elasticsearch/bin/elasticsearch-env
51+
RUN tar zxf /opt/elasticsearch.tar.gz --strip-components=1
52+
RUN sed -i -e 's/ES_DISTRIBUTION_TYPE=tar/ES_DISTRIBUTION_TYPE=docker/' /usr/share/elasticsearch/bin/elasticsearch-env
4553
RUN mkdir -p config config/jvm.options.d data logs
4654
RUN chmod 0775 config config/jvm.options.d data logs
4755
COPY config/elasticsearch.yml config/log4j2.properties config/
@@ -53,20 +61,20 @@ RUN chmod 0660 config/elasticsearch.yml config/log4j2.properties
5361
# Add entrypoint
5462
################################################################################
5563
56-
FROM ${base_image}
64+
FROM centos:7
5765
5866
ENV ELASTIC_CONTAINER true
5967
6068
COPY --from=builder /tini /tini
6169
62-
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \
63-
yum install --setopt=tsflags=nodocs -y nc shadow-utils zip unzip && \
64-
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \
70+
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \\
71+
yum install --setopt=tsflags=nodocs -y nc shadow-utils zip unzip && \\
72+
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \\
6573
(exit \$exit_code)
6674
67-
RUN groupadd -g 1000 elasticsearch && \
68-
adduser -u 1000 -g 1000 -G 0 -d /usr/share/elasticsearch elasticsearch && \
69-
chmod 0775 /usr/share/elasticsearch && \
75+
RUN groupadd -g 1000 elasticsearch && \\
76+
adduser -u 1000 -g 1000 -G 0 -d /usr/share/elasticsearch elasticsearch && \\
77+
chmod 0775 /usr/share/elasticsearch && \\
7078
chgrp 0 /usr/share/elasticsearch
7179
7280
WORKDIR /usr/share/elasticsearch
@@ -81,32 +89,32 @@ ENV PATH /usr/share/elasticsearch/bin:\$PATH
8189

8290
COPY bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
8391

84-
RUN chmod g=u /etc/passwd && \
92+
RUN chmod g=u /etc/passwd && \\
8593
chmod 0775 /usr/local/bin/docker-entrypoint.sh
8694

8795
# Ensure that there are no files with setuid or setgid, in order to mitigate "stackclash" attacks.
8896
RUN find / -xdev -perm -4000 -exec chmod ug-s {} +
8997

9098
EXPOSE 9200 9300
9199

92-
LABEL org.label-schema.build-date="${build_date}" \
93-
org.label-schema.license="${license}" \
94-
org.label-schema.name="Elasticsearch" \
95-
org.label-schema.schema-version="1.0" \
96-
org.label-schema.url="https://www.elastic.co/products/elasticsearch" \
97-
org.label-schema.usage="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \
98-
org.label-schema.vcs-ref="${git_revision}" \
99-
org.label-schema.vcs-url="https://github.com/elastic/elasticsearch" \
100-
org.label-schema.vendor="Elastic" \
101-
org.label-schema.version="${version}" \
102-
org.opencontainers.image.created="${build_date}" \
103-
org.opencontainers.image.documentation="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \
104-
org.opencontainers.image.licenses="${license}" \
105-
org.opencontainers.image.revision="${git_revision}" \
106-
org.opencontainers.image.source="https://github.com/elastic/elasticsearch" \
107-
org.opencontainers.image.title="Elasticsearch" \
108-
org.opencontainers.image.url="https://www.elastic.co/products/elasticsearch" \
109-
org.opencontainers.image.vendor="Elastic" \
100+
LABEL org.label-schema.build-date="${build_date}" \\
101+
org.label-schema.license="${license}" \\
102+
org.label-schema.name="Elasticsearch" \\
103+
org.label-schema.schema-version="1.0" \\
104+
org.label-schema.url="https://www.elastic.co/products/elasticsearch" \\
105+
org.label-schema.usage="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\
106+
org.label-schema.vcs-ref="${git_revision}" \\
107+
org.label-schema.vcs-url="https://github.com/elastic/elasticsearch" \\
108+
org.label-schema.vendor="Elastic" \\
109+
org.label-schema.version="${version}" \\
110+
org.opencontainers.image.created="${build_date}" \\
111+
org.opencontainers.image.documentation="https://www.elastic.co/guide/en/elasticsearch/reference/index.html" \\
112+
org.opencontainers.image.licenses="${license}" \\
113+
org.opencontainers.image.revision="${git_revision}" \\
114+
org.opencontainers.image.source="https://github.com/elastic/elasticsearch" \\
115+
org.opencontainers.image.title="Elasticsearch" \\
116+
org.opencontainers.image.url="https://www.elastic.co/products/elasticsearch" \\
117+
org.opencontainers.image.vendor="Elastic" \\
110118
org.opencontainers.image.version="${version}"
111119

112120
ENTRYPOINT ["/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]

0 commit comments

Comments
 (0)