0

I'm trying to dockerize an existing django application. I've followed the example docker-compose and docker files on https://docs.docker.com/samples/django/

I've added the links and networks suggestion as see on other posts, to no avail.

I've brought up the db by itself with docker-compose up db and run docker ps and confirmed it's up, accepting connections, and has the name db. I'm not sure what else to try.

My docker-compose:

version: '3'

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    container_name: db
    networks:
      harpnetwork:
  harp:
    build:
      context: .
      dockerfile: docker/harp/Dockerfile
    volumes:
      - ./harp:/harp
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 16765:16765
    depends_on:
      - db
    networks:
      harpnetwork:
    links:
      - db:db

networks:
  harpnetwork:

My django db config:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': 'db',
        'PORT': 5432,
    }
}

and my Dockerfile to build the django project

From python:3.10

COPY ./harp harp

WORKDIR /harp

RUN pip install -r requirements.txt

RUN python manage.py migrate

CMD ["python", "manage.py", "runserver", "0.0.0.0:16765"]
Atrus
  • 788
  • 4
  • 13
  • 34
  • Looks correct. What error are you getting? – Sergio Santiago May 01 '22 at 00:17
  • The `links:` option is obsolete and unnecessary, and on some other questions it's seemed like deleting it helps; can you try that? You also can't `RUN ./manage.py migrate` from the Dockerfile for a couple of reasons, but see [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose). – David Maze May 01 '22 at 00:52
  • (You should also not need the manual `networks:` or `container_name:` settings, but `links:` more specifically activates an outdated Docker networking mode which could cause problems.) – David Maze May 01 '22 at 00:52
  • The links, networks, and container_name were all added in an attempt to fix it, however it was actually the RUN ./manage.py migrate that was causing the issue for me – Atrus May 01 '22 at 03:50

0 Answers0