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. These make
commands just execute a series of docker-compose
commands and are needed sometimes in dev.
The two possible ways in I can think of are:
You can directly access the bash of the Docker Container and execute commands there. It's very easy to launch the bash of the Container and you can do so using this command.
Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.
Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.
Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With