This is a simple CLI that generates a Dockerfile for a Django Projects.
With dockerfile-django
installed, you can generate a Dockerfile for your Django project. The command will search for the Django project files in the specified directory and generate a Dockerfile for your Django application.
Go to the root directory of your Django project and run the following command:
dockerfile-django generate
By default, the command will search for Django project files in the current directory.
--dir
: The directory path to search for Django project files. By default, it is set to the current directory.--force
: Force overwriting the existing Dockerfile. By default, it is set toFalse
.--diff
: Display differences between the current and generated Dockerfile. By default, it is set toFalse
.--pyver
: Python version to use in the Dockerfile. By default, it is set toDEFAULT_PYTHON_VERSION
(3.12).--help
: Show the help message and exit.
dockerfile-django generate --dir /.../Projects/my-djanogo-project/ --diff --pyver "3.11.4"
This command will search for Django project files in the /.../Projects/my-djanogo-project/
directory (and all subdirectories) and generate a Dockerfile with Python version 3.11.4
. It will also display the differences between the current and generated Dockerfile but not force save the generated Dockerfile.
You can save default options for the dockerfile-django
command by creating/updating a pyproject.toml
file in your project directory.
[tool]
table is used to store the default options for the dockerfile-django
command.
# pyproject.toml
[tool.dockerfile_django]
dir = "...Projects/my-djanogo-project/"
force = false
diff = true
pyver = "3.11.4"
dockerfile-django
will use the default options from the pyproject.toml
file if no options are provided.
You can also override the default options by providing the options in the command line.
Before you can start this project, you'll need to install Poetry, a tool for dependency management and packaging in Python. For full installation instructions, please refer to the Poetry documentation.
- Clone the repository
- Navigate to the project directory (root)
- Run
poetry install
to create the virtual environment and install the dependencies - Run
poetry shell
to activate the virtual environment - Once you are done, you can deactivate the virtual environment by running
exit
or closing the terminal.
git clone https://github.com/fly-apps/dockerfile-django.git
cd dockerfile-django
poetry install
poetry shell
To get help and see the available options for the dockerfile-django
command, you can use the following command:
poetry run dockerfile-django --help
Usage: dockerfile-django [OPTIONS] COMMAND [ARGS]...
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --install-completion Install completion for the current shell. │
│ --show-completion Show completion for the current shell, to copy it or customize the installation. │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ generate Generate a Dockerfile for Django projects. │
│ welcome Display a welcome message. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
poetry run dockerfile-django generate --help
Usage: dockerfile-django generate [OPTIONS]
Generate a Dockerfile for Django projects.
╭─ Options ─────────────────────────────────────────────────────────────────────────────╮
│ --dir TEXT The directory to search for Django project files [default: .] │
│ --force Force overwriting the existing Dockerfile │
│ --diff Display differences between current and generated Dockerfile │
│ --pyver TEXT Python version to use in the Dockerfile [default: None] │
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────╯
Ruff is a fast and configurable Python linter and formatter written in Rust.
Ruff is already installed in the project's virtual environment (dev).
Once installed, you can run ruff check
to lint the files, which mean it analyses the code to find and report issues related to style, potential errors, and best practices:
poetry run ruff check .
and use --fix
option to fix any fixable errors:
poetry run ruff check . --fix
Additionally, you can automatically format your code to comply with the style guidelines enforced by running:
poetry run ruff format .
Specific linter and format configurations are described on
pyproject.toml
on tables[tool.ruff.lint]
and[tool.ruff.format]
, respectively.
To generate a Dockerfile for a test case, you can use the following command:
poetry run dockerfile-django generate --dir tests/test_cases/server/gunicorn/ --pyver "3.12"
To run the tests, you can follow the steps below:
- Navigate to the project directory (root)
- Run the tests directly in the virtual environment
cd dockerfile-django/
poetry run pytest
To produce a coverage report, run:
poetry run pytest --cov=dockerfile_django
- Navigate to the project directory (root)
- Activate the virtual environment
- Run the tests
cd dockerfile-django/
poetry shell
pytest
To assist with this process, outputs of tests can be captured automatically. This is useful when adding new tests and when making a change that affects many tests. Be sure to inspect the output (e.g., by using git diff) before committing.
Capturing test results is done by setting the following environment variable before running pytest
:
TEST_CAPTURE=1