-
Notifications
You must be signed in to change notification settings - Fork 7
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
Update Dockerfile configuration #195
base: develop
Are you sure you want to change the base?
Changes from all commits
a1b9e94
bac905d
ce1247a
5088fdc
22aa084
66032ea
aa39972
6b36d69
45f104a
446c9b5
0a49b3e
8534e1d
8113726
77b5e34
35e8753
2266949
d530d07
adfcfdb
8e03ad9
c45345f
d42ae8f
1b3e9d8
2811690
5a601fe
6d487cf
0d7cc8f
b28481f
fff262b
dd7d982
e054517
d2c1ba2
ae46c28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,59 @@ | ||
ARG VERSION=unspecified | ||
# Official Docker images are in the form library/<app> while non-official | ||
# images are in the form <user>/<app>. | ||
FROM docker.io/library/python:3.12.2-alpine3.19 as compile-stage | ||
|
||
FROM python:3.12.0-alpine | ||
### | ||
# Unprivileged user variables | ||
### | ||
ARG CISA_USER="cisa" | ||
ENV CISA_HOME="/home/${CISA_USER}" | ||
ENV VIRTUAL_ENV="${CISA_HOME}/.venv" | ||
|
||
ARG VERSION | ||
# Versions of the Python packages installed directly | ||
ENV PYTHON_PIP_VERSION=24.0 | ||
ENV PYTHON_PIPENV_VERSION=2023.12.1 | ||
ENV PYTHON_SETUPTOOLS_VERSION=69.1.1 | ||
ENV PYTHON_WHEEL_VERSION=0.42.0 | ||
|
||
### | ||
# Install the specified versions of pip, setuptools, and wheel into the system | ||
# Python environment; install the specified version of pipenv into the system Python | ||
# environment; set up a Python virtual environment (venv); and install the specified | ||
# versions of pip, setuptools, and wheel into the venv. | ||
# | ||
# Note that we use the --no-cache-dir flag to avoid writing to a local | ||
# cache. This results in a smaller final image, at the cost of | ||
# slightly longer install times. | ||
### | ||
RUN python3 -m pip install --no-cache-dir --upgrade \ | ||
pip==${PYTHON_PIP_VERSION} \ | ||
setuptools==${PYTHON_SETUPTOOLS_VERSION} \ | ||
wheel==${PYTHON_WHEEL_VERSION} \ | ||
&& python3 -m pip install --no-cache-dir --upgrade \ | ||
pipenv==${PYTHON_PIPENV_VERSION} \ | ||
# Manually create the virtual environment | ||
&& python3 -m venv ${VIRTUAL_ENV} \ | ||
# Ensure the core Python packages are installed in the virtual environment | ||
&& ${VIRTUAL_ENV}/bin/python3 -m pip install --no-cache-dir --upgrade \ | ||
pip==${PYTHON_PIP_VERSION} \ | ||
setuptools==${PYTHON_SETUPTOOLS_VERSION} \ | ||
wheel==${PYTHON_WHEEL_VERSION} | ||
|
||
### | ||
# Check the Pipfile configuration and then install the Python dependencies into | ||
# the virtual environment. | ||
# | ||
# Note that pipenv will install into a virtual environment if the VIRTUAL_ENV | ||
# environment variable is set. | ||
### | ||
WORKDIR /tmp | ||
COPY src/Pipfile src/Pipfile.lock ./ | ||
RUN pipenv check --verbose \ | ||
&& pipenv install --clear --deploy --extra-pip-args "--no-cache-dir" --verbose | ||
|
||
# Official Docker images are in the form library/<app> while non-official | ||
# images are in the form <user>/<app>. | ||
FROM docker.io/library/python:3.12.2-alpine3.19 as build-stage | ||
|
||
### | ||
# For a list of pre-defined annotation keys and value types see: | ||
|
@@ -27,15 +78,7 @@ ARG CISA_GID=${CISA_UID} | |
ARG CISA_USER="cisa" | ||
ENV CISA_GROUP=${CISA_USER} | ||
ENV CISA_HOME="/home/${CISA_USER}" | ||
|
||
### | ||
# Upgrade the system | ||
# | ||
# Note that we use apk --no-cache to avoid writing to a local cache. | ||
# This results in a smaller final image, at the cost of slightly | ||
# longer install times. | ||
### | ||
RUN apk --update --no-cache --quiet upgrade | ||
ENV VIRTUAL_ENV="${CISA_HOME}/.venv" | ||
|
||
### | ||
# Create unprivileged user | ||
|
@@ -44,52 +87,25 @@ RUN addgroup --system --gid ${CISA_GID} ${CISA_GROUP} \ | |
&& adduser --system --uid ${CISA_UID} --ingroup ${CISA_GROUP} ${CISA_USER} | ||
|
||
### | ||
# Dependencies | ||
# Copy in the Python virtual environment created in compile-stage, symlink the | ||
# Python binary in the venv to the system-wide Python and add the venv to the PATH. | ||
# | ||
# Note that we use apk --no-cache to avoid writing to a local cache. | ||
# This results in a smaller final image, at the cost of slightly | ||
# longer install times. | ||
### | ||
ENV DEPS \ | ||
ca-certificates \ | ||
openssl \ | ||
py-pip | ||
RUN apk --no-cache --quiet add ${DEPS} | ||
|
||
### | ||
# Make sure pip, setuptools, and wheel are the latest versions | ||
# | ||
# Note that we use pip3 --no-cache-dir to avoid writing to a local | ||
# cache. This results in a smaller final image, at the cost of | ||
# slightly longer install times. | ||
### | ||
RUN pip3 install --no-cache-dir --upgrade \ | ||
pip \ | ||
setuptools \ | ||
wheel | ||
|
||
WORKDIR ${CISA_HOME} | ||
|
||
### | ||
# Install Python dependencies | ||
# | ||
# Note that we use pip3 --no-cache-dir to avoid writing to a local | ||
# cache. This results in a smaller final image, at the cost of | ||
# slightly longer install times. | ||
### | ||
RUN wget --output-document sourcecode.tgz \ | ||
https://github.com/cisagov/skeleton-python-library/archive/v${VERSION}.tar.gz \ | ||
&& tar --extract --gzip --file sourcecode.tgz --strip-components=1 \ | ||
&& pip3 install --no-cache-dir --requirement requirements.txt \ | ||
&& ln -snf /run/secrets/quote.txt src/example/data/secret.txt \ | ||
&& rm sourcecode.tgz | ||
# Note that we symlink the Python binary in the venv to the system-wide Python so that | ||
# any calls to `python3` will use our virtual environment. We are using short flags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this comment be moved down to after the |
||
# because the ln binary in Alpine Linux does not support long flags. The -f instructs | ||
# ln to remove the existing file and the -s instructs ln to create a symbolic link. | ||
### | ||
COPY --from=compile-stage --chown=${CISA_USER}:${CISA_GROUP} ${VIRTUAL_ENV} ${VIRTUAL_ENV} | ||
RUN ln -fs "$(command -v python3)" "${VIRTUAL_ENV}"/bin/python3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL you can |
||
ENV PATH="${VIRTUAL_ENV}/bin:$PATH" | ||
|
||
### | ||
# Prepare to run | ||
### | ||
ENV ECHO_MESSAGE="Hello World from Dockerfile" | ||
WORKDIR ${CISA_HOME} | ||
USER ${CISA_USER}:${CISA_GROUP} | ||
EXPOSE 8080/TCP | ||
VOLUME ["/var/log"] | ||
ENTRYPOINT ["example"] | ||
CMD ["--log-level", "DEBUG"] | ||
CMD ["--log-level", "DEBUG", "8", "2"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--requirement requirements-test.txt | ||
ipython | ||
pipenv | ||
semver |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
# List any Python dependencies for the image here | ||
[packages] | ||
# This should match the version of the image | ||
example = {file = "https://github.com/cisagov/skeleton-python-library/archive/v0.2.0.tar.gz"} | ||
|
||
# This version should match the version of Python in the image | ||
[requires] | ||
python_full_version = "3.12.2" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.1" | ||
__version__ = "0.2.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is the
compile-stage
of the build, do we need to optimize for layer count. i.e., does it still make sense to concatenate each command with&&
versus having discreetRUN
s.I ask because I think you will get a more localized error statement when something fails when the command is decomposed. There may be some other gains to be had as well with caching, and parallelization.