Skip to content

Commit

Permalink
added docker (#41)
Browse files Browse the repository at this point in the history
* added the option to include a simple Dockerfile
  • Loading branch information
Florian Maas authored Jun 29, 2022
1 parent d6eec90 commit cf35530
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 6 deletions.
3 changes: 0 additions & 3 deletions .python-version

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Build status](https://img.shields.io/github/workflow/status/fpgmaas/cookiecutter-poetry/merge-to-main)](https://img.shields.io/github/workflow/status/fpgmaas/cookiecutter-poetry/merge-to-main)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/cookiecutter-poetry)](https://pypi.org/project/cookiecutter-poetry/)
[![Docs](https://img.shields.io/badge/docs-gh--pages-blue)](https://fpgmaas.github.io/cookiecutter-poetry/)
[![Commit activity](https://img.shields.io/github/commit-activity/m/fpgmaas/cookiecutter-poetry)](https://img.shields.io/github/commit-activity/m/fpgmaas/cookiecutter-poetry)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/cookiecutter-poetry)](https://img.shields.io/pypi/dm/cookiecutter-poetry?style=flat-square)
[![License](https://img.shields.io/github/license/fpgmaas/cookiecutter-poetry)](https://img.shields.io/github/license/fpgmaas/cookiecutter-poetry)


Expand All @@ -27,6 +27,7 @@ repository to generate the file structure for a Python project that uses
- Documentation with [MkDocs](https://www.mkdocs.org/)
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
- Compatibility testing for multiple versions of Python with [Tox](https://tox.wiki/en/latest/)
- Containerization with [Docker](https://www.docker.com/)

## Example CI/CD Pipeline

Expand Down
1 change: 1 addition & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"mkdocs": ["y", "n"],
"tox": ["y","n"],
"mypy" : ["y","n"],
"dockerfile" : ["y","n"],
"open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"]
}
23 changes: 23 additions & 0 deletions docs/features/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Containerization with Docker

If `dockerfile` is set to `"y"`, a simple `Dockerfile` is added to the
repository. The Dockerfile installs poetry, sets up the environment and runs
`foo.py` when run.

The docker image can be built with

```bash
docker build . -t my-docker-image
```

It can then be run in the background with

```bash
docker run -d my-docker-image
```

Or, run it interactive mode with

```
docker run --rm -it --entrypoint bash my-docker-image
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A project generated with ``cookiecutter-poetry`` supports the following features
- Documentation with [MkDocs](https://www.mkdocs.org/)
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
- Compatibility testing for multiple versions of Python with [Tox](https://tox.wiki/en/latest/)
- Containerization with [Docker](https://www.docker.com/)

An example of a repository generated with this package can be found [here](https://github.com/fpgmaas/cookiecutter-poetry-example).

Expand Down
8 changes: 6 additions & 2 deletions docs/prompt_arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ will be deployed to the `gh-pages` branch.

**tox**

`"y"` or `"n"` Adds automatic [Tox](https://tox.wiki/) testing for
`"y"` or `"n"`. Adds automatic [Tox](https://tox.wiki/) testing for
compatibility with multiple versions of Python.

**mypy**

`"y"` or `"n"` Adds automatic static type checking with [mypy](https://mypy.readthedocs.io/en/stable/).
`"y"` or `"n"`. Adds automatic static type checking with [mypy](https://mypy.readthedocs.io/en/stable/).

**dockerfile**

`"y"` or `"n"`. Adds a simple [Dockerfile](https://docker.com).

**open_source_license**

Expand Down
3 changes: 3 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ def remove_dir(filepath: str) -> None:

if "{{cookiecutter.mkdocs}}" != "y":
remove_dir("docs")

if "{{cookiecutter.dockerfile}}" != "y":
remove_file("Dockerfile")
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nav:
- Documentation with MkDocs: features/mkdocs.md
- Compatibility testing with Tox: features/tox.md
- Static type checking with mypy: features/mypy.md
- Containerization with Docker: features/docker.md
- Tutorial: tutorial.md
- Prompt Arguments: prompt_arguments.md
plugins:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ def test_not_tox(cookies, tmp_path):
f"{result.project_path}/.github/workflows/on-release-main.yml", "poetry add tox-gh-actions"
)
assert not file_contains_text(f"{result.project_path}/tox.ini", "[tox]")


def test_dockerfile(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"dockerfile": "y"})
assert os.path.isfile(f"{result.project_path}/Dockerfile")


def test_not_dockerfile(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"dockerfile": "n"})
assert not os.path.isfile(f"{result.project_path}/Dockerfile")
20 changes: 20 additions & 0 deletions {{cookiecutter.project_name}}/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# syntax=docker/dockerfile:1

FROM python:3.9-slim-buster

ENV POETRY_VERSION=1.1.13

# Install poetry
RUN pip install "poetry==$POETRY_VERSION"

# Copy only requirements to cache them in docker layer
WORKDIR /code
COPY poetry.lock pyproject.toml /code/

# Project initialization:
RUN poetry config virtualenvs.create false && \poetry install --no-interaction --no-ansi --no-root --no-dev

# Copy Python code to the Docker image
COPY {{cookiecutter.project_slug}} /code/{{cookiecutter.project_slug}}/

CMD [ "python", "{{cookiecutter.project_slug}}/foo.py"]
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ def foo() -> str:
"""

return "foo"


if __name__ == "__main__":
print(foo())

0 comments on commit cf35530

Please sign in to comment.