Sometimes, there is a need to mount storage to a container directly from a Docker host, and this post will show how to do it.
Mounting Storage vs Storage Volume
In previous posts, we saw how to mount a storage volume to a Docker container where the volume was created by Docker and managed by Docker.
We will do something simpler to use an existing directory on the Docker host and mount it to a Docker container. This option is not ideal; however, it will do the job for quick testing or specific applications and scenarios.
In my case, I have a Docker host running on Ubuntu 20.04. Inside the host, I have a directory called host. The following Docker command will create a container and mount the host directory to the container data directory.
docker run -ti -v /host:/data ubuntu:latest /bin/bash
The above command will create a container that runs the latest ubuntu image and mount the host directory inside the container under the data folder. The command also starts a bash shell.