I have a docker-compose file with django project that trying to use database situated on host machine.
Now my Dockerfile is:
FROM python:3-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /code.
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt
RUN export dockerhost=$(docker-machine ip)
docker-compose.yml:
version: "2"
networks:
workernetwork:
webnetwork:
services:
static:
volumes:
- /static:/static
- /media:/media
image: alpine:latest
web:
build: .
command: bash -c "SECRET_KEY=temp_value python /code/manage.py collectstatic --noinput && python /code/manage.py migrate && /code/run_gunicorn.sh"
volumes:
- .:/code
volumes_from:
- static
env_file:
- secrets.env
ports:
- 443:443
networks:
- webnetwork
extra_hosts:
- "dockerhost:${dockerhost}"
DATABASES in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'revolution',
'USER': get_env_setting('POSTGRES_USER'),
'PASSWORD': get_env_setting('POSTGRES_PASSWORD'),
'HOST': 'dockerhost',
'PORT': 5432,
}
}
What Im doing wrong?
Thx for attention!
Finally resolved with docker volume:
as first, create volume:
docker volume create --name=coredb
docker-compose.yml
version: "2"
services:
...
web:
build:
context: .
command: bash -c "python /code/manage.py collectstatic --noinput && python /code/manage.py migrate && /code/run_gunicorn.sh"
volumes:
- /static:/data/web/static
- /media:/data/web/media
- .:/code
env_file:
- ../.env
depends_on:
- db
db:
restart: always
image: postgres
env_file:
- ../.env
volumes:
- pgdata: /var/lib/postgresql/data
volumes:
pgdata:
external:
name: coredb
.env variables:
POSTGRES_DB={hidden}
POSTGRES_USER={hidden}
POSTGRES_PASSWORD={hidden}
Important:
You need to create database and user manually via docker exec -it backend_db_1 bash and following this instructions till Install Django within a Virtual Environment chapter
Related
So I have a successfully built image where I have postgres mounted as a volume, however I keep getting django.db.utils.OperationalError: could not connect to server: Connection refused- when I ran docker-compose up. My Dockerfile and docker-compose.yml looks like this:
# syntax=docker/dockerfile:1
FROM python:3.10-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /track
COPY requirements.txt .
RUN apt-get update \
&& apt-get -y install libpq-dev gcc \
&& pip install --upgrade pip \
&& pip install --default-timeout=100 -r requirements.txt
COPY . .
ENV DJANGO_SETTINGS_MODULE track.settings
EXPOSE 8000
version: "3.9"
services:
django:
build: .
command: python manage.py runserver 0.0.0.0:8000
stdin_open: true
tty: true
volumes:
- .:/track
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:9.6.10-alpine
restart: always
user: postgres
volumes:
- track_db:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASS: postgres
POSTGRES_DB: postgres
volumes:
track_db: {}
Please, what am I doing wrong?
Jango service must be linked to db:
services:
django:
build: .
command: python manage.py runserver 0.0.0.0:8000
stdin_open: true
tty: true
volumes:
- .:/track
ports:
- "8000:8000"
depends_on:
- db
links:
- db
Now you can connect to db by container hostname
I'm trying to connect Django to the PostgreSQL server but still didn't manage to see what's the problem.
The docker container seems up and running correctly, but in Django, I get this error:
Pgdb throws me this error:
pgdb | 2021-06-09 23:49:00.871 UTC [35] FATAL: password authentication failed for user "django"
pgdb | 2021-06-09 23:49:00.871 UTC [35] DETAIL: Role "django" does not exist.
pgdb | Connection matched pg_hba.conf line 99: "host all all all md5"
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
docker-compose.yml
version: '3.8'
services:
django:
build: .
container_name: django
command: >
sh -c "python manage.py makemigrations &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/usr/src/app/
ports:
- '8000:8000'
environment:
DB_NAME: django
DB_USER: django
DB_HOST: pgdb
DB_PORT: 5432
DB_PASSWORD: django
CELERY_BROKER: redis://redis:6379/0
depends_on:
- pgdb
- redis
celery:
build: .
command: celery -A conf worker -l INFO
volumes:
- .:/usr/src/app/
depends_on:
- django
- redis
pgdb:
image: postgres
container_name: pgdb
environment:
POSTGRES_USER: django
POSTGRES_PASSWORD: django
ports:
- '5432:5432'
volumes:
- pgdata:/var/lib/postgresql/data/
redis:
image: 'redis:alpine'
volumes:
pgdata:
Django settings includes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
},
}
What's the problem? Am I missing something?
I know there have been similar questions on this site, but the answers to these are sadly outdated.
So I have a django application and i want to use postgres as the underlying database.
Additionally a want to separate both programs in separate dockers.
Now the docker docs have a way to do it, but sadly it seems to be outdated: link
The problem appears when i call manage.py migrate in the docker build function which is being run by the docker compose file.
But i get the error that the host 'db' is unknown/invalid.
Compose excerpt:
services:
db:
image: postgres
restart: always
volumes:
- DataBase:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
app:
build: FehlzeitErfassungProject
restart: always
volumes:
- Logs:/app/logs
- Backups:/app/Backups
- Media:/app/MEDIA
ports:
- "5432:5432"
depends_on:
- db
app dockerfile:
FROM ubuntu
WORKDIR /app
ADD ./requirements.txt ./
RUN apt-get update && \
apt-get upgrade -y
# getting tzdata to shutup
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Berlin
RUN apt-get -y install tzdata
#installing all needed porgramms
RUN apt-get install -y uwsgi python3 python3-pip
RUN python3 -m pip install pip --upgrade
RUN python3 -m pip install -r requirements.txt
COPY ./ ./
RUN ./manage.py migrate
CMD ["uwsgi", "uwsgu.ini"]
PS: It seems to appear, that the other dockers are only launched when the app docker has already finished building
Edit:
the database_settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '127.0.0.1',
'PORT': 5432,
}
}
I think that your problem is in the file "settings.py" in the DB configuration for the HOST param, you need to have the name of the postgresql service that you have defined in the docker-compose file, in this case, is "db".
BTW I think that using constants in settings.py is bad behavior, it's better to define in the docker-compose file environmental variables.
In your case would be:
docker-compose.yml
services:
db:
image: postgres
restart: always
volumes:
- DataBase:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
app:
build: FehlzeitErfassungProject
restart: always
volumes:
- Logs:/app/logs
- Backups:/app/Backups
- Media:/app/MEDIA
ports:
- "5432:5432"
environment:
- DB_HOST=db
- DB_NAME=postgres
- DB_USER=postgres
- DB_PASSWORD=postgres
depends_on:
- db
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
}
}
Remember to import os in settings.py.
binding both container port at same port for the host will not work.
to access your database in other container you should link to it.
version: "3.0"
services:
db:
image: postgres
restart: always
volumes:
- DataBase:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
app:
build: FehlzeitErfassungProject
restart: always
volumes:
- Logs:/app/logs
- Backups:/app/Backups
- Media:/app/MEDIA
ports:
- "8000:8000"
links:
- "db"
depends_on:
- db
I'm relatively new to Django and Docker and am following a tutorial to build a mini application. However, I'm getting stuck with the following error:
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known
My docker-compose file looks as follows:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
My settings.py file contains the database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
I've seen the post here and here however both have not fixed the issue.
Would appreciate some guidance. Thanks.
So you are trying to reach the db which is running in one container from another container? If yes - the following could potentially help, at least it helped me when I had similar issues.
Try to define networks config in addition to links in your compose file, create a network with some name and define it in both services. Like described here, as the docks on links config recommend to do that.
Something like this for your case:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
networks:
some_network:
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
networks:
some_network:
networks:
some_network:
It helped me to resolve the host name to connect to the db.
I had to use docker-compose up to make sure my db container was running.
Instead of redefining the networks, use
docker-compose down -v
to stop all containers and remove all cached data.
Then
docker-compose up
to restart from scratch.
I've been searching for several days for the solution to this issue. Here's what I did:
1 - I copied the postgresql.conf.sample file from my postgres container to my project folder
you must get in to cd usr/share/postgresql
with docker exec -it yourcontainer bash
2 - I changed its name to postgresql.conf
3 - I modified these variables in postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 100
superuser_reserved_connections = 3
unix_socket_directories = '/tmp'
4 - I added it to my Dockerfile
COPY postgresql.conf /tmp/postgresql.conf
5 - settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test',
'USER': 'testing',
'PASSWORD': 'tests',
'HOST': 'localhost',
'PORT': 5432,
}
}
6 - I did docker-compose up -build again
I came across this issue recently, i solved it by adding postgres environment variables to db in my docker-compose.yml file like so
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
environment:
- POSTGRES_DB=booksdb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
In my specific case, I had postgres without any version, so postgres image gets updated and the db (postgres) container won't get up
The following configuration works for me from official documentation
version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
then run those commands:
docker-compose up -d --build
docker-compose logs
docker exec -it "container_name" python3 manage.py migrate
This works for me. Make sure of the path to the manage.py file is correct.
docker-file:
FROM python:3.6
WORKDIR /usr/src/jobsterapi
COPY ./ ./
RUN pip install -r requirements.txt
CMD ["/bin/bash"]
docker-compose:
version: '3.6'
services:
#Backend API
jobsterapi:
container_name: jobsterapi
build: .
command: python src/manage.py runserver 0.0.0.0:8000
working_dir: /usr/src/jobsterapi
links:
- mongodb
depends_on:
- mongodb
ports:
- "8000:8000"
volumes:
- ./:/usr/src/facerecognition-api
mongodb:
restart: always
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_INITDB_ROOT_USERNAME=${soubhagya}
- MONGO_INITDB_ROOT_PASSWORD=${Thinkonce}
- MONGODB_USERNAME='soubhagya'
- MONGODB_PASSWORD='Thinkonce'
- MONGODB_DATABASE=='jobster'
volumes:
- ./data/db:/var/micro-data/mongodb/data/db
ports:
- 27017:27017
command: mongod --smallfiles --logpath=/dev/null # --quiet
django-database setting:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'jobster',
'user': 'soubhagya',
'password':'Thinkonce',
'port': 27017
}
}
i don't know actually how to setup django with mongodb inside docker-compose. i am trying from some blogs by this way. but it is not working.
please have a look into my code.
You have errors in your environment:
- MONGO_INITDB_ROOT_USERNAME=${soubhagya}
- MONGO_INITDB_ROOT_PASSWORD=${Thinkonce}
- MONGO_INITDB_DATABASE=='jobster'