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
Related
I'm trying to connect to an express database on sql server accesible throughout 192.168.0.130:1433 on local network from Docker Django container.
I'm on a Mac and from local host i have ping
$ ping 192.168.0.130
64 bytes from 192.168.0.130: icmp_seq=0 ttl=128 time=5.796 ms
64 bytes from 192.168.0.130: icmp_seq=1 ttl=128 time=2.234 ms
But inside docker container get timeout error.
docker-compose.yml:
version: '3.7'
services:
...
django:
container_name: djangonoguero_django_ctnr
build:
context: .
dockerfile: Dockerfile-django
restart: unless-stopped
env_file: ./project/project/settings/.env
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./project:/djangonoguero
depends_on:
- postgres
ports:
- 8000:8000
networks:
- djangonoguero-ntwk
networks:
djangonoguero-ntwk:
driver: bridge
Anybody could help me please ?
Thanks in advance.
Reset fabric default values from Docker solved the problem.
For example, you have postgres container:
db:
image: postgres
environment:
# This is for example, not use this in production!!!
POSTGRES_USER: developer
POSTGRES_DB: db
POSTGRES_PASSWORD: Passw0rd33
...
Then, in your django project settings, you should write:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db',
'USER': 'developer',
'PASSWORD': 'Passw0rd33',
'HOST': 'db',
}
}
Be careful, this is example settings! Best practice is to store this data in .env file!
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
My django app is not dockerized, but I run postgres inside docker container using docker-compose.yml script. After docker-compose up I can connect to db with dbeaver, but not with django app. Every time I'm getting an error:
django.db.utils.OperationalError: could not translate host name "db" to address:
Temporary failure in name resolution
File docker-compose.yml:
version: "3.9"
services:
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
- POSTGRES_USER="postgres"
- POSTGRES_PASSWORD="postgres"
- POSTGRES_DB="postgres"
ports:
- 5432:5432
volumes:
postgres_data
Django project file config/settings.py:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env.str("DB_NAME"),
'USER': env.str("DB_USER"),
'PASSWORD': env.str("DB_PASS"),
'HOST': env.str("DB_HOST"),
'PORT': env.decimal("DB_PORT")
}
}
It was some kind of pipenv error. When I restarted terminal and executed pipenv shell again and then python manage.py runserver everything worked just fine
I'm attempting to follow the guide provided by: https://docs.docker.com/compose/django/
Whenever I attempt to makemigrations, it gives me the Unknown host error given in the title. I'm trying to use PostgreSQL with Django and Wagtail as its CMS
My docker-compose.yml looks like:
version: "3.9"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
and my settings in the settings.py file look like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
Am I missing anything?
Your code can run in two different environments, and hard-coding the connection information might not be correct.
You mention in a comment that you're running something like:
docker-compose up -d db
python manage.py makemigrations
In this environment python is running outside of Docker. If you add ports: [5432:5432] to the database configuration in the docker-compose.yml file, the database will be accessible via (probably) localhost. On the other hand, when you run docker-compose up, the application runs inside Docker and the database will be reachable at db.
You can use an environment variable to configure this. I find it useful to give these variables default values that would be useful for a developer, and set them to different values in my deployment setup (the docker-compose.yml).
DATABASES = {
'default': {
...
'HOST': os.getenv('DB_HOST', 'localhost'),
...
}
}
version: "3.9"
services:
db:
ports:
- '5432:5432' # makes this accessible from your development environment
...
web:
environment:
- DB_HOST=db
...
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?