How to Execute Multiple Commands in Docker-Compose?

Better Stack Team
Updated on August 1, 2022

It is possible to define and run multiple commands in the docker-compose.yml file.

To execute multiple commands using Docker-Compose, structure the file in the following way:

...
services:
  app:
    build:
      context: .
    command: >
      sh -c "command1 &&
             command2 &&
             command3"
...
Copied!

You can define multiple commands to be executed by enclosing them in the character and connecting them using && operator.

If you want to for example execute migration in then tart the server in Python Django app, you can do the following:

...
command: >
    bash -c "python manage.py migrate &&
                       python manage.py runserver 0.0.0.0:8000"
...
Copied!
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack