I am running two different django
projects in my machine. My machine is running on os -ubuntu 16.04
.
I am very new to docker
. As far as I know, the only way to differentiate between two projects setup is defining different containers. To have different containers, I have given different container_name
in my docker-compose.yml
file. So, basically, I do have different containers name for two different project and I have used different postgres
database name in settings.py file for the each project as well.
Following are two different docker-compose.yml
file configurations
PROJECT - 1
version: '3'
services:
nginx:
restart: always
image: nginx:latest
container_name: NGINX_P1
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
restart: always
build: .
container_name: DJANGO_P1
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn safersit.wsgi -b 0.0.0.0:8000 --reload"
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
db:
restart: always
image: postgres:latest
container_name: PSQL_P1
And, the settings.py file for project-1 is :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres_test', <--- different name for postgres
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
For PROJECT - 2
version: '3'
services:
nginx:
restart: always
image: nginx:latest
container_name: NGINX_P2
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
restart: always
build: .
container_name: DJANGO_P2
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn safersit.wsgi -b 0.0.0.0:8000 --reload"
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
db:
restart: always
image: postgres:latest
container_name: PSQL_P2
And, the settings.py file for project-2 is as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
The project-1 is already setup and running well. But, when I try to run the second project, it is giving following error:
DJANGO_P2 | users.User.numbers: (models.E006) The field 'numbers' clashes with the field 'numbers' from model 'users.user'.
I don't have any field named numbers
in my seconds project users model, but it is trying to access it from my first project (project-1). I am very confused right now with this set up. Am I doing it right way? Why is my second project trying to access database from first one, even though I do have different container name and database name?
PROJECT-1
folder structure
.
├── config
│ ├── nginx
│ └── requirements.pip
├── docker-compose-backup.yml
├── docker-compose.yml
├── Dockerfile
├── README.md
└── src
├── project1
├── manage.py
├── messages
└── users
PROJECT-2
folder structure:
.
├── config
│ ├── nginx
│ └── requirements.pip
├── docker-compose.yml
├── Dockerfile
├── README.md
└── src
├── manage.py
├── project2
└── users
And the running containers list from both projects is as follows :
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f12e1f78ce3 nginx:latest "nginx -g 'daemon ..." 21 minutes ago Exited (0) 11 minutes ago NGINX_P1
6c20c4a10a8a project1server_web "bash -c 'python m..." About an hour ago Up 23 minutes
8000/tcp DJANGO_P1
b7781939ce29 postgres:latest "docker-entrypoint..." About an hour ago Up 23 minutes
5432/tcp PSQL_P1
4600da6f7d29 nginx:latest "nginx -g 'daemon ..." 9 hours ago Exited (0) 9 minutes ago NGINX_P2
3069796edfd5 project2server_web "bash -c 'python m..." 9 hours ago Restarting (1) 14 minutes ago DJANGO_P2
3be863184995 postgres:latest "docker-entrypoint..." 9 hours ago Up About an hour 5432/tcp PSQL_P2
I will be grateful for your guidance.