Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute host commands from within a docker container

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:

  • 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.
like image 302
rgareth Avatar asked Jul 30 '15 10:07

rgareth


People also ask

Can docker container run commands on host?

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.

How do I run a command inside a docker container?

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.

How do I connect to docker host from inside a docker container?

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.

How do I connect to host from inside container?

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.


Video Answer


1 Answers

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

like image 152
AnandKumar Patel Avatar answered Sep 27 '22 21:09

AnandKumar Patel