How to run shell script on host from docker container?

Use a named pipe.
On the host OS, create a script to loop and read commands, and then you call eval on that.

Have the docker container read to that named pipe.

To be able to access the pipe, you need to mount it via a volume.

This is similar to the SSH mechanism (or a similar socket-based method), but restricts you properly to the host device, which is probably better. Plus you don’t have to be passing around authentication information.

My only warning is to be cautious about why you are doing this. It’s totally something to do if you want to create a method to self-upgrade with user input or whatever, but you probably don’t want to call a command to get some config data, as the proper way would be to pass that in as args/volume into docker. Also, be cautious about the fact that you are evaling, so just give the permission model a thought.

Some of the other answers such as running a script. Under a volume won’t work generically since they won’t have access to the full system resources, but it might be more appropriate depending on your usage.

Leave a Comment