简体   繁体   中英

Run commands on host from container command prompt

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"?

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.

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.

First of all : You never ever want to do so.

Volumes mounted to containers are used to persist the container's data as containers are designed to be volatile -(the container itself shouldn't persist it s state so restarting the container n number of times should result in the same container state each time it starts)- so think of the volume as a the database where all the data (state of the container) should be stored.

Seeing volumes this way makes it easier to decide against sharing the host's entire file system, as this container would have read write permissions over the host OS files itself which is a huge security threat .

Sharing volumes across containers is considered a bad container architecture let alone sharing the entirety of the host file system.

I would propose simple ssh (or remote desktop) to your host if you require access to it to run commands or tasks on your host.

OR if your container requires access to a specific folder for some reason then you should consider mounting or binding that folder to the container

docker run -d --name devtest --mount source=myvol2,target=/app nginx:latest

I would recommend copying the content of that folder into a docker managed volume (a folder under the docker/volumes tree) and binding the container to this volume instead of the original folder to minimize the impact of your container on your host's OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM