Search code examples
docker

How to run 2 commands with docker exec


I need to run 2 commands with docker exec. I am copying a file out of the docker container and don't want to have to deal with credentials to use something like ssh. This command copies a file:

sudo docker exec boring_hawking tar -cv /var/log/file.log | tar -x

But it creates a subdirectory var/log, I want to avoid that so if I could do these in the docker container I should be good:

cd /var/log ; tar -cv ./file.log

How can I make docker exec run 2 commands?


Solution

  • This led to the answer: Escape character in Docker command line I ended up doing this:

    sudo docker exec boring_hawking \
        bash -c 'cd /var/log ; tar -cv ./file.log' \
        | tar -x
    

    So it works by, sort of, running the one bash command with a parameter that is the 2 commands I want to run.