Skip to content

Commit 71a7397

Browse files
authored
Merge pull request pact-foundation#339 from mikegeeves/single-dockerfile-with-args
build: use a single Dockerfile, providing args for the Python version
2 parents e99e7fb + 429e171 commit 71a7397

7 files changed

Lines changed: 49 additions & 82 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
FROM python:3.10-alpine3.17
1+
ARG PY
2+
ARG ALPINE=3.17
3+
FROM python:${PY}-alpine${ALPINE}
24

35
ENV PIP_ROOT_USER_ACTION=ignore
46

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

16-
CMD ["tox", "-e", "py310-{test,install}"]
18+
# We can't do ENV manipulation to remove the . for tox, so need another var
19+
ARG TOXPY
20+
ENV TOXPY="${TOXPY}"
21+
CMD ["sh", "-c", "tox -e py${TOXPY}-{test,install}"]

docker/README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,36 @@ that) you can just run them in docker using containers.
77

88
# Setup
99

10-
To build a container say for Python 3.11 change to the root directory of the
10+
To build a container say for Python 3.11, change to the root directory of the
1111
project and run:
1212

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

17-
To then run the tests and exit, you will need:
17+
This uses an Alpine based image (currently 3.17), which is available as of
18+
2023-04 for Python versions 3.7 - 3.11.
19+
20+
Note: To run tox, the Python version without the '.' is required, i.e. '311'
21+
instead of '3.11', so some manipulation with `sed` is used to remove the '.'
22+
23+
To build for Python versions which require a different Alpine image, such as if
24+
trying to build against Python 3.6, an extra `ALPINE` arg can be provided:
25+
26+
```bash
27+
(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 .)
28+
```
29+
30+
To then run the tests and exit:
1831

1932
```bash
20-
docker run -it --rm -v "$(pwd)":/home pactfoundation:python311
33+
docker run -it --rm -v "$(pwd)":/home pactfoundation:python3.11
2134
```
2235

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

2538
```bash
26-
docker run -it --rm -v "$(pwd)":/home pactfoundation:python311 sh
39+
docker run -it --rm -v "$(pwd)":/home pactfoundation:python3.11 sh
2740
```
2841

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

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

4053
```bash
41-
docker/build.sh 311
54+
docker/build.sh 3.11
4255
```
4356

44-
where `311` is the python environment version (3.11 in this case).
57+
where `3.11` is the python environment version.

docker/build.sh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
#!/bin/sh
22

3-
set -eo pipefail
3+
set -euo pipefail
44

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

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

15-
docker build -t "$DOCKER_IMAGE" -f "$DOCKER_FILE" .
18+
DOCKER_IMAGE="pactfoundation:python${PY}"
19+
DOCKER_FILE="docker/Dockerfile"
20+
21+
docker build \
22+
--build-arg PY="$PY" \
23+
--build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" \
24+
--build-arg ALPINE="${ALPINE}" \
25+
-t "$DOCKER_IMAGE" -f "$DOCKER_FILE" .
26+
27+
echo
28+
echo "Image successfully built and tagged as: ${DOCKER_IMAGE}"

docker/py311.Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

docker/py37.Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

docker/py38.Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

docker/py39.Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)