Skip to content

Commit fb86e8d

Browse files
authored
Add UBI-based Docker images (#48710)
This commit adds Docker images based on the UBI base image.
1 parent 83ef155 commit fb86e8d

File tree

10 files changed

+77
-41
lines changed

10 files changed

+77
-41
lines changed

distribution/docker/build.gradle

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,31 @@ dependencies {
2121
restSpec project(':rest-api-spec')
2222
}
2323

24-
ext.expansions = { oss, local ->
24+
ext.expansions = { oss, ubi, local ->
2525
final String classifier = 'linux-x86_64'
2626
final String elasticsearch = oss ? "elasticsearch-oss-${VersionProperties.elasticsearch}-${classifier}.tar.gz" : "elasticsearch-${VersionProperties.elasticsearch}-${classifier}.tar.gz"
2727
return [
28+
'base_image' : ubi ? 'registry.access.redhat.com/ubi7/ubi-minimal:7.7' : 'centos:7',
2829
'build_date' : project.ext.buildDate,
2930
'elasticsearch' : elasticsearch,
3031
'git_revision' : project.ext.gitRevision,
3132
'license' : oss ? 'Apache-2.0' : 'Elastic-License',
33+
'package_manager' : ubi ? 'microdnf' : 'yum',
3234
'source_elasticsearch': local ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
3335
'version' : VersionProperties.elasticsearch
3436
]
3537
}
3638

37-
private static String files(final boolean oss) {
38-
return "build/${ oss ? 'oss-' : ''}docker"
39+
private static String buildPath(final boolean oss, final boolean ubi) {
40+
return "build/${ oss ? 'oss-' : ''}${ ubi ? 'ubi-' : ''}docker"
3941
}
4042

41-
private static String taskName(final String prefix, final boolean oss, final String suffix) {
42-
return "${prefix}${oss ? 'Oss' : ''}${suffix}"
43+
private static String taskName(final String prefix, final boolean oss, final boolean ubi, final String suffix) {
44+
return "${prefix}${oss ? 'Oss' : ''}${ubi ? 'Ubi': ''}${suffix}"
4345
}
4446

4547
project.ext {
46-
dockerBuildContext = { boolean oss, boolean local ->
48+
dockerBuildContext = { boolean oss, boolean ubi, boolean local ->
4749
copySpec {
4850
into('bin') {
4951
from project.projectDir.toPath().resolve("src/docker/bin")
@@ -55,24 +57,26 @@ project.ext {
5557
* by creating config files in oss or default build-context sub-modules.
5658
*/
5759
from project.projectDir.toPath().resolve("src/docker/config")
58-
from project.projectDir.toPath().resolve(oss ? "oss-docker-build-context" : "docker-build-context").resolve("src/docker/config")
60+
if (oss) {
61+
from project.projectDir.toPath().resolve("src/docker/config/oss")
62+
}
5963
}
6064

6165
from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) {
62-
expand(expansions(oss, local))
66+
expand(expansions(oss, ubi, local))
6367
}
6468
}
6569
}
6670
}
6771

68-
void addCopyDockerContextTask(final boolean oss) {
69-
task(taskName("copy", oss, "DockerContext"), type: Sync) {
70-
expansions(oss, true).each { k, v ->
72+
void addCopyDockerContextTask(final boolean oss, final boolean ubi) {
73+
task(taskName("copy", oss, ubi, "DockerContext"), type: Sync) {
74+
expansions(oss, ubi, true).each { k, v ->
7175
inputs.property(k, { v.toString() })
7276
}
73-
into files(oss)
77+
into buildPath(oss, ubi)
7478

75-
with dockerBuildContext(oss, true)
79+
with dockerBuildContext(oss, ubi, true)
7680

7781
if (oss) {
7882
from configurations.ossDockerSource
@@ -144,42 +148,42 @@ task integTest(type: Test) {
144148

145149
check.dependsOn integTest
146150

147-
void addBuildDockerImage(final boolean oss) {
148-
final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) {
149-
dependsOn taskName("copy", oss, "DockerContext")
151+
void addBuildDockerImage(final boolean oss, final boolean ubi) {
152+
final Task buildDockerImageTask = task(taskName("build", oss, ubi, "DockerImage"), type: LoggedExec) {
153+
dependsOn taskName("copy", oss, ubi, "DockerContext")
150154
List<String> tags
151155
if (oss) {
152156
tags = [
153-
"docker.elastic.co/elasticsearch/elasticsearch-oss:${VersionProperties.elasticsearch}",
154-
"elasticsearch-oss:test"
157+
"docker.elastic.co/elasticsearch/elasticsearch-oss:${VersionProperties.elasticsearch}${ubi ? '-ubi7' : ''}",
158+
"elasticsearch-oss:test${ubi ? '-ubi7' : ''}"
155159
]
156160
} else {
157161
tags = [
158-
"elasticsearch:${VersionProperties.elasticsearch}",
159-
"docker.elastic.co/elasticsearch/elasticsearch:${VersionProperties.elasticsearch}",
160-
"docker.elastic.co/elasticsearch/elasticsearch-full:${VersionProperties.elasticsearch}",
161-
"elasticsearch:test",
162+
"elasticsearch:${VersionProperties.elasticsearch}${ubi ? '-ubi7' : ''}",
163+
"docker.elastic.co/elasticsearch/elasticsearch:${VersionProperties.elasticsearch}${ubi ? '-ubi7' : ''}",
164+
"docker.elastic.co/elasticsearch/elasticsearch-full:${VersionProperties.elasticsearch}${ubi ? '-ubi7' : ''}",
165+
"elasticsearch:test${ubi ? '-ubi7' : ''}",
162166
]
163167
}
164168
executable 'docker'
165-
final List<String> dockerArgs = ['build', files(oss), '--pull', '--no-cache']
169+
final List<String> dockerArgs = ['build', buildPath(oss, ubi), '--pull', '--no-cache']
166170
for (final String tag : tags) {
167171
dockerArgs.add('--tag')
168172
dockerArgs.add(tag)
169173
}
170174
args dockerArgs.toArray()
171175
}
176+
assemble.dependsOn(buildDockerImageTask)
172177
BuildPlugin.requireDocker(buildDockerImageTask)
173178
}
174179

175180
for (final boolean oss : [false, true]) {
176-
addCopyDockerContextTask(oss)
177-
addBuildDockerImage(oss)
181+
for (final boolean ubi : [false, true]) {
182+
addCopyDockerContextTask(oss, ubi)
183+
addBuildDockerImage(oss, ubi)
184+
}
178185
}
179186

180-
assemble.dependsOn "buildOssDockerImage"
181-
assemble.dependsOn "buildDockerImage"
182-
183187
// We build the images used in compose locally, but the pull command insists on using a repository
184188
// thus we must disable it to prevent it from doing so.
185189
// Everything will still be pulled since we will build the local images on a pull
@@ -195,25 +199,26 @@ subprojects { Project subProject ->
195199
if (subProject.name.contains('docker-export')) {
196200
apply plugin: 'distribution'
197201

198-
final boolean oss = subProject.name.startsWith('oss')
202+
final boolean oss = subProject.name.contains('oss-')
203+
final boolean ubi = subProject.name.contains('ubi-')
199204

200-
def exportTaskName = taskName("export", oss, "DockerImage")
201-
def buildTaskName = taskName("build", oss, "DockerImage")
202-
def tarFile = "${parent.projectDir}/build/elasticsearch${oss ? '-oss' : ''}_test.${VersionProperties.elasticsearch}.docker.tar"
205+
def exportTaskName = taskName("export", oss, ubi, "DockerImage")
206+
def buildTaskName = taskName("build", oss, ubi, "DockerImage")
207+
def tarFile = "${parent.projectDir}/build/elasticsearch${oss ? '-oss' : ''}_test.${VersionProperties.elasticsearch}${ubi ? '-ubi7' : ''}.docker.tar"
203208

204209
final Task exportDockerImageTask = task(exportTaskName, type: LoggedExec) {
205210
executable 'docker'
206211
args "save",
207212
"-o",
208213
tarFile,
209-
"elasticsearch${oss ? '-oss' : ''}:test"
214+
"elasticsearch${oss ? '-oss' : ''}${ubi ? '-ubi7' : ''}:test"
210215
}
211216

212217
exportDockerImageTask.dependsOn(parent.tasks.getByName(buildTaskName))
213218

214219
artifacts.add('default', file(tarFile)) {
215220
type 'tar'
216-
name "elasticsearch${oss ? '-oss' : ''}"
221+
name "elasticsearch${oss ? '-oss' : ''}${ubi ? '-ubi7' : ''}"
217222
builtBy exportTaskName
218223
}
219224

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ task buildDockerBuildContext(type: Tar) {
55
compression = Compression.GZIP
66
archiveClassifier = "docker-build-context"
77
archiveBaseName = "elasticsearch"
8-
with dockerBuildContext(false, false)
8+
with dockerBuildContext(false, false, false)
99
}
1010

1111
assemble.dependsOn buildDockerBuildContext

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ task buildOssDockerBuildContext(type: Tar) {
55
compression = Compression.GZIP
66
archiveClassifier = "docker-build-context"
77
archiveBaseName = "elasticsearch-oss"
8-
with dockerBuildContext(true, false)
8+
with dockerBuildContext(true, false, false)
99
}
1010

1111
assemble.dependsOn buildOssDockerBuildContext
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'base'
2+
3+
task buildOssUbiDockerBuildContext(type: Tar) {
4+
extension = 'tar.gz'
5+
compression = Compression.GZIP
6+
archiveClassifier = "oss-ubi-docker-build-context"
7+
archiveBaseName = "elasticsearch"
8+
with dockerBuildContext(true, true, false)
9+
}
10+
11+
assemble.dependsOn buildOssUbiDockerBuildContext
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is intentionally blank. All configuration of the
2+
// export is done in the parent project.

distribution/docker/src/docker/Dockerfile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
# Set gid=0 and make group perms==owner perms
1212
################################################################################
1313

14-
FROM centos:7 AS builder
14+
FROM ${base_image} AS builder
15+
16+
RUN for iter in {1..10}; do ${package_manager} update --setopt=tsflags=nodocs -y && \
17+
${package_manager} install --setopt=tsflags=nodocs -y gzip shadow-utils tar && \
18+
${package_manager} clean all && exit_code=0 && break || exit_code=\$? && echo "${package_manager} error: retry \$iter in 10s" && sleep 10; done; \
19+
(exit \$exit_code)
1520

1621
ENV PATH /usr/share/elasticsearch/bin:\$PATH
1722

@@ -35,13 +40,13 @@ COPY config/elasticsearch.yml config/log4j2.properties config/
3540
# Add entrypoint
3641
################################################################################
3742

38-
FROM centos:7
43+
FROM ${base_image}
3944

4045
ENV ELASTIC_CONTAINER true
4146

42-
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \
43-
yum install -y --setopt=tsflags=nodocs nc && \
44-
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \
47+
RUN for iter in {1..10}; do ${package_manager} update --setopt=tsflags=nodocs -y && \
48+
${package_manager} install --setopt=tsflags=nodocs -y nc shadow-utils && \
49+
${package_manager} clean all && exit_code=0 && break || exit_code=\$? && echo "${package_manager} error: retry \$iter in 10s" && sleep 10; done; \
4550
(exit \$exit_code)
4651

4752
RUN groupadd -g 1000 elasticsearch && \
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'base'
2+
3+
task buildUbiDockerBuildContext(type: Tar) {
4+
extension = 'tar.gz'
5+
compression = Compression.GZIP
6+
archiveClassifier = "ubi-docker-build-context"
7+
archiveBaseName = "elasticsearch"
8+
with dockerBuildContext(false, true, false)
9+
}
10+
11+
assemble.dependsOn buildUbiDockerBuildContext
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is intentionally blank. All configuration of the
2+
// export is done in the parent project.

0 commit comments

Comments
 (0)