The main problem is that i have two containers: containerA and containerB. containerB is the container of Portia, which is running and i cant stop due to external reasons. And in containerA i need to execute a docker exec of containerB.
I've readen two main solutions, the first one that i already tried and it works is using ssh to run the script in the host but i cant have a user with no password and giving a password seems to me like it's not the best way to do this. The second way is using docker.sock and a docker compose file, but many people said in comments that this is not a secure way.
Can someone explain me other way or if i am wrong and why? Thanks for your time.
You’ve basically highlighted the only two ways to directly run a command in another container. In particular, allowing
docker execaccess gives your process unlimited root-level control over the host, and any security issue in your setup opens the very real possibility of compromising the host (I have seen many SO questions with trivial shell-injection attacks onsystem("docker exec $COMMAND")type calls).Best practice is to try to avoid
docker execas much as possible. It is a very helpful debugging tool, but it should not be in your core application flow at all. (It is very much the equivalent of “ssh as root to the server and...”, which is never a best practice.). If one container needs to request that another container does something, this is typically done via some sort of HTTP interface.