0

I'm trying to launch my django app inside docker container

docker-compose.yml

version: '3.8'

services:
  api:
    build:
      context: .
      dockerfile: ./Dockerfile-local
    ports:
      - "8000:8000"
    entrypoint: ./entrypoint-local.sh
    env_file:
      - .env-local
    links:
      - postgresql
  postgresql:
    image: postgres:12.4
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_DB=frov_db
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_HOST=postgresql
      - POSTGRES_PORT=5432
      - PGDATA=/var/lib/postgresql/data/pgdata
      - C_FORCE_ROOT=true
    ports:
      - '5433:5432'

Dockerfile-local

FROM python:3.8-slim

ENV PYTHONDONTWRITEBYTECODE 1

WORKDIR /usr/src/app

COPY Pipfile.lock Pipfile ./
RUN pip install -U setuptools pip pipenv && pipenv install --system --deploy --ignore-pipfile --dev

COPY . .

RUN touch /var/log/filebeat.log && chmod 664 /var/log/filebeat.log \
&& chmod +x ./entrypoint-local.sh

entrypoint-local.sh

#!/bin/bash

python manage.py collectstatic --no-input --settings frov.settings.default

python manage.py migrate --settings frov.settings.default
python manage.py seed

gunicorn -b 0.0.0.0:8000 --workers=1 --env DJANGO_SETTINGS_MODULE=frov.settings.default frov.wsgi:application

.env-local

DEBUG=True
DJANGO_SETTINGS_MODULE=frov.settings.default
DB_NAME=frov_db
DB_USER=postgres
DB_PASS=postgres
DB_HOST=postgresql
DB_PORT=5433

And now I have now idea, why do I get such error

api_1         | psycopg2.OperationalError: could not connect to server: Connection refused
api_1         |     Is the server running on host "postgresql" (172.21.0.2) and accepting
api_1         |     TCP/IP connections on port 5433?

Can anyone hepls me? I just can't figure out where the mistake can take place

4
  • 1
    5433 is the port exposed to the host. Inside docker network the postgres is still accessible by postgresql:5432, so try to switch the port in the env to 5432 Commented Nov 1, 2020 at 9:16
  • @awesoon - oh, yes, you are right. If you want, you can post an answer and I will accept it Commented Nov 1, 2020 at 9:23
  • @Stefano 5432 is a container port exposed as 5433 host port. However, even docs refer to 5433 port as the publicly exposed port. So I apologize for confusion, but I don't think I'm wrong here. Commented Nov 1, 2020 at 10:14
  • you're right. I apologize. Commented Nov 1, 2020 at 10:23

1 Answer 1

1

This ports config section:

ports:
  - '5433:5432'

Exposes 5432 container port to 5433 host port. However within the docker network the db is still accessible on the 5432 port, so you need to change your .env file to use 5432 port.

If you don't need other apps to access db from the host you may omit this ports section at all (you'll still be able to connect to it using e.g. docker-compose exec)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.