Capability to establish Docker container volumes and implement bind mounts on the Ubuntu operating system.


Abstract.
We will be able to comprehend how Docker offers the capacity to construct and manage data volumes, which is a preferred method for storing data generated by and used in Docker containers. Bind mounts provide an alternative by directly mapping a host machine's directory or file into a container, whereas volumes are managed by Docker and live independently of the container's lifecycle. The ability to build and use volumes in combination with bind mount capability is the main focus of this blog.
Table of Contents.
Introduction.
Prerequisite.
Create a Docker Container Image and Docker Volume.
Build a Container Base Image in Docker.
Create a Docker Bind Mount.
Conclusion.
INTRODUCTION.
A method for generating volumes by directly mapping a directory or file from the host computer's file system into a Docker container is offered by Docker bind mounts. This creates a direct connection between the container and the host, enabling real-time data synchronization that can be both readable and writable by different engineering personnel at different locations.
In addition, both Docker volumes and bind mounts provide a way to manage data in containers; they have some differences:
i. Docker volumes are maintained by Docker and are normally stored in a specific directory on the host computer, but a Bind mount refers to a directory on the host machine and mounts it directly into the container at a defined path.
ii. Bind mounts are strongly tied to the host file system and lack the capabilities and flexibility of Docker volumes, which are independent of the container's lifecycle and may be created, maintained, and shared across numerous containers.
The main characteristics of Docker bind mounts are as follows:
Direct Host-Container Mapping:
A specific path on the host machine is linked directly to a path within the container.
Real-time Synchronization:
Changes made to the mounted directory or file on either the host or within the container are immediately reflected in both locations.
Host Dependency:
Bind mounts are dependent on the host machine's directory structure and operating system, meaning they are less portable than Docker-managed volumes.
Use Cases:
Ideal for development environments where immediate code changes need to be reflected in a running container, or when a container needs to access existing data on the host.
PREREQUISITE.
Let's get started with our blog project. Some tools are necessary for this project, and they are as follows:
A laptop computer running Ubuntu (the instructions are based on Ubuntu 22.04 LTS).
Install the Docker Hub and Docker Desktop or use Docker Web on your laptop system.
Get familiar with the Linux syntax as we go along this project blog.
CREATE A DOCKER CONTAINER IMAGE AND A DOCKER VOLUME.
The recommended method for storing data created and utilized by Docker containers is to use Docker volumes. They offer a means of storing information outside of the writable layer of the container, guaranteeing data stability even if the container is deleted or rebuilt. Using the
docker volume create
command and giving the volume a name is the most popular method for creating a Docker volume. As a result, a named volume is created that several containers may readily refer to and utilize. When you create a volume, it's stored within a directory on the Docker host. When you mount the volume into a container, this directory is what's mounted into the container. Also note that Volumes work on both Linux and Windows containers.Now, let’s make sure we have installed Docker on Ubuntu Linux OS by setting up Docker’s
apt
repository on a new Ubuntu machine. Install the Docker packages and verify with the output as below.Note that “Hello world” is the official image for Docker, as shown above, after you have installed the Docker repository. Now, you need to update our package still on the Ubuntu environment with the command
apt update
to ensure you have the latest information on the newest versions of packages and their dependencies. Additionally, ensure you are logged in as the root user, as shown below.Now you will enter the default directory,
cd /var/lib/docker
, and enter the directoryls
and view the default files within Docker. This is where the containers, volumes, etc, will be saved.Now you need to create a directory where Docker will be housed by using
mkdir <directory name>
. and vim into the Dockerfile, where we will borrowFROM
ubuntu (its base image or parent image) and take note of the command used before exiting the vim page with theESC
, :wq Enter.Next is to build the image, targeting the image as <
-t
\> with a full stop <.
\> as shown below.Furthermore, you need to log in to your Docker account on the Ubuntu environment; before that, you should create a repository on your Docker Hub account and click create as shown below.
Give your repository a name and make it public so others can view it.
Now, confirm you can log in to your Docker Hub with your Docker username and password login details via the Ubuntu environment, as shown below.
Next, you can view your container parent/image and all information you added via the vim stage.
Now,tag your Docker image with the latest version and push it to the repository you created one Docker Hub, as shown below.
To confirm your image has been pushed to the DockerHub repository, just navigate to the Docker Hub and refresh the page, and the output is as shown below.
To spin up a container with port mapping with the image earlier created is as
docker run -d —name <container name> -p <port name> image name
, and to list all in the container,docker ps -a
as shown below.Now, to inspect the container and view all content in it,
docker inspect <name of container>
The next step is to establish a Docker volume, which is kept in a directory on the Docker host. When you mount a volume into a container, this directory is mounted. Make sure you are at the volume path, then create
docker volume create <volume name>
,You need to attach or mount the volume to the container,
docker run -d —mount source=<volume name>,target=/app new base image
. Be aware that the new image (a web server) will attempt to pull automatically before it generates and mounts the volume to the container.Next, is to view the content of the container volume by going out of the path, as shown below
Furthermore, to confirm if the volume has been mounted to the container, you input
docker inspect <container ID>
as shown below.And to view the container volume briefly,
docker volume inspect <volume name>
as shown below.BUILD A CONTAINER BASE IMAGE IN DOCKER.
In this phase, we will create a Docker image within a container. Docker images are used to serve as templates for Docker containers. More specific container images are constructed using Docker base images as the base layer. They usually include the components required to run a program, such as a basic operating system or a particular runtime environment. Examples include MySQL (database images that offer a pre-configured database instance) and Nginx (a well-known web server frequently used as a platform for providing static content or as a reverse proxy), among others. Now, we are using the nginx base image to build a Docker image within the container as follows: '
docker run -d —name nginx
'. Docker will start pulling the base image from its library, as shown below.Now we can view the content of the container as
docker ps -a
also note the container ID will shorten as it displays on your file path.You can now inspect the container with the ID or container name, docker inspect
<container_name> or <container_ID>
, as shown below.You can check logs with
docker logs <docker_name>
as shown below.To inspect the created base image, by inputting
docker inspect <base image_name>
. You will see the nginx base image, version with its repository and label being run in the process.As the picture above shows, we use the nginx base image, but we may also create our image using a Dockerfile. Similar to source code, Dockerfiles are plain text files that can be version-controlled. This makes it simple to keep track of modifications, roll back to earlier iterations, and manage various setups. By setting up a text file with the command
vim dockerfile
and building our image withdocker build -t <image_name> .
as shown below, the base image output.Now we can view our base image with the command
docker inspect builtdocker
as shown below.Use the command
docker volume create <volume_name>
to create a volume. This will install the volume locally on your computer. As you can see from the Docker volume creation above, multiple volumes can be created at once accordingly.CREATE A DOCKER BIND MOUNT.
Bind mounts' primary use is to be extremely helpful for DevOps engineers. Any modifications made to the host machine's code are instantly mirrored in the container that is currently operating when a local project directory is mounted into it. By removing the need to rebuild and restart containers after every code change, this significantly expedites the development process and makes features like hot reloading possible. To start with, you create a directory
mkdir <directory_name>
, with an input content astouch <txt_content>
, display output asecho <display_content>
, now run the container volume and bind the content asdocker run -d —mount type=mount, source/=root/<directory_name>/,target=/app/data <base image_name>:latest
as shown below.You can view the latest container with its image with
docker ps
as shown below.Now that the container has been built and mount-bound, you can use the command
docker exec -it /bin/sh
andls
to view inside the container as indicated below.You can verify the welcomememo.txt file in your container, which was previously entered into your local computer. You can also make other changes to your container to observe the same effect on your local computer. (Note: the container is the red arrow, and the local system is the yellow arrow.)
Additionally, as demonstrated below, you can make another update from your local computer, and the same changes will be made to your container simultaneously. (Note: the container is the red arrow, and the local system is the yellow arrow.)
CONCLUSION.
This project demonstrates how bind mounts, which offer direct access to the host disk, are highly beneficial for development processes. Because they allow code changes to be reflected instantly within an active container, they eliminate the need for frequent image rebuilds and container restarts during development. This direct mapping enables seamless interaction with source code management and host-based development tools.
However, bind mounts are less suitable for production environments or scenarios requiring robust data management and high mobility. Because of their reliance on the host computer's directory structure and potential security problems resulting from direct host filesystem exposure, they are less appropriate for scalable and safe production deployments.Many thanks once again for going through my blog.
Subscribe to my newsletter
Read articles from Gabriel Aboiraor directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
