Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multistage build for gateway #1506

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
FROM registry.access.redhat.com/ubi9-minimal:9.4@sha256:104cf11d890aeb7dd5728b7d7732e175a0e4018f1bb00d2faebcc8f6bf29bd52
RUN microdnf install -y python3.11-3.11.7 python3.11-pip-22.3.1 python3.11-devel-3.11.7 vim-enhanced-8.2.2637 &&\
microdnf clean all
RUN ln -s /usr/bin/python3.11 /usr/local/bin/python3 && \
ln -s /usr/bin/python3.11 /usr/local/bin/python &&\
ln -s /usr/bin/pip3.11 /usr/local/bin/pip3 &&\
ln -s /usr/bin/pip3.11 /usr/local/bin/pip
ARG MICRO_IMAGE_DIR=/ubi-micro-img

# BASE image using UBI 9 micro where the
# application and requirements will be installed
FROM registry.access.redhat.com/ubi9-micro:9.4-15 AS BASE

# BUILD image using UBI 9 where the dependencies that
# require installing with a package manager will be installed
FROM registry.access.redhat.com/ubi9:9.4-1214.1726694543 AS BUILD
ARG MICRO_IMAGE_DIR

# Copy the BASE image into the BUILD image
RUN mkdir ${MICRO_IMAGE_DIR}
COPY --from=BASE / ${MICRO_IMAGE_DIR}

# Install Python inside the BASE image
# hadolint ignore=DL3041
RUN dnf install --installroot ${MICRO_IMAGE_DIR} --nodocs -y \
python3.11-3.11.7 \
python3.11-devel-3.11.7 \
libstdc++ &&\
dnf upgrade --installroot ${MICRO_IMAGE_DIR} --nodocs -y && \
dnf clean all --installroot ${MICRO_IMAGE_DIR}

# APP image from `scratch` which will be the final image
# and remaining application requirements will be installed
FROM scratch AS APP
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this APP image is necessary. What is the difference between copying back the ${MICRO_IMAGE_DIR}/ into the BASE image and the APP image?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BASE image still has things like a package manager and a shell that the gateway really doesn't need, whereas scratch / APP is empty except for what we explicitly add to it. So that does two things: (a) it reduces the number of components that we have to manage for CVEs, and (b) it reduces the threat vectors for that particular container.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong but I read

COPY --from=BASE / ${MICRO_IMAGE_DIR}

copies everything in the BASE to ${MICRO_IMAGE_DIR} directory and

COPY --from=BUILD ${MICRO_IMAGE_DIR}/ .

copies everything in the BASE + additional pieces to the APP
So everything in the BASE is copied to APP. Where am I misunderstanding?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be right... I'm just copying from what another team already did and didn't look too closely at what they were doing, I just assumed it worked since it had already gone through review.

ARG MICRO_IMAGE_DIR
# hadolint ignore=DL3045
COPY --from=BUILD ${MICRO_IMAGE_DIR}/ .

# create symlinks for python
RUN ln -s /usr/bin/python3.11 /usr/bin/python

# Create project dir
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
Tansito marked this conversation as resolved.
Show resolved Hide resolved
ENV PYTHONUNBUFFERED 1

USER 0
COPY gateway/requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir &&\
# Install pip
RUN python3.11 -m ensurepip --upgrade
# Install dependencies and update then uninstall pip (not needed in final image)
RUN python3.11 -m pip install -r requirements.txt --no-cache-dir --upgrade && \
cp -r -n /usr/local/lib64/python3.11/site-packages/symengine /usr/local/lib/python3.11/site-packages &&\
cp -r -n /usr/local/lib/python3.11/site-packages/symengine /usr/local/lib64/python3.11/site-packages
cp -r -n /usr/local/lib/python3.11/site-packages/symengine /usr/local/lib64/python3.11/site-packages &&\
python3.11 -m pip uninstall -y pip

COPY gateway .
RUN chown -R 1000:100 /usr/src/app &&\
mkdir /usr/src/app/media && chown 1000:100 /usr/src/app/media

# Need versions of pip/setuptools more recent than provided by UBI image
RUN python3.11 -m ensurepip --upgrade

# hadolint ignore=DL3013
RUN pip install --upgrade --no-cache-dir pip>=24.2 &&\
pip install --upgrade --no-cache-dir setuptools>=72.1.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the setuptools unnecessary after all or is the right version in ubi9-micro:9.4-15?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was assuming that the scratch image doesn't have setuptools so it shouldn't need to be updated. but maybe it will be?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Scan will tell us :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all hail general scan 😆


USER 1000:100
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh &&\
chmod +x /usr/src/app/entrypoint.sh

EXPOSE 8000
USER 1000:100
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]