Host Directory Volume Mounting in Docker

Part 1 was all about Running a single docker container explained step by step.

In Part 2 we were Running multiple Docker containers side by side and observed the Data separation.

In this part we are going to step into volume mounting in Docker. This part is about Understanding Host Directory Volume Mounting in Docker Step by Step.

Prerequisites

Again, make sure you installed “Docker Desktop” and you have the Docker Daemon running. Then open a PowerShell on Windows, a Terminal on Mac/Linux and follow along! This is fun.

Docker Run Video Walkthrough

You can also follow along in this video, if you prefer. You can also get this as a full Docker Video QuickStart Course

Link to YouTube

Running docker containers with a shared host file system (a.k.a. docker volumes)

Let’s share some files from our host to our docker container. This is done with the “-v” flag and then “host-directory:client-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” from inside your container.

The container is then ended and removed because of “ — rm” inside the container, but written to the host. From the 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 and 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

  • “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. Shows that a file can be compressed in a docker container where the file itself is hosted outside on the host. Let’s try something else: Let’s set the Working Directory. This was you don’t need to specify a full path anymore:

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 Shows how a -w working-directory flag can be added to the command. That’s the end of the tutorial. If you like this, then also check out the video course Understanding Docker and Docker-Compose — 100% Hands-On**. **It’s a 2.5h Quickstart into Docker.

If you want more, then follow me on Medium, there will be a new tutorial frequently. Also, checkout my blog where I have tons of Beginner Tutorials about about Blockchain, Solidity, Ethereum, Docker and other Technical Topics.

In the next part you are going to apply Docker Volumes to a real-world scenarios where you share a source-file from your host through a docker container.

Thanks!