I am trying to generate a project template using cookiecutter
through a Docker container and the jinja2
templates I provide in the values of the cookiecutter.json
file don’t appear to render properly.
The goal of this is to allow anyone to run a simple Docker command to generate a template without having to first install cookiecutter
on their machine.
Here is my workflow and output.
I have the following project structure
mldev_template/
├── Dockerfile.cookiecutter
├── entrypoint.sh
├── cookiecutter.json
└── {{cookiecutter._project_name_snake_case}}/
├── Dockerfile
The files contain the following:
Dockerfile.cookiecutter
FROM python:3.10.8
RUN pip install cookiecutter==2.6.0
WORKDIR /workspace/mldev_template
COPY entrypoint.sh /workspace/mldev_template/entrypoint.sh
RUN chmod +x /workspace/mldev_template/entrypoint.sh
ENTRYPOINT ["/workspace/mldev_template/entrypoint.sh"]
entrypoint.sh
#!/bin/bash
cookiecutter -v /workspace/mldev_template
cookiecutter.json
{
"project_name": "My Awesome Project",
"_project_name_snake_case": "{{ cookiecutter.project_name | lower | replace(' ', '_') }}"
}
I have also tried cookiecutter.project_name.lower().replace(' ', '_')
as well as jinja2-strcase
functions in place of this jinja2
template with the same results.
{{cookiecutter._project_name_snake_case}}/Dockerfile
FROM python:3.10-slim
LABEL project="{{ cookiecutter._project_name_snake_case }}"
I am expecting the jinja2
templates to render as the directory name of the newly generated directory and in the project
LABEL of the Dockerfile inside the newly generated directory, both as snake case.
In my example below I input Test Project
at the prompt, so I would expect to see test_project
as the directory name and the project
LABEL in the newly generated Dockerfile
.
I ran the following commands to build the Docker image on my local machine, and then ran a container interactively. These commands were run from inside the top-level directory.
docker build --no-cache -f Dockerfile.cookiecutter -t cookiecutter-runner .
docker run -it --rm -v $(pwd)/..:/workspace --entrypoint /bin/bash cookiecutter-runner
Once inside the container I run cookiecutter -v /workspace/mldev_template
, follow the prompt, and get the following output.
root@345df12673ac:/workspace/mldev_template# cookiecutter -v /workspace/mldev_template
DEBUG cookiecutter.config: User config not found. Loading default config.
DEBUG cookiecutter.hooks: hooks_dir is /workspace/mldev_template/hooks
DEBUG cookiecutter.hooks: No hooks/dir in template_dir
DEBUG cookiecutter.main: context_file is /workspace/mldev_template/cookiecutter.json
DEBUG cookiecutter.generate: Context generated is OrderedDict([('cookiecutter', OrderedDict([('project_name', 'My Awesome Project'), ('_project_name_snake_case', "{{ cookiecutter.project_name | lower | replace(' ', '_') }}")]))])
[1/1] project_name (My Awesome Project): Test Project
DEBUG cookiecutter.main: context is OrderedDict([('cookiecutter', OrderedDict([('project_name', 'Test Project'), ('_project_name_snake_case', "{{ cookiecutter.project_name | lower | replace(' ', '_') }}")])), ('_cookiecutter', {'project_name': 'My Awesome Project'})])
DEBUG cookiecutter.utils: Making sure path exists (creates tree if not exist): /root/.cookiecutter_replay/
DEBUG cookiecutter.find: Searching /workspace/mldev_template for the project template.
DEBUG cookiecutter.find: The project template appears to be /workspace/mldev_template/{{ cookiecutter._project_name_snake_case }}
DEBUG cookiecutter.generate: Generating project from /workspace/mldev_template/{{ cookiecutter._project_name_snake_case }}...
DEBUG cookiecutter.generate: Rendered dir {{ cookiecutter.project_name | lower | replace(' ', '_') }} must exist in output_dir .
DEBUG cookiecutter.utils: Making sure path exists (creates tree if not exist): {{ cookiecutter.project_name | lower | replace(' ', '_') }}
DEBUG cookiecutter.generate: Project directory is /workspace/mldev_template/{{ cookiecutter.project_name | lower | replace(' ', '_') }}
DEBUG cookiecutter.hooks: hooks_dir is /workspace/mldev_template/hooks
DEBUG cookiecutter.hooks: No hooks/dir in template_dir
DEBUG cookiecutter.hooks: No pre_gen_project hook found
DEBUG cookiecutter.generate: Processing file Dockerfile
DEBUG cookiecutter.generate: Created file at /workspace/mldev_template/{{ cookiecutter.project_name | lower | replace(' ', '_') }}/Dockerfile
DEBUG cookiecutter.generate: Check Dockerfile to see if it's a binary
DEBUG cookiecutter.generate: Using detected newline character '\n'
DEBUG cookiecutter.generate: Writing contents to file /workspace/mldev_template/{{ cookiecutter.project_name | lower | replace(' ', '_') }}/Dockerfile
DEBUG cookiecutter.hooks: hooks_dir is /workspace/mldev_template/hooks
DEBUG cookiecutter.hooks: No hooks/dir in template_dir
DEBUG cookiecutter.hooks: No post_gen_project hook found
A new directory is created but the new directory is named {{ cookiecutter.project_name | lower | replace(' ', '_') }}
instead of test_project
and the Dockerfile beneath it looks like this
FROM python:3.10-slim
LABEL project="{{ cookiecutter.project_name | lower | replace(' ', '_') }}"
Is there something else I need to do to make these jinja2
templates render properly using cookiecutter
? I can’t seem to find any information about this anywhere. This is my first time using cookiecutter
and jinja2
in general so I’m probably missing something super simple, but I can’t seem to find an example that generates cookiecutter
using Docker and I can’t seem to find an example where someone else’s jinja2
templates don’t render properly.
I also verified that when I build my Docker image all requirements to run cookiecutter
are properly installed.
Package Version Latest Version
MarkupSafe 2.1.5 2.1.5
PyYAML 6.0.1 6.0.1
arrow 1.3.0 1.3.0
binaryornot 0.4.4 0.4.4
certifi 2024.6.2 2024.6.2
chardet 5.2.0 5.2.0
charset-normalizer 3.3.2 3.3.2
click 8.1.7 8.1.7
cookiecutter 2.6.0 2.6.0
idna 3.7 3.7
jinja2 3.1.4 3.1.4
jinja2-strcase 0.0.2 0.0.2
markdown-it-py 3.0.0 3.0.0
mdurl 0.1.2 0.1.2
pip 23.0.1 24.0
pygments 2.18.0 2.18.0
python-dateutil 2.9.0.post0 2.9.0.post0
python-slugify 8.0.4 8.0.4
requests 2.32.3 2.32.3
rich 13.7.1 13.7.1
setuptools 58.1.0 70.0.0
six 1.16.0 1.16.0
text-unidecode 1.3 1.3
types-python-dateutil 2.9.0.20240316 2.9.0.20240316
urllib3 2.2.2 2.2.2
wheel 0.43.0 0.43.0
The following worked for me using jinja2_strcase
library (https://pypi.org/project/jinja2-strcase/)
Add "_extensions": ["jinja2_strcase.StrcaseExtension"]
to cookiecutter.json
Add jinja2-strcase==0.0.2
to Dockerfile install
Use {{cookiecutter.project_name | to_snake }}
, {{cookiecutter.project_name | to_camel }}
, etc. throughout template
Make entrypoint.sh
executable outside of Dockerfile