1

I am setting up docker with the Django application but the bash command is not working with docker Ubuntu:20.04 and docker-compose version 3.5.

My docker version is Docker version 20.10.7, build f0df350 and docker-compose version is Docker Compose version 2.0.0-beta.4

Can anyone help me to resolve the issue?

Below are my docker files:

Dockerfile:

FROM ubuntu:20.04

ENV PYTHONUNBUFFERED 1

RUN apt-get -y update
RUN apt-get install -y --no-install-recommends default-libmysqlclient-dev
RUN apt-get install -y gcc git libc-dev python3-dev python3-pip
RUN ln -s /usr/bin/python3 /usr/bin/python

RUN mkdir /app
WORKDIR /app
ADD . /app

RUN pip install --upgrade pip && pip install -r requirements.txt

EXPOSE 8000

ENTRYPOINT [ "/app/manage.py" ]

docker-compose.yml

version: '3.5'
services:
  db:
    image: mysql:5.6
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: "mydb"
      MYSQL_ROOT_PASSWORD: "root"
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    networks:
      default:
        aliases:
          - app-db

  
  django:
    build: .
    command: bash -c "while true; do runserver 0.0.0.0:8000; sleep 10; done"
    stdin_open: true
    tty: true
    volumes:
      - .:/app
    depends_on:
      - db
    ports:
      - "8000:8000"
    restart: always
    environment:
      MYSQL_DATABASE: "mydb"
      MYSQL_USER: "root"
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_HOST: "app-db"

volumes:
    mysql_data: {}

I am getting an error on command while running docker-compose up --build:

bash -c "while true; do runserver 0.0.0.0:8000; sleep 10; done"

Error: Unknown command: 'bash'

Thanks in advance

1 Answers1

1

When you run the container, the ENTRYPOINT and CMD are combined together into a single command; it doesn't matter if the command part is overridden by Docker Compose, the container still runs only a single command this way. So in effect you're asking for the main container process to be

/app/manage.py bash -c "while true; do runserver 0.0.0.0:8000; sleep 10; done"

and the complaint is that the Django runner doesn't understand manage.py bash as a subcommand.

In your Dockerfile itself, you probably want the default command to be to launch the server. Having ENTRYPOINT as an arbitrary "half of the command" tends to be a little more confusing, and leads to needing to override that too; it's probably better to just put this as the standard container CMD.

# No ENTRYPOINT, but
CMD ["/app/manage.py", "runserver", "0.0.0.0:8000"]

You don't need to put the restart loop into the container command since Docker already allows you to specify a restart policy for containers. You should be able to trim the docker-compose.yml section down to:

django:
  build: .
  # command: is built into the image
  # don't usually need stdin_open: or tty:
  # don't overwrite the image code with volumes:
  depends_on:
    - db
  ports:
    - "8000:8000"
  restart: always  # replaces the "while true ... done" shell loop
  environment: *as-in-the-question
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thank you so much for the detailed explanation. It helped me a lot to understand the issue. The error is gone but it didn't solve the issue for which I wanted to run bash script. Can you please tell me how to wait for the `db` container to complete before `django` container? – Rabia Iftikhar Jun 30 '21 at 12:10
  • As you mentioned to include the command in the `restart`. It doesn't work. The error is: `Error response from daemon: invalid restart policy 'bash -c "while true; do python manage.py runserver 0.0.0.0'` – Rabia Iftikhar Jun 30 '21 at 12:12
  • No, when you say `restart: always` then Docker will restart the container for you if it fails (maybe on startup); you don't need a while loop to do it. Just having the container crash and restart if the database isn't ready yet is fine (this is how things usually work in Kubernetes), or see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). – David Maze Jun 30 '21 at 13:05
  • Thank you so much for the clarification. Restarting the container works fine but I have also fixed it using the Django management command for now. Thank you for the help :) – Rabia Iftikhar Jun 30 '21 at 14:53