Skip to content

Commit 14a6c24

Browse files
Fix: OPA reduce image size (#1038)
* remove recursive chmod/chown in final image * adapted changelog * use check permissions script * consolidation * Update CHANGELOG.md Co-authored-by: Siegfried Weber <[email protected]> --------- Co-authored-by: Siegfried Weber <[email protected]>
1 parent 0ab91ac commit 14a6c24

File tree

2 files changed

+89
-62
lines changed

2 files changed

+89
-62
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file.
1111
- spark-connect-client: A new image for Spark connect tests and demos ([#1034])
1212
- nifi: check for correct permissions and ownerships in /stackable folder via
1313
`check-permissions-ownership.sh` provided in stackable-base image ([#1027]).
14+
- opa: check for correct permissions and ownerships in /stackable folder via
15+
`check-permissions-ownership.sh` provided in stackable-base image ([#1038]).
1416

1517
### Changed
1618

@@ -21,12 +23,14 @@ All notable changes to this project will be documented in this file.
2123

2224
- hadoop: reduce docker image size by removing the recursive chown/chmods in the final image ([#1029]).
2325
- nifi: reduce docker image size by removing the recursive chown/chmods in the final image ([#1027]).
26+
- opa: reduce docker image size by removing the recursive chown/chmods in the final image ([#1038]).
2427
- spark-k8s: reduce docker image size by removing the recursive chown/chmods in the final image ([#1042]).
2528
- Add `--locked` flag to `cargo install` commands for reproducible builds ([#1044]).
2629

2730
[#1027]: https://github.com/stackabletech/docker-images/pull/1027
2831
[#1029]: https://github.com/stackabletech/docker-images/pull/1029
2932
[#1034]: https://github.com/stackabletech/docker-images/pull/1034
33+
[#1038]: https://github.com/stackabletech/docker-images/pull/1038
3034
[#1042]: https://github.com/stackabletech/docker-images/pull/1042
3135
[#1044]: https://github.com/stackabletech/docker-images/pull/1044
3236
[#1050]: https://github.com/stackabletech/docker-images/pull/1050

opa/Dockerfile

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ FROM stackable/image/stackable-base AS opa-bundle-builder
66
ARG BUNDLE_BUILDER_VERSION
77

88
# Update image and install everything needed for Rustup & Rust
9-
RUN microdnf update \
10-
&& microdnf install \
11-
cmake \
12-
gcc \
13-
gcc-c++ \
14-
git \
15-
make \
16-
openssl-devel \
17-
pkg-config \
18-
systemd-devel \
19-
unzip \
20-
&& rm -rf /var/cache/yum
9+
RUN <<EOF
10+
microdnf update
11+
microdnf install \
12+
cmake \
13+
gcc \
14+
gcc-c++ \
15+
git \
16+
make \
17+
openssl-devel \
18+
pkg-config \
19+
systemd-devel \
20+
unzip
21+
rm -rf /var/cache/yum
22+
EOF
2123

2224
WORKDIR /
2325

@@ -29,6 +31,8 @@ cd ./opa-bundle-builder
2931
. "$HOME/.cargo/env"
3032
rustup toolchain install
3133
cargo --quiet build --release
34+
# set correct groups
35+
chmod -R g=u /opa-bundle-builder/target/release/
3236
EOF
3337

3438
FROM stackable/image/stackable-base AS multilog-builder
@@ -37,33 +41,38 @@ ARG DAEMONTOOLS_VERSION=0.76
3741

3842
COPY opa/daemontools /daemontools
3943

40-
RUN microdnf update && \
41-
microdnf install \
42-
gcc \
43-
gzip \
44-
make \
45-
patch \
46-
tar && \
47-
microdnf clean all \
48-
&& rm -rf /var/cache/yum
49-
50-
WORKDIR /daemontools
51-
52-
RUN tar xzf daemontools-${DAEMONTOOLS_VERSION}.tar.gz
44+
RUN <<EOF
45+
microdnf update
46+
microdnf install \
47+
gcc \
48+
gzip \
49+
make \
50+
patch \
51+
tar
52+
microdnf clean all
53+
rm -rf /var/cache/yum
54+
EOF
5355

54-
WORKDIR /daemontools/admin/daemontools-${DAEMONTOOLS_VERSION}/src
56+
RUN <<EOF
57+
cd /daemontools
58+
tar xzf daemontools-${DAEMONTOOLS_VERSION}.tar.gz
5559

56-
RUN patch < /daemontools/conf-cc.patch && \
57-
patch multilog.c < /daemontools/multilog_max_file_size.patch
60+
cd /daemontools/admin/daemontools-${DAEMONTOOLS_VERSION}/src
61+
patch < /daemontools/conf-cc.patch
62+
patch multilog.c < /daemontools/multilog_max_file_size.patch
5863

59-
WORKDIR /daemontools/admin/daemontools-${DAEMONTOOLS_VERSION}
64+
cd /daemontools/admin/daemontools-${DAEMONTOOLS_VERSION}
65+
package/install
6066

61-
RUN package/install
67+
# set correct groups
68+
chmod g=u /daemontools/admin/daemontools/command/multilog
69+
EOF
6270

6371
FROM stackable/image/stackable-base AS opa-builder
6472

6573
ARG PRODUCT
6674
ARG RELEASE
75+
ARG STACKABLE_USER_UID
6776
ARG TARGETARCH
6877
ARG TARGETOS
6978

@@ -73,19 +82,25 @@ ENV GOOS=$TARGETOS
7382
# gzip, tar - used to unpack the OPA source
7483
# git - needed by the cyclonedx-gomod tool to determine the version of OPA
7584
# golang - used to build OPA
76-
RUN microdnf update && \
77-
microdnf install \
78-
git \
79-
golang \
80-
gzip \
81-
tar && \
82-
microdnf clean all
85+
RUN <<EOF
86+
microdnf update
87+
microdnf install \
88+
git \
89+
golang \
90+
gzip \
91+
tar
92+
microdnf clean all
93+
EOF
8394

95+
COPY --chown=${STACKABLE_USER_UID}:0 opa/stackable/bin /stackable/opa/bin
96+
97+
RUN <<EOF
8498
# We use version 1.7.0, since a newer version of cyclonedx-gomod is not compatible with the version of Golang (>= 1.23.1)
85-
RUN go install github.com/CycloneDX/cyclonedx-gomod/cmd/[email protected]
86-
RUN curl "https://repo.stackable.tech/repository/packages/opa/opa_${PRODUCT}.tar.gz" -o opa.tar.gz && \
87-
tar -zxvf opa.tar.gz && \
88-
mv "opa-${PRODUCT}" opa
99+
go install github.com/CycloneDX/cyclonedx-gomod/cmd/[email protected]
100+
curl "https://repo.stackable.tech/repository/packages/opa/opa_${PRODUCT}.tar.gz" -o opa.tar.gz
101+
tar -zxvf opa.tar.gz
102+
mv "opa-${PRODUCT}" opa
103+
EOF
89104

90105
WORKDIR /opa
91106

@@ -97,7 +112,12 @@ git config user.name "Fake commiter"
97112
git commit --allow-empty --message "Fake commit, so that we can create a tag"
98113
git tag "v${PRODUCT}"
99114
go build -o opa -buildmode=exe
100-
~/go/bin/cyclonedx-gomod app -json -output-version 1.5 -output "opa_${PRODUCT}.cdx.json" -packages -files
115+
# move artifact to /stackable/*/ to copy in final image
116+
~/go/bin/cyclonedx-gomod app -json -output-version 1.5 -output /stackable/opa/"opa_${PRODUCT}.cdx.json" -packages -files
117+
# move artifact to /stackable/* to copy in final image
118+
mv /opa/opa /stackable/opa/
119+
# set correct groups
120+
chmod -R g=u /stackable/opa
101121
EOF
102122

103123
FROM stackable/image/vector
@@ -107,43 +127,46 @@ ARG RELEASE
107127
ARG STACKABLE_USER_UID
108128

109129
LABEL name="Open Policy Agent" \
110-
maintainer="[email protected]" \
111-
vendor="Stackable GmbH" \
112-
version="${PRODUCT}" \
113-
release="${RELEASE}" \
114-
summary="The Stackable image for Open Policy Agent (OPA)." \
115-
description="This image is deployed by the Stackable Operator for OPA."
130+
maintainer="[email protected]" \
131+
vendor="Stackable GmbH" \
132+
version="${PRODUCT}" \
133+
release="${RELEASE}" \
134+
summary="The Stackable image for Open Policy Agent (OPA)." \
135+
description="This image is deployed by the Stackable Operator for OPA."
116136

117-
COPY opa/licenses /licenses
137+
COPY --chown=${STACKABLE_USER_UID}:0 opa/licenses /licenses
118138

119-
COPY --from=opa-builder --chown=${STACKABLE_USER_UID}:0 /opa/opa /stackable/opa/opa
120-
COPY --from=opa-builder --chown=${STACKABLE_USER_UID}:0 /opa/opa_${PRODUCT}.cdx.json /stackable/opa/
139+
COPY --from=opa-builder --chown=${STACKABLE_USER_UID}:0 /stackable/opa /stackable/opa
121140
COPY --from=opa-bundle-builder --chown=${STACKABLE_USER_UID}:0 /opa-bundle-builder/target/release/stackable-opa-bundle-builder /stackable/opa-bundle-builder
122141
COPY --from=multilog-builder --chown=${STACKABLE_USER_UID}:0 /daemontools/admin/daemontools/command/multilog /stackable/multilog
123142

124-
COPY --chown=${STACKABLE_USER_UID}:0 opa/stackable/bin /stackable/opa/bin
125-
126143
RUN <<EOF
127144
microdnf update
128-
129145
# jq: Required for filtering logs
130146
microdnf install \
131147
jq
132148
microdnf clean all
133149
rm -rf /var/cache/yum
134150

135-
# All files and folders owned by root group to support running as arbitrary users.
136-
# This is best practice as all container users will belong to the root group (0).
137-
chown -R ${STACKABLE_USER_UID}:0 /stackable
138-
chmod -R g=u /stackable
151+
# fix missing permissions
152+
chmod g=u /stackable/opa
139153
EOF
140154

141155
# ----------------------------------------
142-
# Attention: We are changing the group of all files in /stackable directly above
143-
# If you do any file based actions (copying / creating etc.) below this comment you
144-
# absolutely need to make sure that the correct permissions are applied!
145-
# chown ${STACKABLE_USER_UID}:0
156+
# Checks
157+
# This section is to run final checks to ensure the created final images
158+
# adhere to several minimal requirements like:
159+
# - check file permissions and ownerships
160+
# ----------------------------------------
161+
162+
# Check that permissions and ownership in /stackable are set correctly
163+
# This will fail and stop the build if any mismatches are found.
164+
RUN <<EOF
165+
/bin/check-permissions-ownership.sh /stackable ${STACKABLE_USER_UID} 0
166+
EOF
167+
146168
# ----------------------------------------
169+
# Attention: Do not perform any file based actions (copying/creating etc.) below this comment because the permissions would not be checked.
147170

148171
USER ${STACKABLE_USER_UID}
149172
WORKDIR /stackable/opa

0 commit comments

Comments
 (0)