How to Mount a Directory Inside a Docker Container | by Mahbub Zaman | Towards Data Science

It works on my machine, let’s ship your machine then! That’s Docker in a nutshell. It allows us to run an application from a Docker image on any computer by using containerization technology. A Docker container is a package of a Base OS (alpine, ubuntu, etc.) and other necessary software dependencies, which you can define inside a docker image. Now you can use that image to create a container to run your application on different machines or even on the cloud platforms (AWS, GCP, Azure, etc.) without worrying about environment management (development, testing, and production).
Docker is one of the popular tools for DevOps for its portability, security, and performance. From this post, you’ll learn some basic concepts and commands of Docker that will teach you how to mount a directory inside a Docker container. I use Docker almost every day for local development — for example, you can see how I dockerized a Ruby on Rails application for a personal project, here.
First, you will need to install Docker. For demonstration purposes, we will use the pre-built official image of Ruby and macOS.
Docker Image: It’s a template containing instructions to create containers.
Docker Container: A Container is the running instance of an image.
The docker run command is used to run a container from an image. Here we are using the ruby image to create the container, but you can use your own image. If the ruby image does not exist on the host machine, Docker will pull that from Docker Hub. Once downloaded on your local machine, Docker uses the same image for consecutive container creation. To mount a volume, just run this line in your terminal
docker run -v /host/directory:/container/directory
Now you’re going to mount the scripts folder inside the scripts folder of the container. This is the best practice because you don’t have to worry about environment management on different machines.
docker run -it --rm -v $HOME/Desktop/scripts:/scripts --name scripts ruby bash
The flags -i -t (often written as -it) is used to access the container in an interactive mode. The --rm flag (optional) removes the container when you exit or stop it to free system resources (CPU, memory). If you don’t use that after you stop or exit the container, it has to be manually removed if you want to free disk space. The -v flag is used to mount a host folder, and it consists of two fields separated by a colon. The first part is the path in the host machine. The second part is the path in the container.
-v /host/directory:/container/directory
The--name flag (optional) is used to give the container a specific name. If you don’t want to provide one, Docker will randomly assign a name, so you may want to choose one specifically if you want to keep track of your containers more easily. The ruby command is used to load the image. If you want to use the 2.5 version of Ruby specifically, use the ruby:2.5 command.
docker run -it --rm -v $HOME/Desktop/scripts:/scripts --name scripts ruby:2.5 bash
Last but not least, the bash command is used to get a bash shell inside the container.
To run Python code, use the python image.
docker run -it --rm -v $HOME/Desktop/scripts:/scripts --name scripts python bash
As I explained above, if you want to free system resources and disk space, you need to stop and remove containers. You can run docker ps command on the host machine to see a list of your running containers. To stop a container, use the docker stop command with either the container id or container name. You can use the -a flag to see all the stopped or exited containers.
docker stop d61f09eb42ad# ordocker stop scripts
To manually remove a container, use one of the following commands.
docker rm d61f09eb42ad# ordocker rm scripts
In the case that you want to remove an image to free the disk space, for example, the Ruby image you just created, use the docker rmi command. But first, let’s try to understand what is going on under the hood. For that, we will use the docker images and docker system df commands to display information regarding disk space usage by the Docker daemon. The docker images command lists all the images you have in your machine.
The docker system df command tells me that I can only claim 65% of the disk space because I’m still running the scripts container, and that container is using the ruby image. If you add the remaining two images, you’ll get the 65% reclaimable disk space.
933MB (python) + 643MB (java) = 1.576GB (65%)
Alright, by running the docker system df command after stopping the container tells us that we can now claim 100% of disk space.
Be aware if you try to remove an image that's being used by a container, you’ll get the following error.
Error response from daemon: conflict: unable to delete 121862ceb25f (cannot be forced) — image is being used by running container d61f09eb42ad
Finally, we can use the docker rmi command with the image id to remove an image.
docker rmi 121862ceb25f# remove multiple images by putting a space between themdocker rmi efdecc2e377a d23bdf5b1b1b
Now you know how to mount a directory inside a docker container. Docker helps me immensely to create local development environments easily on multiple machines without any extra effort and time. It’s a skill you want to keep in your bag for both personal and industrial purposes. I hope this will help you to get started with the magical world of Docker.