1

I have a docker django postgres project.

docker-compose.yml:

version: '3'

services:

  web:
    build: .
    command: bash -c "python manage.py migrate && python manage.py load_patient_data && python manage.py runserver 0.0.0.0:8000"
    ports:
      - 8000:8000
    depends_on:
      - db

  db:
    image : postgres
    restart: always
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"

Dockerfile:

FROM python:3

ENV PYTHONUNBUFFERED 1

WORKDIR /app

ADD . /app

COPY ./requirements.txt /app/requirements.txt

RUN pip install -r requirements.txt

COPY . /app

After I run:

docker-compose up

It returns:

db_1   | 2021-11-23 03:00:08.204 UTC [62] LOG:  database system was shut down at 2021-11-23 03:00:08 UTC
db_1   | 2021-11-23 03:00:08.209 UTC [1] LOG:  database system is ready to accept connections

And will handle on there.

This time if I control+c and run:

 docker-compose up

again, it will work. Or if I go to the docker app and manually start the container, it will also work.

What should I do, to let the container run automatically just by running:

docker-compose up

William
  • 3,724
  • 9
  • 43
  • 76
  • Is the django app dockerfile located in the right folder ? – Eyal Solomon Nov 23 '21 at 07:10
  • As shown in this question: https://stackoverflow.com/questions/37259584/postgres-shuts-down-immediately-when-started-with-docker-compose, postgres will restart itself during the initialization. – minhuw Nov 23 '21 at 12:04
  • And did you check the states of these two containers with `docker container ls`? – minhuw Nov 23 '21 at 12:10
  • Question is not very clear to me, is the postgres container not running? Or do you want to let docker-compose run without Ctrl+c ? – Sukhmeet Sethi Dec 23 '21 at 18:59

1 Answers1

0

I had the same issue but with a ENTRYPOINT [ "powershell" ] at end of my DockerFile, but I think it is the same with no entry point, and I found out that docker compose exits immediately "terminal" (this is the same with cmd for instance or bash in Linux) and then container exits, to avoid this, use tty: true and/or stdin_open: true option, for instance with a minimalist docker-compose.yml:

services:
  myservice:
    image: myservice:1.0
    container_name:
      "myservice"
    tty: true        # <- this
    stdin_open: true # <- and/or this
gluttony
  • 402
  • 6
  • 14