Skip to content

Commit b3cf53c

Browse files
committed
Docker improvements and fixes
1 parent 4309d91 commit b3cf53c

File tree

9 files changed

+111
-33
lines changed

9 files changed

+111
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Don't forget to give the project a star! Thanks again!
134134
<!-- LICENSE -->
135135
## License
136136

137-
Distributed under the MIT License. See [`LICENSE.txt`](https://github.com/nullhack/cookiecutter-python-base-project/blob/main/LICENSE.txt) for more information.
137+
Distributed under the MIT License. See [`LICENSE`](https://github.com/nullhack/cookiecutter-python-base-project/blob/main/LICENSE) for more information.
138138

139139
<p align="right">(<a href="#top">back to top</a>)</p>
140140

{{cookiecutter.project_slug}}/.flake8

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[flake8]
2-
ignore = E203, E266, E501, W503, T499, ANN101, ANN102, ANN002, ANN003, S101, SCS108
2+
#ignore = E203, E266, E501, W503, T499, ANN101, ANN102, ANN002, ANN003, S101, SCS108
3+
ignore = E501
34
max-line-length = 79
45
enable-extensions=G
56
#select = B,C,E,F,W,T4,T0,N8,DC,DAR,SCS
67
import-order-style = google
78
docstring-convention = google
8-
#per-file-ignores = tests/*:DC,SCS,ANN,S101 noxfile.py:ANN,DAR101
9+
per-file-ignores = tests/*:S101, SCS108
910
max-complexity = 10
1011
exclude =
1112
docs/
Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
1-
FROM python:3.8.3-slim-buster
1+
ARG BUILDPLATFORM=linux/amd64
2+
ARG BUILDTAG=3-alpine
23

3-
RUN adduser --system user
4+
FROM --platform=$BUILDPLATFORM python:$BUILDTAG as base_build
45

56
WORKDIR /home/user/app
67

7-
RUN apt-get update && \
8-
apt-get install -y git && \
9-
chown user /home/user/app && \
10-
rm -rf /var/lib/apt/lists/*
11-
12-
USER user
13-
14-
ENV PATH /home/user/.local/bin:${PATH}
15-
168
COPY {{cookiecutter.package_name}} {{cookiecutter.package_name}}
179
COPY tests tests
1810
COPY pyproject.toml .
1911
COPY poetry.lock .
12+
COPY README.md .
2013
COPY .pre-commit-config.yaml .
2114
COPY .gitignore .
2215
COPY .flake8 .
2316

24-
RUN pip install poetry poethepoet --user && \
25-
poe install
17+
ENV PATH=$PATH:/home/user/.local/bin
18+
19+
FROM base_build as test
20+
21+
ARG TESTBUILD=True
22+
ENV TESTBUILD=$TESTBUILD
23+
24+
RUN pip install --no-cache poetry poethepoet
25+
RUN poetry config --no-cache
26+
RUN if [ "$TESTBUILD" = 'True' ]; then poe install-dev; fi
27+
RUN poetry build --format=wheel
28+
RUN poetry export --only main -f requirements.txt --without-hashes --output requirements.txt
29+
RUN if [ "$TESTBUILD" = 'True' ]; then poe test; fi
2630

2731
ENTRYPOINT ["poe", "-q"]
28-
CMD ["run"]
32+
CMD ["test"]
33+
34+
FROM --platform=$BUILDPLATFORM python:$BUILDTAG as prod
35+
36+
RUN addgroup --system user && adduser --system user --ingroup user
37+
USER user
38+
39+
WORKDIR /home/user/app
40+
41+
COPY --chown=user:user --from=test /home/user/app/requirements.txt requirements.txt
42+
COPY --chown=user:user --from=test /home/user/app/dist dist
43+
44+
RUN pip install --no-cache -r requirements.txt dist/*.whl --user
45+
46+
47+
ENTRYPOINT ["python", "-m"]
48+
CMD ["{{cookiecutter.package_name}}.{{cookiecutter.module_name}}"]
49+

{{cookiecutter.project_slug}}/poetry.lock

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

{{cookiecutter.project_slug}}/pyproject.toml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ name = "{{cookiecutter.package_name}}"
33
version = "{{cookiecutter.version}}"
44
description = "{{cookiecutter.project_short_description}}"
55
authors = ["{{cookiecutter.full_name}} <{{cookiecutter.email}}>"]
6+
maintainers = ["{{cookiecutter.full_name}} <{{cookiecutter.email}}>"]
7+
license = "{{cookiecutter.license}}"
8+
repository = "https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}"
9+
documentation = "https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/tree/main/docs/api/"
10+
readme = "README.md"
11+
packages = [
12+
{ include = "{{cookiecutter.package_name}}" },
13+
]
614

715
[tool.poetry.dependencies]
816
python = "^3.8"
917

1018
[tool.poetry.group.dev.dependencies]
1119
pre-commit = "^2.18.1"
12-
mypy = "^0.942"
1320
isort = "^5.10.1"
1421
black = {version = "^22.3.0", allow-prereleases = true}
1522
xdoctest = "^1.0.0"
@@ -19,8 +26,9 @@ pdoc3 = "^0.10.0"
1926
cookiecutter = "^1.7.3"
2027
pylint = "^2.13.4"
2128
poethepoet = "^0.16.4"
22-
flake8 = "^4.0.1"
2329
pep8-naming = "^0.12.1"
30+
mypy = "^0.942"
31+
flake8 = "^4.0.1"
2432
flake8-docstrings = "^1.6.0"
2533
flake8-todos = "^0.1.5"
2634
flake8-secure-coding-standard = "^1.2.1"
@@ -31,6 +39,8 @@ flake8-bugbear = "^22.3.23"
3139
flake8-bandit = "^3.0.0"
3240
flake8-annotations = "^2.8.0"
3341
flake8-black = "^0.3.2"
42+
flake8-mypy = "^17.8.0"
43+
flake8-simplify = "^0.19.3"
3444
pytest = "7.1.1"
3545
pytest-html = "^3.1.1"
3646
pytest-sugar = "^0.9.6"
@@ -76,7 +86,7 @@ exclude = '''
7686

7787
[tool.pytest.ini_options]
7888
minversion = "6.0"
79-
addopts = "--verbose --flake8 --reverse --color=yes --cov={{cookiecutter.package_name}} --html=docs/pytest_report.html --self-contained-html --cov-fail-under=100 --cov-report term-missing --cov-report html:docs/cov-report --doctest-modules --cov-config=pyproject.toml"
89+
addopts = "--verbose --maxfail=1 --flake8 --reverse --color=yes --cov={{cookiecutter.package_name}} --html=docs/pytest_report.html --self-contained-html --cov-fail-under=100 --cov-report term-missing --cov-report html:docs/cov-report --doctest-modules --cov-config=pyproject.toml"
8090
testpaths = [
8191
"tests",
8292
"{{cookiecutter.package_name}}"
@@ -118,4 +128,14 @@ test = "poetry run pytest"
118128
pre-commit = "poetry run pre-commit run --all-files"
119129
lint = "poetry run flake8"
120130
doc = "poetry run pdoc --html . --force --output-dir docs/api"
121-
build = "docker build -t {{cookiecutter.package_name}}:latest ."
131+
132+
[tool.poe.tasks.docker-build]
133+
cmd = "docker build --build-arg TESTBUILD=$no_test --build-arg BUILDTAG=$build_tag --build-arg BUILDPLATFORM=$build_platform --target $target_build -t {{cookiecutter.package_name}}:$target_build-$build_tag ."
134+
help = "Build a docker image to test the project in an isolated environment"
135+
args = [
136+
{ name = "no-test", default = true, type = "boolean" },
137+
{ name = "build-tag", default = "3-alpine" },
138+
{ name = "build-platform", default = "linux/amd64" },
139+
{ name = "target-build", default = "prod" },
140+
]
141+

{{cookiecutter.project_slug}}/tests/__init__.py

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

{{cookiecutter.project_slug}}/tests/{{cookiecutter.package_name}}_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
3-
41
"""This file contains examples of how to write tests using pytest!
52
63
Some good practices for writting great Python tests:
@@ -32,7 +29,7 @@ class TestGroup:
3229
"""A class with common parameters, `param1` and `param2`."""
3330

3431
@pytest.fixture()
35-
def fixt(self: Self) -> None:
32+
def fixt(self: Self) -> int:
3633
"""This fixture will only be available within the scope of TestGroup.
3734
3835
Returns:
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""{{cookiecutter.project_short_description}}."""

{{cookiecutter.project_slug}}/{{cookiecutter.package_name}}/{{cookiecutter.module_name}}.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""Module Docstring."""
42

53
import logging
@@ -11,7 +9,7 @@
119
logger.info("This is a {word}", extra={"word": "Log"})
1210

1311

14-
class Calculator(object):
12+
class Calculator:
1513
"""Class for simple calculator operations."""
1614

1715
@staticmethod

0 commit comments

Comments
 (0)