No cp or git command is executed on docker-compose

This is my docker-compose.yml:

version: "3.9"
services: 
    app:
        image: company/image
        ports:
            - "5000:5000"
        working_dir: /path/to/git/repository
        command: sh -c "git reset --hard origin/main"
        command: git reset --hard origin/main
        command: cp /src/module/menu.json /src/menu.json
        command: sh -c "cp /src/module/menu.json /src/menu.json"
        command: dummy
        command: sh -c "dummy"
        volumes: 
            - /path/on/local:/path/on/docker
        command: sh -c "echo 'hello from inside docker'"

When I run docker-compose up, I only see this:

Recreating module_app_1 ... done
Attaching to module_app_1
app_1 | hello from inside docker
module_app_1 exited with code 0

And I can inspect the container using docker-compose exec app and I can see that none of the cp and git commands are executed and I also see no error at all in the output.

Also dummy is apparently a command that does not exist on this container. Why do I see no errors for that?

What should I do?

Upvotes: 1

Views: 251

Answers (1)

ErikMD
ErikMD

Reputation: 14723

none of the cp and git commands are executed and I also see no error at all in the output.

Yes, because you added multiple command: lines in the docker-compose.yml, while this property is intended to specify a single command (just to recall, a Docker container is intended to run just one program).

As a result, you only see the outcome of the last command: line, which overwrites the previous ones:

app_1 | hello from inside docker

Still, you can do the following to address your use case (even if it may be not very idiomatic):

version: "3.9"
services: 
    app:
        image: company/image
        ports:
            - "5000:5000"
        working_dir: /path/to/git/repository
        command: |
          sh -e -c "
            git reset --hard origin/main
            cp /src/module/menu.json /src/menu.json
            echo 'hello from inside docker'
          "
        volumes: 
            - /path/on/local:/path/on/docker

This answer specifically relies on the so-called block style of YAML, and the shell option -e is recommended to exit immediately if one of the shell commands fails.

Upvotes: 1

Related Questions