I'm learning how to work with Compose + Django using this manual https://docs.docker.com/compose/django/
Here are my configuration files
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
When I run using docker-compose up everything is fine.
But when I run using python manage.py runserver I got this error
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
Guess, I have wrong cofiguration
You forgot set hostname in db service.
version: '3'
services:
db:
image: postgres
hostname: db
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Tip: Don't use runserver when you deploy production server. Check this doc
The solution is described in comments
Edit)
separate settings example. Make your original settings.py to base.py in settings folder.
<your_app>/settings/local.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}
<your_app>/settings/deploy.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
So finally, your settings folder contains three python file. base.py, local.py, deploy.py
And when you run server with local settings, python manage.py runserver --settings <your_app>.settings.local.
This is a common problem in docker/django setup , postgres container is initialized but the engine is not ready yet.
You must wait for the postgresql server to start before running the web container.
You can do this in several ways , have a look at dockerize
dockerize -timeout=20s -wait ${POSTGRES_PORT}
I guess you trying to run manage.py runserver on host machine. Error means, that you have no access to host 'db' from it (try ping db in console). There are several causes of this:
Your docker container db is down. Try run docker-compose up -d db
You are using Windows as host system. In Windows there is no access to containers from host, you must install postgres locally.
You have networking problems. Try ping 127.0.0.11 (it's docker's DNS server)
P.S. Do you really want local Django use Postgres in container?
Related
I am trying to python manage.py makemigrations for a django app in postgres, but I am getting the follow error:
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: database "backend_db" does not exist
Before this, I am doing docker compose up with the following docker-compose and .env file:
version: '3.2'
services:
postgres:
image: postgres:13.4
environment:
POSTGRES_DB: backend_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- database-data:/var/lib/postgresql/data/
ports:
- 5432:5432
networks:
- postgres
volumes:
database-data:
driver: local
networks:
postgres:
driver: bridge
DB_NAME='backend_db'
DB_USER='postgres'
DB_PASSWORD='postgres'
# DB_HOST is localhost or the IP of the machine running postgres
DB_HOST='localhost'
DB_PORT='5432'
The part of the settings.py that I define the postgres is the following:
DATABASES = {
'default': get_config(
'DATABASE_URL',
'sqlite:///' + BASE_DIR.child('db.sqlite3'),
cast=db_url
),
'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': get_config('DB_NAME', 'backend_db'),
'USER': get_config('DB_USER', 'postgres'),
'PASSWORD': get_config('DB_PASSWORD', 'postgres'),
'HOST': get_config('DB_HOST', 'postgres-service'),
'PORT': get_config('DB_PORT', '5432')
}
}
Any idea of what causes the error?
I believe you can use:
docker-compose run web python manage.py makemigrations
You may need to change web to your appropriate service.
I encountered the same problem, and it turns out that in my case, there was a PostgreSQL server (via Postbird) running on my local machine and the python server was inadvertently connecting to it instead of to the one in the Docker container. Context: I normally run my python app natively (i.e., locally with python manage.py runserver) and database services in a Docker container, connected to via port forwarding.
The solution was to kill the local Postgres instance and let the python server connect to the Docker instance
I've got an issue with connecting PostgreSQL with Django using Docker.
Error: web_1 | django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"
If I don't use Docker (just runserver command in prompt) it looks as it should, no errors. In another project Docker works correctly with this config.
My docker-compose.yml:
version: '3.8'
services:
web:
build: .
command: python bookstore/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
links:
- db:db
db:
image: postgres:14
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD= postgres
- POSTGRES_DB= postgres
My settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
That is the error I get when running docker-compose exec web python manage.py migrate
my docker-compose.yml contains:
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
This Is what I put for DATABASE in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
I have tried running docker-compose up -d --build and then docker-compose exec web python manage.py migrate
But that doesn't work.
You will need to add an environment for your db service:
services:
# ...
db:
# ...
environment:
POSTGRES_PASSWORD: postgres
as POSTGRES_PASSWORD is the only non-optional env variable needed to run the image as stated in the docs.
I have a configuration of docker-compose, and on building database step, django management throws an error:
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
I think - the docker refusing connections.
Configuration by socket similar not working (Says error connection by socket)
Docker Compose file
version: '3'
services:
db:
image: mariadb:5.5
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "bh"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
ports:
- "3302:3306"
behealthy_dev:
build: .
container_name: behealthy_dev
expose:
- "86"
command: python manage.py runserver 0.0.0.0:80
volumes:
- /behealthy_dev/
ports:
- "86:86"
depends_on:
- db
Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /behealthy_dev
WORKDIR /behealthy_dev
ADD requirements.txt /behealthy_dev/
RUN pip install -r requirements.txt
ADD . /behealthy_dev/
RUN python manage.py makemigrations
RUN python manage.py migrate
RUN python manage.py collectstatic --noinput
Database settings (settings)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bh',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'root',
'OPTIONS': {
'sql_mode': 'traditional',
'init_command': 'SET innodb_strict_mode=1',
'charset': 'utf8mb4',
},
},
}
Have any solutions? I tried to resolve from other stack answer but it's not working for me.
in your service behealthy_dev change the MySQL host from
'HOST': '127.0.0.1'
to
'HOST': 'db',
Edit:
Change also the MYSQL_ROOT_PASSWORD: "root" to:
`MYSQL_ROOT_PASSWORD: "rootpassword"`
and your Database settings to:
'PASSWORD': 'rootpassword',
I have two Docker containers. The first one is Postgresql container, which I run using the following command.
sudo docker run -v /home/mpmsp/project/ezdict/postgresql/data:/var/lib/postgresql/data -p 127.0.0.1:5432:5432 -name my-postgres -d postgres
It is based on official image and it is working perfectly, I can connect to Postgresql from the host.
The second container is a container with my Django application. The image is built using the following Dockerfile (based on this image):
FROM python:3-onbuild
EXPOSE 8000 5432
CMD ["/bin/bash"]
And I run this container with the following command
sudo docker run --link my-postgres:my-postgres -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app
docker ps output shows that containers are linked
NAMES
my-app/my-postgres, my-postgres
However, when I go to localhost:8000, I see an error page from Django, with the following output
OperationalError at /api-auth/login/
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Request Method: GET
Request URL: http://127.0.0.1:8000/api-auth/login/
Django Version: 1.6.4
Exception Type: OperationalError
Exception Value:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Exception Location: /usr/local/lib/python3.4/site-packages/psycopg2/__init__.py in connect, line 164
Python Executable: /usr/local/bin/python
Python Version: 3.4.1
Python Path:
['/usr/src/app',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-linux',
'/usr/local/lib/python3.4/lib-dynload',
'/root/.local/lib/python3.4/site-packages',
'/usr/local/lib/python3.4/site-packages']
Server time: Птн, 10 Окт 2014 12:07:07 +0400
Application's settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
How to make linking work? Thanks in advance
The Dockerfile for your Django image should not expose port 5432 as no Postgresql server will be running in any container created from that image:
FROM python:3-onbuild
EXPOSE 8000
CMD ["/bin/bash"]
Then as you are running the Django container linking it with
--link my-postgres:my-postgres
your settings for the database are incorrect.
In the Django container: 127.0.0.1 refers to the Django container which isn't running any service listening on port 5432.
So your settings.py file should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'my-postgres',
'PORT': '5432',
}
}
As you run your Django container with:
sudo docker run --link my-postgres:db -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app
then your settings.py file would have to be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'db',
'PORT': '5432',
}
}
Ths syncdb only works AFTER both db and django containers are build and started, then you can manually run the syncdb command with fig/docker-compose/docker.
I am thinking of creating an AT job and let the container run the syncdb itself (and creating an admin user after the syncdb - for creating the necessary tables)