Skip to Content
CoursesLearn DockerUnderstanding Volume Mounting

Running Docker Containers With A Shared Host File System (Docker Volumes Introduction)

In this lecture you’re going to see how easy it is to map files into the container from your host filesystem.

Docker-Performance on a Mac

On Macs the performance of file shares is pretty low. If you have multiple thousand files to share, it can deteriorate and be 60x slower than WSL on windows or native Linux.

If you experience this, there is a new tool which you can try: http://docker-sync.io 

Furthermore, there is a great overview about this issue on this blog, which also recommends looking into VirtioFS: https://www.proudcommerce.com/devops/docker-performance-on-apple-macbook-pro-with-m1-max-processor-status-and-tips 

Video Walkthrough

View the Full Course Now 

Steps and Explanations

Let’s share some files from our host to our docker container. This is done with the ā€œ-vā€ flag and then ā€œhost-directory:container-directoryā€ and is directly mapped into the container.

docker run --rm -v ${PWD}:/myvol ubuntu /bin/bash -c "ls -lha > /myvol/myfile.txt"
  • This command runs ā€œls -lhaā€ and pipes the output to /myvol/myfile.txt
  • /myvol is ā€œmountedā€ to the current working directory on the host
  • On the host you can observe a new file called ā€œmyfile.txtā€ with the output of ā€œls -lhaā€
  • The container is then ended and removed because of ā€œā€”rmā€

Using RAR without installing anything through Docker

From Docker-Hub I get an image that contains the rar tool. It’s minimal, but it demonstrates what can be done with separated environments:

https://hub.docker.com/r/klutchell/rar/dockerfile 

docker run --rm klutchell/rar
  • This will download the image klutchell/rar
  • Execute it
  • In the Dockerfile  you can see the ā€œentrypointā€ is already the ā€œrarā€ executable
    • This means, upon execution of ā€œdocker runā€ it starts the rar process.
    • All you must do is work with it like you work with ā€œrarā€ itself

How does RAR work?

It needs flags as commands, for example:

  • a command will add to an archive.
  • If we mount the right directory, we can simply compress files from the command line:
docker run --rm -v ${PWD}:/files klutchell/rar a /files/myrar.rar /files/myfile.txt
  • It creates a new compressed rar file called ā€œmyrar.rarā€ inside the hosts working directory
  • It’s fully compliant to rar files

Understanding the ā€œWorking Directoryā€

As you see above, we need to give the container the full path (/files/...). By specifying a working directory, you can instruct docker to basically start inside that directory (like cd’ing inside):

docker run --rm -v ${PWD}:/files -w /files klutchell/rar a myrar.rar myfile.txt
  • The ā€œ-w /filesā€ flag sets the working directory to /files.
  • It’s like ā€œcd /filesā€ inside the container and we can skip the full path to the files we want to compress
Last updated on