Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute multiple commands in a Docker container and leave the shell session open?

Tags:

docker

I have a running Docker container and need to start and interactive shell session inside the container and execute multiple commands inside before this. How can I do that?

I tried

docker exec -it foo bash < my_commands_to_exexute_inside_the_container.sh

and

docker run foo bash -c "echo Foo;echo Bar"

But none of this works. The first one does not execute the commands from the script and the second one does this, but closes the terminal session.

Any idea? Btw: The commands should come from outside the container. There is not file in the container with those commands.

Edit:

The following does almost exactly what I want it. The only catch is that the first command closes the shell and the second command opens it again. This causes me to lose environment variables.

docker exec -i foo bash < "cli.sh"
docker exec -it foo bash

Is there any way to achieve this behavior without closing the shell in between?

like image 540
DarkMath Avatar asked Oct 26 '25 14:10

DarkMath


1 Answers

What about this?

docker run -it alpine sh -c "pwd;date;sh"
like image 63
Ladich Avatar answered Oct 29 '25 04:10

Ladich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!