django with docker , TCP/IP connections on port 5432 error
Question:
I have a django application. And the application works fine. But now I try to dockkerize the django application. So my docker file looks like:
# pull official base image
FROM python:3.9-alpine3.13
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update
&& apk add linux-headers postgresql-dev gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY ./requirements.dev.txt .
RUN pip install -r requirements.txt
# copy entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
# copy project
COPY . .
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
And dockercompose looke like:
version: '3.9'
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- .:/app
command: >
sh -c "python manage.py migrate &&
python app/manage.py runserver 192.168.1.135:8000"
env_file:
- variables.env
depends_on:
- db
db:
image: postgres:13-alpine
container_name: postgres
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=db
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
ports:
- '5432:5432'
volumes:
dev-db-data:
dev-static-data:
So when I do: docker-compose up -d –build
I see that the db container is running. But the app container is not running.
And this error occurs:
django.db.utils.OperationalError: could not connect to server: Connection refused
dwl_backend-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
dwl_backend-app-1 | could not connect to server: Address not available
dwl_backend-app-1 | Is the server running on host "localhost" (::1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
And of course I googled. And I found something that I have to change the postgresql.conf file and set the port to 5432. But port number is setting correct.
Question: how to tackle this error?
This i have in settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': environ.get("DATABASE_NAME"),
'USER': environ.get("DATABASE_USER"),
'PASSWORD': environ.get("DATABASE_PASSWORD"),
'HOST': environ.get("DATABASE_HOST"),
'PORT': environ.get("DATABASE_PORT"),
}
}
Answers:
As it is mentioned in the error message, you application tries to connect to localhost:
django.db.utils.OperationalError: could not connect to server: Connection refused
dwl_backend-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
You have to change to the docker where postgresql is running
postgres:5432
I have a django application. And the application works fine. But now I try to dockkerize the django application. So my docker file looks like:
# pull official base image
FROM python:3.9-alpine3.13
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update
&& apk add linux-headers postgresql-dev gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY ./requirements.dev.txt .
RUN pip install -r requirements.txt
# copy entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
# copy project
COPY . .
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
And dockercompose looke like:
version: '3.9'
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- .:/app
command: >
sh -c "python manage.py migrate &&
python app/manage.py runserver 192.168.1.135:8000"
env_file:
- variables.env
depends_on:
- db
db:
image: postgres:13-alpine
container_name: postgres
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=db
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
ports:
- '5432:5432'
volumes:
dev-db-data:
dev-static-data:
So when I do: docker-compose up -d –build
I see that the db container is running. But the app container is not running.
And this error occurs:
django.db.utils.OperationalError: could not connect to server: Connection refused
dwl_backend-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
dwl_backend-app-1 | could not connect to server: Address not available
dwl_backend-app-1 | Is the server running on host "localhost" (::1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
And of course I googled. And I found something that I have to change the postgresql.conf file and set the port to 5432. But port number is setting correct.
Question: how to tackle this error?
This i have in settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': environ.get("DATABASE_NAME"),
'USER': environ.get("DATABASE_USER"),
'PASSWORD': environ.get("DATABASE_PASSWORD"),
'HOST': environ.get("DATABASE_HOST"),
'PORT': environ.get("DATABASE_PORT"),
}
}
As it is mentioned in the error message, you application tries to connect to localhost:
django.db.utils.OperationalError: could not connect to server: Connection refused
dwl_backend-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
dwl_backend-app-1 | TCP/IP connections on port 5432?
You have to change to the docker where postgresql is running
postgres:5432