Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run commands on host from container command prompt

Tags:

docker

I use portainer to manage containers and it works great.

https://portainer.io/

But when I connect to console, I get the command prompt of container. Is there any way to run simple commands like ls /home/ that will list the files on host?

In other words is there any image that will mount the file system of host server "as-is"?

like image 386
shantanuo Avatar asked May 04 '18 11:05

shantanuo


People also ask

Can docker container Run command on host?

If you are not worried about security and you're simply looking to start a docker container on the host from within another docker container like the OP, you can share the docker server running on the host with the docker container by sharing it's listen socket.

How do I run a command inside a 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 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.

Which command you can execute commands on running container?

Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container. Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.


2 Answers

Here's an example using docker command line:

$ docker run --rm -it -v ~/Desktop:/Desktop alpine:latest /bin/sh
/ # ls /Desktop/

You can extend the approach to as far as you need to. Experiment with it. Learn about the different mount options.

I know the Docker app on MacOS provides a way for default volume mounts. Portainer also claims to provide a volume management screen, am yet to use it.

Hope this helps.

like image 154
Timir Avatar answered Oct 20 '22 16:10

Timir


If you're dealing with services, or an existing, running container, you can in most cases access the shell directly. Let's say you have a container called "meow". You can run:

docker exec -it meow bash

and it will drop you into the bash shell. You'll actually need to know if bash is installed, or try calling sh instead.

The "i" option indicates it should be interactive, and the "t" option indicates it should emulate a TTY terminal. When you're done, you can hit Ctrl+D to exit out of the container.

like image 42
Dockstar Avatar answered Oct 20 '22 16:10

Dockstar