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?

2
  • 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... Commented Jan 31, 2022 at 5: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. Commented Jan 31, 2022 at 11:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
The best practice is to run them in multiple containers, but question was something else. @DavidMaze
-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"

2 Comments

This would still block.
When I run python3 manage.py runserver 0.0.0.0:8000 && python3 simple_script.py the server begins to run, but the simple_script.py does not run until I quit the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.