BraveAdmin
BraveAdmin

Reputation: 773

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:

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?

Upvotes: 25

Views: 32832

Answers (5)

Alex Konkin
Alex Konkin

Reputation: 736

I would like to add my example here because it is a bit more complex then the ones thate were shown above. This example also illustrates on how to find a container id that should be used in the docker exec command.

I needed to execute a composite docker exec command against docker container over ssh. I managed to achieve this in 2 steps:

-definition of the variable that contains a command expored as an environment variable

-ssh command that runs it

  1. environment varialbe definition:

    export COMMAND="bash -c 'php bin/console --version && composer --version'"

  2. ssh command that runs on a remote system:

ssh -t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i keyfile.pem [email protected] 'docker exec docker ps|grep php|grep api|grep -v cron|awk '"'"'{print $1}'"'"' '$COMMAND

As you can see I left the command out of the single quotes to pass its actual value to the SSH process

The output of the command execution is:

Warning: Permanently added '111.222.111.222' (ECDSA) to the list of known hosts.
Cannot load Xdebug - it was already loaded
Symfony 4.3.5 (env: dev, debug: true)
Cannot load Xdebug - it was already loaded
Composer version 1.9.1 2019-11-01 17:20:17
Connection to 111.222.111.222 closed.

If you wish to execute this command in a single line you can use a bit modified version of my first example:

COMMAND="bash -c 'php bin/console --version && composer --version'" ssh -t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i keyfile.pem [email protected] 'docker exec `docker ps|grep php|grep api|grep -v cron|awk '"'"'{print $1}'"'"'` '$COMMAND

Upvotes: 0

wisbucky
wisbucky

Reputation: 37807

The other answers are fine if you want to run 2 arbitrary commands. But if the first command is simply cd, then you should use the -w option to set the working directory instead.

docker exec -w {dir} {container} {commands}

So in your example:

docker exec -w /var/www/project {container} composer install

Upvotes: 2

BMitch
BMitch

Reputation: 263549

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'

Upvotes: 4

Dan Lowe
Dan Lowe

Reputation: 56518

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'

Upvotes: 40

BraveAdmin
BraveAdmin

Reputation: 773

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!

Upvotes: 2

Related Questions