0

I get the following error while I try to run my django project on docker.

relation "DBTable" does not exist at character 218

I figured out it was because the migrations weren't being applied. So I changed my Dockerfile as follows to make migrations before starting the server:

FROM python:3.7
WORKDIR /ServerCode2/
COPY . /ServerCode2/
RUN pip install -r req.txt
EXPOSE 8000
CMD ["python", "manage.py", "makemigrations" ]
CMD ["python", "manage.py", "migrate" ]
CMD ["python", "manage.py", "runserver", "--noreload" ]

Below is my docker-compose.yml file

version: '3.7'
services:
  server:
    build:
      context: ./Appname
      dockerfile: Dockerfile
    image: serverimgtest1
    container_name: servercontest1
    ports:
      - 8000:8000
    links:
      - db:db
    depends_on:
      - db

  db:
    image: postgres
    environment:
        POSTGRES_DB: "DBname"
        POSTGRES_HOST_AUTH_METHOD: "trust"
    ports:
        - 5432:5432

  client:
    build:
      context: ./appfrontend
      dockerfile: Dockerfile
    image: clientimgtest1
    container_name: clientcontest1
    depends_on:
      - server
    ports:
      - 3000:3000

However, I still see the error which says the relation does not exist. Is there any way I could achieve migrations through commands in dockerFile?

  • A container only runs one command; if the Dockerfile has multiple `CMD` the last one wins. You can use `CMD command1 && command2` in shell form, or use an entrypoint wrapper script, or manually `docker-compose run` the migrations; the linked question has examples of all of these. – David Maze Oct 23 '21 at 19:30

1 Answers1

0

try to run:

docker-compose run server sh -c "python manage.py makemigrations"

then

docker-compose run server sh -c "python manage.py migrate"

thepylot
  • 261
  • 4
  • 18