I am running a PyCharm 2018.2 version.
I have my django code running on a docker instance and the Postgres running on another docker instance.
My docker-compose.yml file is here
version: "2.3"
services:
cv_db:
container_name: cv_db
image: postgres:10.3-alpine
restart: always
ports:
- "9081:5432"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: ****
POSTGRES_DB: cv
volumes:
- cv_db:/var/lib/postgresql/data
expose:
- "9081:5432"
cv_redis:
container_name: cv_redis
image: redis:4.0.5-alpine
restart: always
ports:
- "6379:6379"
cv:
container_name: cv
image: cv
restart: always
depends_on:
- cv_redis
- cv_db
ports:
- "9080:9080"
- "8080:8080"
expose:
- "9080"
build:
context: .
args:
http_proxy:
https_proxy:
no_proxy:
TF_ANNOTATION: "no"
USER: "django"
DJANGO_CONFIGURATION: "production"
WITH_TESTS: "no"
environment:
DJANGO_MODWSGI_EXTRA_ARGS: ""
DJANGO_LOG_SERVER_URL: ""
volumes:
- cvat_data:/home/django/data
- cvat_keys:/home/django/keys
- cvat_logs:/home/django/logs
- ./share:/home/django/share
volumes:
cv_db:
cv_data:
cv_keys:
cv_logs:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
# 'HOST': 'cv_db',
'HOST': 'localhost',
'PORT': '9801',
'NAME': 'cv_dev',
'USER': 'root',
'PASSWORD': os.getenv('POSTGRES_PASSWORD', '****'),
}
I am using the django debugger:
Edit Configuration Settings are below:
Host: 0.0.0.0 Port: 9080
Run Browser: http://0.0.0.0:9080/
WHen I click on debug, I get the error saying it cant connect to Database:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 9081?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 9081?
what might be going wrong?
Related
django.db.utils.OperationalError: connection to server at "db" (172.18.0.2), port 5432 failed: FATAL: the database system is starting up
I have an issue connecting to the Postgres contaner. I was trying in different ways like setting passwords only in the docker-compose file. Still am stuck.
docker-compose.yml
version: '3'
services:
db:
image: postgres:alpine
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=user
- POSTGRES_DB=userdb
volumes:
- django_db:/var/lib/postgresql/data
container_name: db
singup:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
container_name: singup
depends_on:
- db
volumes:
django_db:
database setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'userdb',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'db',
}
}
This is a timing issue, even thought you have the below line
singup:
depends_on:
- db
It's just waiting for db container to be up and running, not neccessarily Postgres.
To avoid this, use a tool such as wait-for-it, dockerize, sh-compatible wait-for, or RelayAndContainers template. These are small wrapper scripts which you can include in your application’s image to poll a given host and port until it’s accepting TCP connections.
For example, to use wait-for-it.sh or wait-for to wrap your service’s command:
singup:
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
Repo of wait-for-it: https://github.com/vishnubob/wait-for-it
I have a problem connecting my Postgres database to django project on docker container.
I have already tried every solution found on the internet and nothing seems to work.
I've already added host all all all md5 to my pg_hba.conf file, listen_addresses = '*' to postgresql.conf.
Here is log from lsof -i :5432:
postgres 19774 postgres 5u IPv4 2046377 0t0 TCP *:postgresql (LISTEN)
postgres 19774 postgres 6u IPv6 2046378 0t0 TCP *:postgresql (LISTEN)
I've tried to open 5432 port by using ufw allow 5432/tcp, it seems to be open.
My docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:12.0-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
build: .
container_name: db
volumes:
- postgres_data:/var/lib/postgresql/data/
networks:
- djangonetwork
ports:
- '5432'
web:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 127.0.0.1:8000
volumes:
- ./TwitterClone/:/usr/src/TwitterClone/
ports:
- 8000:8000
env_file:
- ./.env
depends_on:
- db
links:
- db:db
networks:
- djangonetwork
networks:
djangonetwork:
driver: bridge
volumes:
postgres_data:
settings.py:
try:
DATABASE_PASSWORD = os.environ.get('POSTGRES_PASSWORD')
DATABASE_USER = os.environ.get('POSTGRES_USER')
DATABASE_NAME = os.environ.get('POSTGRES_DB')
except:
print('Could not get environment variables')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASSWORD,
'HOST': '127.0.0.1',
'PORT': 5432,
}
}
No matter what I try to do, I always end up getting that error:
django.db.utils.OperationalError: 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?
Thank you in advance for any help, anything could help. Just let me know if you need more information from me, I will gladly provide it.
EDIT:
I have edited my docker-compose file by deleting all networks related lines. Now I don't get the previous error, but it seems to have a problem with my PostgreSQL server running on port 5432:
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint db (a1ed9a70eddb931e5d2a26522cb95a2873e019029420f8b994146e6bebe5ce09): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
Having following configuration placed in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql$
'NAME': 'database1', # Or path to database file if $
# The following settings are not used with sqlite3:
'USER': 'database1_role',
'PASSWORD': 'database1_password',
'HOST': 'database1', # Empty for localhost throu$
'PORT': '5432', # Set to empty string for defaul$
'ATOMIC_REQUESTS': True
}
}
docker-compose.yml:
version: '3'
services:
# web container, with django + gunicorn
djangoapp:
build: .
environment:
- DJANGO_SETTINGS_MODULE
volumes:
- .:/opt/services/djangoapp/src
- static:/opt/services/djangoapp/static
- media:/opt/services/djangoapp/media
networks:
- database1_network
- nginx_network
depends_on:
- database1
# reverse proxy container (nginx)
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static:/opt/services/djangoapp/static
- media:/opt/services/djangoapp/media
networks:
- nginx_network
depends_on:
- djangoapp
# database containers, one for each db
database1:
image: postgres:10
environment:
POSTGRES_USER: database1_role
POSTGRES_PASSWORD: database1_password
POSTGRES_DB: database1
ports:
- "5432"
volumes:
- database1_volume:/var/lib/postgresql/data
networks:
- database1_network
networks:
database1_network:
driver: bridge
nginx_network:
driver: bridge
volumes:
database1_volume:
static:
media:
I still do receive following error communicate whenever I am trying to access my site using webbrowser:
OperationalError at / FATAL: password authentication failed for user
"database1_role"
Previously I used to have a message that my database does not even exist / psycopg2 cannot make any connections with it.
I've solved it by replacing the db name in both files:
settings.py as well as POSTGRES_DB, but right at the moment I literally have no idea of what to do right now.
In order to run this setup I am using these two commands:
docker-compose build
docker-compose up
command:
docker-compose ps
clearly says that there are three components running:
Name Command State Ports
-----------------------------------------------------------------------------------
services_database1_1_f84f6d3c38e0 docker-entrypoint.sh postgres Exit 0
services_djangoapp_1_da56c77d50ff gunicorn -c config/gunicor ... Exit 0
services_nginx_1_c6e0edb717c0 nginx -g daemon off; Exit 0
my django app cannot connect to postgresql. I'm using Dockerfile for django and build using docker-compose with postgres official image.
docker-compose.yml
version: '2'
services:
db:
image: eg_postgresql
expose:
- 5432
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
- POSTGRES_DB=postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/test_application
ports:
- "8000:8000"
links:
- "db:db"
environment:
- DATABASE_URL=postgres://docker:docker#db:5432/postgres
- DJANGO_SECRET_KEY=x7-g-xu^h5k%h8860!7ksn=#)7q9frn9_l6tmefvf)y=0)d!uh
output:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
docker-compose ps
ubuntu#ip-172-31-7-117:~/dj1/helloworld$ sudo docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
helloworld_db_1 /usr/lib/postgresql/9.3/bi ... Up 5432/tcp
helloworld_web_1 python3 manage.py runserve ... Up 0.0.0.0:8000->8000/tcp
in app settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'docker',
'PASSWORD': 'docker',
'HOST': 'db',
'PORT': '5432',
}
}
I tried in many ways to connect but the issue is same...
Try replacing expose: - 5432 for your DB service with ports - "5432:5432"
I used to use v1 of docker-compose to run a set of services and then use the dev serve of Django to connect to them. I used to connect to the services (such as a db) with the <machine_ip>:<port> (smt like 192.168.99.100:5432 for postgres)
Now, i just installed docker for mac, and v1 is not working. I moved to version 2 but Django complains that there's no service listening to the port 5432. As far as I understood it's a problem of the network, but I can't get my head around on how to configure it. In theory a default network is created for all the services within the compose, which is fine in general but not for my case. I use compose to spin up a set of services to connect to my local execution of django.
this is my docker-compose file
version: '2'
services:
db:
image: postgres:9.4
volumes_from:
- data
ports:
- "5432:5432"
es:
image: elasticsearch:2.3
ports:
- "9200:9200"
- "9300:9300"
rabbit:
image: rabbitmq:3
ports:
- "5672:5672"
- "48429:48429"
data:
# use this that is already downloaded
# links just to keep the db
image: alpine:3.3
command: echo 'Data Container for PostgreSQL'
volumes:
- /var/lib/postgresql/data
networks:
default:
driver: bridge
and in my django conf
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': '192.168.99.100', # 'localhost',
'PORT': '5432' # '6543'
},
}
where 192.168.99.100 is the docker-machine ip
the result is
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "192.168.99.100" and accepting
TCP/IP connections on port 5432?
any help on how I can expose all these ports to the machine ip?
Try to use dockers internal network features to let the container talk to each other.
version: '2'
services:
db:
image: postgres:9.4
volumes:
- datavolume_postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
es:
image: elasticsearch:2.3
ports:
- "9200:9200"
- "9300:9300"
rabbit:
image: rabbitmq:3
ports:
- "5672:5672"
- "48429:48429"
depends_on:
- "db"
# Named volume
volumes:
datavolume_postgres: {}
#You dont need to specify the default network
and in your django.conf
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db', # 'localhost',
'PORT': '5432' # '6543'
},
}
I don't know on what es depends bu i hope you get the Point
FYI depends_on reference