Skip to content

Commit a467ad0

Browse files
committed
Optimized mongodb-agent docker layers
1 parent febd854 commit a467ad0

File tree

3 files changed

+98
-81
lines changed

3 files changed

+98
-81
lines changed
Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
ARG imagebase
22
FROM ${imagebase} as base
33

4+
FROM registry.access.redhat.com/ubi9/ubi-minimal as builder
5+
6+
RUN microdnf install -y tar gzip && microdnf clean all
7+
8+
COPY --from=base /data/mongodb-agent.tar.gz /data/mongodb-tools.tgz /tmp/
9+
RUN tar xfz /tmp/mongodb-agent.tar.gz -C /opt && \
10+
tar xfz /tmp/mongodb-tools.tgz -C /opt && \
11+
rm /tmp/*.tgz /tmp/*.tar.gz
12+
13+
RUN chmod +x /opt/mongodb-mms-automation-agent-*/mongodb-mms-automation-agent
14+
415
FROM registry.access.redhat.com/ubi9/ubi-minimal
516

617
ARG version
@@ -15,46 +26,32 @@ LABEL name="MongoDB Agent" \
1526

1627
# Replace libcurl-minimal and curl-minimal with the full versions
1728
# https://bugzilla.redhat.com/show_bug.cgi?id=1994521
18-
RUN microdnf install -y libssh libpsl libbrotli \
29+
RUN microdnf install -y libssh libpsl libbrotli \
1930
&& microdnf download curl libcurl \
2031
&& rpm -Uvh --nodeps --replacefiles "*curl*$( uname -i ).rpm" \
21-
&& microdnf remove -y libcurl-minimal curl-minimal
22-
23-
RUN microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 nss_wrapper
24-
# Copy-pasted from https://www.mongodb.com/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat-tarball/
25-
RUN microdnf install -y --disableplugin=subscription-manager \
26-
cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs openldap openssl xz-libs
27-
# Dependencies for the Agent
28-
RUN microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 \
29-
net-snmp \
30-
net-snmp-agent-libs
31-
RUN microdnf install -y --disableplugin=subscription-manager \
32-
hostname tar gzip procps jq \
32+
&& microdnf remove -y libcurl-minimal curl-minimal \
33+
&& microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 nss_wrapper \
34+
&& microdnf install -y --disableplugin=subscription-manager \
35+
cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs openldap openssl xz-libs \
36+
&& microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 \
37+
net-snmp \
38+
net-snmp-agent-libs \
39+
&& microdnf install -y --disableplugin=subscription-manager \
40+
hostname tar gzip procps jq \
3341
&& microdnf upgrade -y \
34-
&& rm -rf /var/lib/apt/lists/*
42+
&& microdnf clean all \
43+
&& mkdir -p /agent \
44+
/var/lib/mongodb-mms-automation \
45+
/var/log/mongodb-mms-automation \
46+
/var/lib/automation/config \
47+
&& chmod -R +wr /var/log/mongodb-mms-automation/ \
48+
&& touch /var/log/mongodb-mms-automation/readiness.log \
49+
&& chmod ugo+rw /var/log/mongodb-mms-automation/readiness.log \
50+
&& chmod -R +r /var/lib/automation/config
3551

36-
RUN mkdir -p /agent \
37-
&& mkdir -p /var/lib/mongodb-mms-automation \
38-
&& mkdir -p /var/log/mongodb-mms-automation/ \
39-
&& chmod -R +wr /var/log/mongodb-mms-automation/ \
40-
# ensure that the agent user can write the logs in OpenShift
41-
&& touch /var/log/mongodb-mms-automation/readiness.log \
42-
&& chmod ugo+rw /var/log/mongodb-mms-automation/readiness.log
43-
44-
45-
COPY --from=base /data/mongodb-agent.tar.gz /agent
46-
COPY --from=base /data/mongodb-tools.tgz /agent
4752
COPY --from=base /data/LICENSE /licenses/LICENSE
48-
49-
RUN tar xfz /agent/mongodb-agent.tar.gz \
50-
&& mv mongodb-mms-automation-agent-*/mongodb-mms-automation-agent /agent/mongodb-agent \
51-
&& chmod +x /agent/mongodb-agent \
52-
&& mkdir -p /var/lib/automation/config \
53-
&& chmod -R +r /var/lib/automation/config \
54-
&& rm /agent/mongodb-agent.tar.gz \
55-
&& rm -r mongodb-mms-automation-agent-*
56-
57-
RUN tar xfz /agent/mongodb-tools.tgz --directory /var/lib/mongodb-mms-automation/ && rm /agent/mongodb-tools.tgz
53+
COPY --from=builder /opt/mongodb-mms-automation-agent-*/mongodb-mms-automation-agent /agent/mongodb-agent
54+
COPY --from=builder /opt/mongodb-database-tools-*/bin/ /usr/local/bin/
5855

5956
USER 2000
6057
CMD ["/agent/mongodb-agent", "-cluster=/var/lib/automation/config/automation-config.json"]

docker/mongodb-agent/Dockerfile

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
ARG imagebase
2-
FROM ${imagebase} as base
2+
FROM ${imagebase} AS base
3+
4+
FROM registry.access.redhat.com/ubi9/ubi-minimal AS builder
5+
6+
RUN microdnf install -y tar gzip && microdnf clean all
7+
8+
COPY --from=base /data/mongodb_tools_ubi.tgz /data/mongodb_agent_ubi.tgz /tmp/
9+
10+
RUN tar xfz /tmp/mongodb_tools_ubi.tgz -C /opt && \
11+
tar xfz /tmp/mongodb_agent_ubi.tgz -C /opt && \
12+
rm /tmp/*.tgz
13+
14+
RUN chmod +x /opt/mongodb-mms-automation-agent-*/mongodb-mms-automation-agent
15+
RUN chmod +x /opt/mongodb-database-tools-*/bin/*
16+
17+
COPY --from=base /data/probe.sh \
18+
/data/readinessprobe \
19+
/data/version-upgrade-hook \
20+
/data/agent-launcher-lib.sh \
21+
/data/agent-launcher.sh \
22+
/opt/scripts/
23+
24+
RUN chmod +x /opt/scripts/*
325

426
FROM registry.access.redhat.com/ubi9/ubi-minimal
527

@@ -13,51 +35,37 @@ LABEL name="MongoDB Agent" \
1335
release="1" \
1436
maintainer="[email protected]"
1537

16-
COPY --from=base /data/probe.sh /opt/scripts/probe.sh
17-
COPY --from=base /data/readinessprobe /opt/scripts/readinessprobe
18-
COPY --from=base /data/version-upgrade-hook /opt/scripts/version-upgrade-hook
19-
COPY --from=base /data/agent-launcher-lib.sh /opt/scripts/agent-launcher-lib.sh
20-
COPY --from=base /data/agent-launcher.sh /opt/scripts/agent-launcher.sh
21-
COPY --from=base /data/LICENSE /licenses/LICENSE
22-
23-
# Replace libcurl-minimal and curl-minimal with the full versions
24-
# https://bugzilla.redhat.com/show_bug.cgi?id=1994521
25-
RUN microdnf install -y libssh libpsl libbrotli \
38+
RUN microdnf install -y libssh libpsl libbrotli \
2639
&& microdnf download curl libcurl \
2740
&& rpm -Uvh --nodeps --replacefiles "*curl*$( uname -i ).rpm" \
28-
&& microdnf remove -y libcurl-minimal curl-minimal
29-
30-
RUN microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 nss_wrapper
31-
# Copy-pasted from https://www.mongodb.com/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat-tarball/
32-
RUN microdnf install -y --disableplugin=subscription-manager \
33-
cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs openldap openssl xz-libs
34-
# Dependencies for the Agent
35-
RUN microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 \
36-
net-snmp \
37-
net-snmp-agent-libs
38-
RUN microdnf install -y --disableplugin=subscription-manager \
39-
hostname tar gzip procps jq \
41+
&& microdnf remove -y libcurl-minimal curl-minimal \
42+
&& microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 nss_wrapper \
43+
&& microdnf install -y --disableplugin=subscription-manager \
44+
cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs openldap openssl xz-libs \
45+
&& microdnf install -y --disableplugin=subscription-manager --setopt=install_weak_deps=0 \
46+
net-snmp \
47+
net-snmp-agent-libs \
48+
&& microdnf install -y --disableplugin=subscription-manager \
49+
hostname tar gzip procps jq \
4050
&& microdnf upgrade -y \
41-
&& rm -rf /var/lib/apt/lists/*
51+
&& microdnf clean all \
52+
&& mkdir -p /agent \
53+
/var/lib/mongodb-mms-automation \
54+
/var/log/mongodb-mms-automation \
55+
/var/lib/automation/config \
56+
&& chmod -R +wr /var/log/mongodb-mms-automation/ \
57+
&& touch /var/log/mongodb-mms-automation/readiness.log \
58+
&& chmod ugo+rw /var/log/mongodb-mms-automation/readiness.log \
59+
&& chmod -R +r /var/lib/automation/config
4260

61+
COPY --from=base /data/LICENSE /licenses/LICENSE
4362

44-
COPY --from=base /data/mongodb_tools_ubi.tgz /tools/mongodb_tools.tgz
45-
COPY --from=base /data/mongodb_agent_ubi.tgz /agent/mongodb_agent.tgz
46-
47-
RUN tar xfz /tools/mongodb_tools.tgz
48-
RUN mv mongodb-database-tools-*/bin/* /tools
49-
RUN chmod +x /tools/*
50-
RUN rm /tools/mongodb_tools.tgz
51-
RUN rm -rf /mongodb-database-tools-*
52-
53-
RUN tar xfz /agent/mongodb_agent.tgz
54-
RUN mv mongodb-mms-automation-agent-*/mongodb-mms-automation-agent /agent/mongodb-agent
55-
RUN chmod +x /agent/mongodb-agent
56-
RUN rm /agent/mongodb_agent.tgz
57-
RUN rm -rf mongodb-mms-automation-agent-*
63+
COPY --from=builder /opt/scripts/* /opt/scripts/
64+
COPY --from=builder /opt/mongodb-database-tools-*/bin /tools
65+
COPY --from=builder /opt/mongodb-mms-automation-agent-*/mongodb-mms-automation-agent /agent/mongodb-agent
5866

59-
RUN mkdir -p /var/lib/automation/config
60-
RUN chmod -R +r /var/lib/automation/config
67+
RUN mkdir -p /var/lib/automation/config \
68+
&& chmod -R +r /var/lib/automation/config
6169

6270
USER 2000
6371

lib/sonar/builders/docker.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,28 @@ def docker_build_cli(
8181
if not dockerfile_path.startswith("/"):
8282
dockerfile_path = f"{path}/{dockerfile_path}"
8383

84-
args = get_docker_build_cli_args(
84+
cli_command_args = get_docker_build_cli_args(
8585
path=path, dockerfile=dockerfile_path, tag=tag, buildargs=buildargs, labels=labels, platform=platform
8686
)
8787

88-
args_str = " ".join(args)
89-
logger.info(f"executing cli docker build: {args_str}")
88+
cli_command_args_str = " ".join(cli_command_args)
89+
logger.info(f"executing cli docker build: {cli_command_args_str}")
9090

91-
cp = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
92-
if cp.returncode != 0:
93-
raise SonarAPIError(cp.stderr)
91+
docker_build_process = subprocess.Popen(
92+
cli_command_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1
93+
)
94+
collected_output_lines = []
95+
if docker_build_process.stdout:
96+
for output_stream_line in iter(docker_build_process.stdout.readline, ""):
97+
newline_stripped_line = output_stream_line.rstrip()
98+
logger.info(newline_stripped_line)
99+
collected_output_lines.append(newline_stripped_line)
100+
docker_build_process.stdout.close()
101+
102+
process_exit_code = docker_build_process.wait()
103+
104+
if process_exit_code != 0:
105+
raise SonarAPIError("\n".join(collected_output_lines))
94106

95107

96108
def get_docker_build_cli_args(

0 commit comments

Comments
 (0)