Home > Software engineering >  Django and Postgres problems with dockerizing
Django and Postgres problems with dockerizing

Time:10-12

Hello I can not dockerize my django app because I got an error -

listen tcp4 0.0.0.0:5433: bind: address already in use

From the other hand, when I "kill" 5433 port in ubuntu terminal I get this error

Is the server running on host "localhost" (::1) and accepting
web_1  |        TCP/IP connections on port 5433?

What can I do to solve this problem and dockerize successfully my app? Dockerfile

FROM python:3

RUN adduser --system --no-create-home django

ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ENV PYTHONPATH /code



EXPOSE 8000
USER django

CMD ["./main.py"]

docker-compose

version: "3"
   
services:
  

    db:
        image: postgres
        volumes:
          - ./data/db:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=fitshop
          - POSTGRES_USER=fituser
          - POSTGRES_PASSWORD=fitpass
        ports:
          - "5433:5433"
      web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        restart: always
        ports:
          - "8000:8000"
        depends_on:
          - db

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'fitshop',
        'USER': 'fituser',
        'PASSWORD': 'fitpass',
        'HOST': 'localhost',
        'PORT': '5433',
    }
}

CodePudding user response:

You should use "db" as host here. In a docker-compose setup each service is started in a separate Container and the /etc/hosts config of all containers is modified so, that each container can find the other containers via the service names.

CodePudding user response:

I`ve got it!. In 'localhost" port of postgres was 5433 but for 'db' as HOST it was default, 5432.

  • Related