Repository: cookiecutter/cookiecutter-django
Pull request: Avoid docker image/volume collision by prefixing with project slug
<!-- Thank you for helping us out: your efforts mean a great deal to the project and the community as a whole! -->


## Description

Hi, it's my first contribution ever, this pull request is related to issue #3254 and issue #2849. 
The problem happens when we create a second django-cookiecutter project on our dev machine, there is a conflict between the ``container_name: postgres`` from the first project with the new one:

```
ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for docs  Cannot create container for service docs: Conflict. The container name "/docs" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.
ERROR: Encountered errors while bringing up the project.
```


Checklist:

- [x] I've made sure that tests are updated accordingly (especially if adding or updating a template option)
- [x] I've updated the documentation or confirm that my change doesn't require any updates

## Rationale


To avoid conflict, the ```compose_name``` has now a prefix of the ```project_slug``` defined on the project creation.
Fix #2849 
Fix #3254 



Problem statement:
The container name "/postgres" is already in use by container "*"
## What happened?
When I tried to create and run a new project with docker-compose I got these errors:
```
ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for docs  Cannot create container for service docs: Conflict. The container name "/docs" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "...". You have to remove (or rename) that container to be able to reuse that name.
ERROR: Encountered errors while bringing up the project.
```
## What should've happened instead?
Should run the new project without any problem. When I renamed the containers they worked. We could add the project slug name to `container_name`.

## Additional details

I created a new project with docker support, then:
```
docker-compose -f local.yml build
```
Finally:
```
docker-compose -f local.yml up
```
<summary>
Logs:
<details>
<pre>
$ docker-compose -f local.yml up

Creating network "test_api_default" with the default driver
Creating volume "test_api_local_postgres_data" with default driver
Creating volume "test_api_local_postgres_data_backups" with default driver
Creating postgres ... 
Creating docs     ... error

Creating postgres ... error
ontainer to be able to reuse that name.

ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "*". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for docs  Cannot create container for service docs: Conflict. The container name "/docs" is already in use by container "*". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for postgres  Cannot create container for service postgres: Conflict. The container name "/postgres" is already in use by container "*". You have to remove (or rename) that container to be able to reuse that name.
ERROR: Encountered errors while bringing up the project.
</pre>
</details>
</summary>


Update container_name for docker-compose YAML files
## Description

It's a suggestion to have the default container_name  (django, postgres, docs)
set to 
container_<image_name>

## Rationale

The names "django", "postgres" and "docs" are not unique if one plans to create two projects and work on them simultaneously until they change one explicitly.

Hints from issue discussion:
When you remove the container name definition for the docker-compose services, the current folders name is used as prefix for the container name.
By default this is the project slug.
Removing the container names would also allow to pass the `--project-name` option to docker-compose to setup several environments (due to port collisions only one can run at a time though).

My preferred way would be to remove the container_name definitions from the docker-compose files and add the `--project-name={{project_slug}}` option in the documentation.
Hi I develop multiple django projects on my dev machine. 

Adding specific names to the containers creates conflict between multiple django projects. The older method of not specifying any container_name worked quite well.

Is there any specific reason we adopted the 'container_name' param?

Is it possible to go back to not specifying container names atleast in local.yml?
This was added in #2545 and the PR explains the rationale. However, the problems described in this issue are more important than the problem it's solving, in my opinion. As I said in the original PR, `docker-compose` has commands to achieve the same thing.

All things considered, I agree with the suggestion to remove `container_name` in our compose file.
I noticed that it seems that container names are set properly in the `production.yml` file. I solved this error by adding `<project_name>_local_` in front of all `container_name` fields in `local.yml`. Like is done in `production.yml`. 

This would seem to be a reasonable 'fix' in my opinion.
On further inspection, this already seems implemented in `local.yml` in master (already a long time ago according to `blame`). 
So why this was not rendered properly in my case is a mystery to me...
> On further inspection, this already seems implemented in `local.yml` in master (already a long time ago according to `blame`).

Thanks for looking into this @abaart, but this is not implemented (as can be seen [here](https://github.com/pydanny/cookiecutter-django/blob/22d9f99500bd4ed87ecd6d9667a0d502a27ecbaa/%7B%7Bcookiecutter.project_slug%7D%7D/local.yml#L33)). I think you are looking at the `image` field rather than the `container_name` field.
> > On further inspection, this already seems implemented in `local.yml` in master (already a long time ago according to `blame`).
> 
> Thanks for looking into this @abaart, but this is not implemented (as can be seen [here](https://github.com/pydanny/cookiecutter-django/blob/22d9f99500bd4ed87ecd6d9667a0d502a27ecbaa/%7B%7Bcookiecutter.project_slug%7D%7D/local.yml#L33)). I think you are looking at the `image` field rather than the `container_name` field.

You are correct, it seems I mixed up those fields. It is indeed not implemented.
thought is was an easy fix but it breaks postgres
Agree, or <project_slug>_django, <project_slug>_postgres, <project_slug>_docs and etc..
Care enough to send a PR to fix it @pcampos119104?
Also related: #2849
Yes, would be my first time ever. What should I do? Just fork, fix, run tox(and pass) and make a PR?
First contribution, nice 😄 

You can do everything from your browser. Don't worry too much about tests, they are being run in our CI (more on that later). Here is what I recommend to do:

1. Fork the repo under your own account
2. Create a branch using the branch drop down: 
    <img width="370" alt="image" src="https://user-images.githubusercontent.com/861044/148962735-dce78b38-5209-4e87-897c-920bda183347.png">
3. Press "." which should redirect you to a VSCode in your browser, where you can make the changes you need
4. Commit using the source control panel: 
     <img width="324" alt="image" src="https://user-images.githubusercontent.com/861044/148962952-f7efd841-8919-4a9d-a206-bf51d215fae2.png">
5. Open your fork on github.com again, you should see a notification to create a pull request. If you go to the "Actions" tab on your fork, you can see the results of the tests.

If you want more details, GitHub has [a guide](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork) on how to create pull requests, which you might find helpful.
@browniebroke what do you think of this approach? https://github.com/djangopackages/djangopackages/pull/755/files
@luzfcb sounds quite similar to the solution we're discussing, no? Is the difference that you're also prefixing volume names?

Commit message: Prefix volumes with project slug to avoid conflicts

Patch to review:
diff --git a/docs/developing-locally-docker.rst b/docs/developing-locally-docker.rst
index 17e47bfb44..e0b522c8ce 100644
--- a/docs/developing-locally-docker.rst
+++ b/docs/developing-locally-docker.rst
@@ -167,16 +167,18 @@ docker
 
 The ``container_name`` from the yml file can be used to check on containers with docker commands, for example: ::
 
-    $ docker logs worker
-    $ docker top worker
+    $ docker logs <project_slug>_local_celeryworker
+    $ docker top <project_slug>_local_celeryworker
 
 
+Notice that the ``container_name`` is generated dynamically using your project slug as a prefix
+
 Mailhog
 ~~~~~~~
 
 When developing locally you can go with MailHog_ for email testing provided ``use_mailhog`` was set to ``y`` on setup. To proceed,
 
-#. make sure ``mailhog`` container is up and running;
+#. make sure ``<project_slug>_local_mailhog`` container is up and running;
 
 #. open up ``http://127.0.0.1:8025``.
 
diff --git a/{{cookiecutter.project_slug}}/local.yml b/{{cookiecutter.project_slug}}/local.yml
index 89c087821b..38aef3b4a1 100644
--- a/{{cookiecutter.project_slug}}/local.yml
+++ b/{{cookiecutter.project_slug}}/local.yml
@@ -1,8 +1,8 @@
 version: '3'
 
 volumes:
-  local_postgres_data: {}
-  local_postgres_data_backups: {}
+  {{ cookiecutter.project_slug }}_local_postgres_data: {}
+  {{ cookiecutter.project_slug }}_local_postgres_data_backups: {}
 
 services:
   django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %}
@@ -10,7 +10,7 @@ services:
       context: .
       dockerfile: ./compose/local/django/Dockerfile
     image: {{ cookiecutter.project_slug }}_local_django
-    container_name: django
+    container_name: {{ cookiecutter.project_slug }}_local_django
     depends_on:
       - postgres
       {%- if cookiecutter.use_celery == 'y' %}
@@ -33,16 +33,16 @@ services:
       context: .
       dockerfile: ./compose/production/postgres/Dockerfile
     image: {{ cookiecutter.project_slug }}_production_postgres
-    container_name: postgres
+    container_name: {{ cookiecutter.project_slug }}_production_postgres
     volumes:
-      - local_postgres_data:/var/lib/postgresql/data:Z
-      - local_postgres_data_backups:/backups:z
+      - {{ cookiecutter.project_slug }}_local_postgres_data:/var/lib/postgresql/data:Z
+      - {{ cookiecutter.project_slug }}_local_postgres_data_backups:/backups:z
     env_file:
       - ./.envs/.local/.postgres
 
   docs:
     image: {{ cookiecutter.project_slug }}_local_docs
-    container_name: docs
+    container_name: {{ cookiecutter.project_slug }}_local_docs
     build:
       context: .
       dockerfile: ./compose/local/docs/Dockerfile
@@ -59,7 +59,7 @@ services:
 
   mailhog:
     image: mailhog/mailhog:v1.0.0
-    container_name: mailhog
+    container_name: {{ cookiecutter.project_slug }}_local_mailhog
     ports:
       - "8025:8025"
 
@@ -68,12 +68,12 @@ services:
 
   redis:
     image: redis:6
-    container_name: redis
+    container_name: {{ cookiecutter.project_slug }}_local_redis
 
   celeryworker:
     <<: *django
     image: {{ cookiecutter.project_slug }}_local_celeryworker
-    container_name: celeryworker
+    container_name: {{ cookiecutter.project_slug }}_local_celeryworker
     depends_on:
       - redis
       - postgres
@@ -86,7 +86,7 @@ services:
   celerybeat:
     <<: *django
     image: {{ cookiecutter.project_slug }}_local_celerybeat
-    container_name: celerybeat
+    container_name: {{ cookiecutter.project_slug }}_local_celerybeat
     depends_on:
       - redis
       - postgres
@@ -99,7 +99,7 @@ services:
   flower:
     <<: *django
     image: {{ cookiecutter.project_slug }}_local_flower
-    container_name: flower
+    container_name: {{ cookiecutter.project_slug }}_local_flower
     ports:
       - "5555:5555"
     command: /start-flower
@@ -112,7 +112,7 @@ services:
       context: .
       dockerfile: ./compose/local/node/Dockerfile
     image: {{ cookiecutter.project_slug }}_local_node
-    container_name: node
+    container_name: {{ cookiecutter.project_slug }}_local_node
     depends_on:
       - django
     volumes:


Code review comments:
diff_hunk: @@ -33,16 +33,16 @@ services:
       context: .
       dockerfile: ./compose/production/postgres/Dockerfile
     image: {{ cookiecutter.project_slug }}_production_postgres
-    container_name: postgres
+    container_name: {{ cookiecutter.project_slug }}_production_postgres
path: {{cookiecutter.project_slug}}/local.yml
text: @user1:
```suggestion
    container_name: {{ cookiecutter.project_slug }}_local_postgres
```