docker compose | cant run two command using '&&' in command property

833 views Asked by At

I have a docker compose file and in the definition of one of my containers I try to run multiple commands, but when I run two commands one after another, the other command doesn't execute. I tried changing the order between these two commands, and still every time the second command after the && does not run. Does anyone have any idea what this could possibly be?

This is the docker container defenetion in the docker compose file:

  django:
    container_name: django
    restart: unless-stopped
    image: server:${SERVER_IMAGE_TAG}
    hostname: django
    command: /bin/bash -c "/tmp/startup.sh && echo 'export DJANGO_SETTINGS_MODULE=core.settings.dev' >> /etc/apache2/envvars"
    ports:
      - "4443:443"
      - "8080:80"
    depends_on:
      - rabbitmq
      - redis

and a closer look at the command:

command: /bin/bash -c "/tmp/startup.sh && echo 'export DJANGO_SETTINGS_MODULE=core.settings.dev' >> /etc/apache2/envvars"

it is important to say that both of the command works fine

1

There are 1 answers

1
Nikolaj Š. On

If /tmp/startup.sh exits with non-zero status (as found out by digby280), bash will not run second command after &&: see List Constructs: Chaining for details.

Replace && with ; in your bash -c "..." command:

$ false
# `false` returns non-zero exit status
$ echo $?
1
$ false && echo "Won't run"
$ false ; echo "Will run!"
Will run!