简体   繁体   中英

How can I use the shell from a docker image on files from the host machine?

What command do I have to type to start docker and get a shell that has access to all my files or at least the home files on the host?

For example, I'm on a mac and I want to run ubuntu's grep on a file, or execute a bash script using ubuntu bash

I have created an empty file named hello on my host machine at ~/Desktop/tmp/files/hello

Use the -v flag to specify volume binding and read the documentation for Using volumes . For example:

docker run --name voltest --rm -it -v ~/Desktop/tmp:/my ubuntu bash

Says:

  1. docker run Create a container named "voltest"
  2. --rm delete the container when it exits
  3. -it keep an interactive shell
  4. -v ~/Desktop/tmp:/my Bind a volume on my machine at ~/Desktop/tmp to the container filesystem at /my
  5. ubuntu Create the container using the ubuntu image
  6. bash Run the bash command in the container

Then whenever you run:

find /my

You get:

/my
/my/files
/my/files/hello

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