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"
...

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"
...
Got an article suggestion? Let us know
BY NC SA

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

Explore more