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

build: use a single Dockerfile, providing args for the Python version #339

Merged
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions docker/py310.Dockerfile → docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM python:3.10-alpine3.17
ARG PY
ARG ALPINE=3.17
FROM python:${PY}-alpine${ALPINE}

ENV PIP_ROOT_USER_ACTION=ignore

Expand All @@ -13,4 +15,7 @@ RUN apk update \
&& pip install --progress-bar=off --upgrade psutil \
&& pip install --progress-bar=off --use-pep517 -r requirements_dev.txt

CMD ["tox", "-e", "py310-{test,install}"]
# We can't do ENV manipulation to remove the . for tox, so need another var
ARG TOXPY
ENV TOXPY="${TOXPY}"
CMD ["sh", "-c", "tox -e py${TOXPY}-{test,install}"]
29 changes: 21 additions & 8 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@ that) you can just run them in docker using containers.

# Setup

To build a container say for Python 3.11 change to the root directory of the
To build a container say for Python 3.11, change to the root directory of the
project and run:

```bash
docker build -t pactfoundation:python311 -f docker/py311.Dockerfile .
(export PY=3.11 && docker build --build-arg PY="$PY" --build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" -t pactfoundation:python${PY} -f docker/Dockerfile .)
```

To then run the tests and exit, you will need:
This uses an Alpine based image (currently 3.17), which is available as of
2023-04 for Python versions 3.7 - 3.11.

Note: To run tox, the Python version without the '.' is required, i.e. '311'
instead of '3.11', so some manipulation with `sed` is used to remove the '.'

To build for Python versions which require a different Alpine image, such as if
trying to build against Python 3.6, an extra `ALPINE` arg can be provided:

```bash
(export PY=3.6 && docker build --build-arg PY="$PY" --build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" --build-arg ALPINE=3.15 -t pactfoundation:python${PY} -f docker/Dockerfile .)
```

To then run the tests and exit:

```bash
docker run -it --rm -v "$(pwd)":/home pactfoundation:python311
docker run -it --rm -v "$(pwd)":/home pactfoundation:python3.11
```

If you need to debug you can change the command to:

```bash
docker run -it --rm -v "$(pwd)":/home pactfoundation:python311 sh
docker run -it --rm -v "$(pwd)":/home pactfoundation:python3.11 sh
```

This will open a container with a prompt. From the `/home` location in the
Expand All @@ -34,11 +47,11 @@ tox -e py311-{test,install}
```

In all the above if you need to run a different version change
`py311`/`python311` where appropriate. Or you can run the convenience script
`py311`/`python3.11` where appropriate. Or you can run the convenience script
to build:

```bash
docker/build.sh 311
docker/build.sh 3.11
```

where `311` is the python environment version (3.11 in this case).
where `3.11` is the python environment version.
29 changes: 21 additions & 8 deletions docker/build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#!/bin/sh

set -eo pipefail
set -euo pipefail

if [ $# -ne 1 ]; then
echo "$0: usage: build.sh 37|38|39|310|311"
if [ $# -lt 1 ]; then
echo "Usage: $0 PYTHON_VERSION [ALPINE_VERSION]"
echo
echo "Example:"
echo "$0 3.11 Build using Python 3.11, default Alpine"
echo "$0 3.6 3.16 Build using Python 3.6, Alpine 3.15"
exit 1
fi
DOCKER_ENV=$1
echo "Building env ${DOCKER_ENV}"

DOCKER_IMAGE="pactfoundation:python${DOCKER_ENV}"
DOCKER_FILE="docker/py${DOCKER_ENV}.Dockerfile"
PY=$1
ALPINE=${2:-3.17}
echo "Building env for Python: ${PY}, Alpine: ${ALPINE}"

docker build -t "$DOCKER_IMAGE" -f "$DOCKER_FILE" .
DOCKER_IMAGE="pactfoundation:python${PY}"
DOCKER_FILE="docker/Dockerfile"

docker build \
--build-arg PY="$PY" \
--build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" \
--build-arg ALPINE="${ALPINE}" \
-t "$DOCKER_IMAGE" -f "$DOCKER_FILE" .

echo
echo "Image successfully built and tagged as: ${DOCKER_IMAGE}"
16 changes: 0 additions & 16 deletions docker/py311.Dockerfile

This file was deleted.

16 changes: 0 additions & 16 deletions docker/py37.Dockerfile

This file was deleted.

16 changes: 0 additions & 16 deletions docker/py38.Dockerfile

This file was deleted.

16 changes: 0 additions & 16 deletions docker/py39.Dockerfile

This file was deleted.