Jinja2 Templates Not Rendering for Cookiecutter Using Docker

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.

Setup

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
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 }}"

Expectations

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.

Workflow

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

Results

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật