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.
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
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