Execute two commands with docker exec

I’m trying to do two commands in docker exec. Concretely, I have to run a command inside a specific directory.
I tried this, butit didn’t work:

docker exec [id] -c 'cd /var/www/project && composer install'

Parameter -c is not detected.
I also tried this:

  • How to start Docker container only after another container is exited
  • Running composer install in docker container
  • Using Docker, how do you execute PHP composer commands?
  • Installing composer in a docker image
  • Why does not work docker command in MacOS?
  • composer install fails when unable to see mysql database
  • docker exec [id] cd /var/www/project && composer install
    

    But the command composer install is executed after the docker exec command.
    How can I do it?

  • Unable to start jenkins auctomatically inside docker container
  • How to convert a Vagrant box to a Docker image
  • SymmetricDS Postgres target gives “Failed to read table” for all sym_* tables
  • How can I map a shared folder from my container on Mac?
  • Deploy nginx official image, the volumn mount incorrectly
  • What is the proper way of setting a mongodb replica set using docker and fig?
  • 3 Solutions collect form web for “Execute two commands with docker exec”

    In your first example, you are giving the -c flag to docker exec. That’s an easy answer: docker exec does not have a -c flag.

    In your second example, your shell is parsing this into two commands before Docker even sees it. It is equivalent to this:

    if docker exec [id] cd /var/www/project
    then
        composer install
    fi
    

    First, the docker exec is run, and if it exits 0 (success), composer install will try to run locally, outside of Docker.

    What you need to do is pass both commands in as a single argument to docker exec using a string. Then they will not be interpreted by a shell until already inside the container.

    docker exec [id] "cd /var/www/project && composer install"
    

    However, as you noted in the comments, this also does not work. That’s because cd is a shell builtin, and doesn’t exist on its own. Trying to execute it as the initial command will fail. So the next step is to hand this off to a shell to execute.

    docker exec [id] "bash -c 'cd /var/www/project && composer install'"
    

    And finally, at this point the && has moved into an inner set of quote marks, so we don’t really need the quotes around the bash command… you can drop them if you prefer.

    docker exec [id] bash -c 'cd /var/www/project && composer install'
    

    Everything after the container id is the command to run, so in the first example -c isn’t an option to exec, but a command docker tries to run and fails since that command doesn’t exist.

    Most likely you found this syntax from a docker run command where the entrypoint was set to /bin/sh. However, exec bypasses the entrypoint, so you need to include the full command to run. As others have pointed out, that command includes a shell like bash or in the below example, sh:

    docker exec [id] /bin/sh -c 'cd /var/www/project && composer install'
    

    as Nehal J Wani said in his commentary, the correct syntax is the following:

    docker exec [id] /bin/bash -c 'cd /var/www/project && composer install'
    

    many thanks!

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.