2

I have my docker-compose.yaml file like this:

version: "3"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"
      sh -c "python simple_script.py"
      

and the problem is that when i run docker-compose up it never reaches the second command ( sh -c "python simple_script.py" ) .

I think this is because the first command sh -c "python manage.py runserver 0.0.0.0:8000" never exits.

Is there a way to run two commands like this?

Casey Schneider
  • 885
  • 6
  • 13
  • 3
    Run the non-blocking code first, or use two containers, or if both are daemons use a process manager like supervisord. Or run the 2nd command with `docker exec`... – Klaus D. Jan 31 '22 at 05:04
  • A container only runs one command, but you can have multiple containers running on the same image. Since in fact the Django server won't exit, if you want to run a second command while that's running you'll need an approach like this. – David Maze Jan 31 '22 at 11:21

2 Answers2

4

can you try this one:

sh -c "python manage.py runserver 0.0.0.0:8000 & python simple_script.py"  

In linux you can use & to run commands in background.
You can use fg to get back to it.

Mojtaba Arezoomand
  • 2,140
  • 8
  • 23
  • 2
    This may not be what you're after: when `simple_script.py` completes, the container will exit, and if the Django server fails for some reason, Docker simply won't notice it. It's typically better to run multiple containers with one process per container. – David Maze Jan 31 '22 at 11:23
  • The best practice is to run them in multiple containers, but question was something else. @DavidMaze – Mojtaba Arezoomand Jan 31 '22 at 12:12
-2

You can write two commands in one line. like this -

sh -c "python manage.py runserver 0.0.0.0:8000 && python simple_script.py"
Saikat Roy
  • 502
  • 8
  • 20