Trevor Murphy
Trevor Murphy

Reputation: 161

Executing bash then running commands in docker

You can execute a bash shell in a docker container by using

sudo docker exec -it container bash

But I want a command that executes a bash shell in the container and then executes more commands in the bash prompt.

A command like this currently works:

sudo docker exec -it container touch test.txt | bash

However, the command I want to run in the bash prompt is only available from within the docker container, so I get an error:

No such file or directory

Is it possible to execute a command that is local to the docker container using docker exec?

Upvotes: 6

Views: 6848

Answers (2)

Matthew
Matthew

Reputation: 427

Try something like this:

sudo docker exec -it container bash -c "touch test.txt ; <another command>"

Upvotes: 2

Trevor Murphy
Trevor Murphy

Reputation: 161

I suppose I should've read the docker docs more!

docker exec -it container mycommand

works fine! For some reason I was thinking I had to initiate a bash prompt then use other commands, my mistake.

Thanks jrbeverly and user2105103 for the explanation that "EVERY command executed via docker exec is inside the container."

In case someone is curious how to run multiple commands in a container, I'm using:

sudo docker exec -it container sh -c "com1 && com2 && com3"

Upvotes: 4

Related Questions