cookiecutter / cookiecutter-django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
https://cookiecutter-django.readthedocs.io
BSD 3-Clause "New" or "Revised" License
11.83k stars 2.84k forks source link

Use ARG and ENV in Dockerfiles and get rid of production and local compose folder structure #2823

Open arnav13081994 opened 3 years ago

arnav13081994 commented 3 years ago

Description

What are you proposing? How should it be implemented?

I'm proposing to use ENV and ARG to make Dockerfiles dynamic and just have 1 compose file at project root for both local and production. One can pass in an env variable to docker-compose that can then be used to create all compose services, networks, and volume definitions adaptive while also using the same arg to build specific images as the case may be.

Eg

BUILD_ENV=production docker-compose up --build Will go ahead and make production environment images and also use production environment env_files and bring up all production services.

While,

BUILD_ENV=dev docker-compose up --build Will go ahead and make dev environment images and also use dev environment env_files and bring up all dev services.

This will make the project structure much more simple and really cut down the number of files to be managed and will make everything dynamic and flexible.

Rationale

Why should this feature be implemented?

This would make the docker aspect of this template much more understandable and much easier to use and modify. It will also make it a lot more dynamic as well. Currently, there are a lot of files that need to be understood and for most files the only difference between production and development environments are using the correct env_file and/or the env specific image.

Please let me know if there is any interest in this because I can implement and raise a PR for the same.

Example, django service would go

from:


  django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %}
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    image: {{ cookiecutter.project_slug }}_production_django
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
    command: /start

to


  django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %}
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
      args:
        BUILD_ENVIRONMENT: ${BUILD_ENV}
    image: {{ cookiecutter.project_slug }}_production_django
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.${BUILD_ENV}/.django
      - ./.envs/.${BUILD_ENV}/.postgres
    command: /start

And the new Django Dockerfile would now use the BUILD_ENVIRONMENT ARG and install the appropriate requirements file.

scheung38 commented 1 year ago

Hi @arnav13081994 arnav13081994 What about volumes: django.local has but django.production there is none.