Execute host commands from within a docker container
Although it might not be best practice it is still possible to control the host from inside a container. If you are running docker-compose commands you can bind mount the docker socket by using -v /var/run/docker.sock:/var/run/docker.sock
on ubuntu.
If you want to use other system tools you will have to bind mount all required volumes using -v
this gets really tricky and tedious when you want to use system bins that use /lib.*.so
files.
If you need to use sudo
commands don't forget to add --privileged
flag when running the container
![rgareth](https://i.stack.imgur.com/t4Snl.jpg?s=256&g=1)
rgareth
Updated on June 05, 2022Comments
-
rgareth about 2 years
I'm looking for a way for a user to be able to execute a limited set of commands on the host, while only accessing it from containers/browser. The goal is to prevent the need for SSH'ing to the host just to run commands occasionally like
make start
,make stop
, etc. Thesemake
commands just execute a series ofdocker-compose
commands and are needed sometimes in dev.The two possible ways in I can think of are:
- Via cloud9 terminal inside browser (we'll already be using it). By default this terminal only accesses the container itself of course.
- Via a custom mini webapp (e.g. node.js/express) with buttons that map to commands. This would be easy to do if running on the host itself, but I want to keep all code like this as containers.
-
rgareth almost 9 yearsThank you for the clarifications that accessing host processes is against docker methodology. I guess then the answer is that I need a non-docker process (e.g. webserver) that runs directly on the host instead of instead of inside a container.