-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Add automatic building of Docker containers
- Loading branch information
Showing
2 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Building Docker containers | ||
========================== | ||
|
||
We use :doc:`ci-cd`, to create our Python Docker containers. | ||
|
||
#. First, we define the Python version in our :ref:`pyproject-toml` file of the | ||
project: | ||
|
||
.. code-block:: toml | ||
[project] | ||
name = "my-app" | ||
requires-python = "==3.12.*" | ||
#. We then extract this string in our :file:`.gitlab-ci.yml` file and pass it as | ||
a build argument to ``docker build``: | ||
|
||
.. code-block:: yaml | ||
build: | ||
stage: build | ||
only: [main] | ||
script: | ||
- export PY=$(sed -nE 's/^requires-python = "==(3\.[0-9]+)\.*"$/python\1/p' pyproject.toml) | ||
- > | ||
docker build | ||
--build-arg PY=$PY | ||
#. Finally, we can use the extracted version in the Dockerfile to create a | ||
virtual environment in the build phase and install the Python version in the | ||
``application`` stage: | ||
|
||
.. code-block:: docker | ||
:emphasize-lines: 3, 6 | ||
FROM your-docker/build-image as build | ||
ARG PY | ||
RUN --mount=type=cache,target=/root/.cache \ | ||
set -ex \ | ||
&& virtualenv --python $PY /app | ||
… | ||
FROM your-docker/app-image | ||
… | ||
RUN set -ex \ | ||
&& apt-get update -qy \ | ||
&& apt-get install -qy \ | ||
-o APT::Install-Recommends=false \ | ||
-o APT::Install-Suggests=false \ | ||
… | ||
COPY --from=build --chown=app /app /app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters