Understanding volume instructions in Docker
In Docker, we can create separate images, containers, and volumes. Whenever we start a container, we can mount the volume.
If we can do it manually in Docker, we can automate it through a build process using a file called Dockerfile.
This article will discuss building, creating, and mounting volumes in Docker via a Dockerfile.
Understanding the VOLUME instruction in Docker
When using Dockerfile, we use several instructions to automatically build single or multiple containers. An example of these instructions is the VOLUME instruction.
In the Dockerfile, it should look like this:
FROM ubuntu
RUN mkdir /samplevol
VOLUME /samplevol
However, some confusion arises when we try to enter the value of the VOLUME instruction in the Dockerfile. When using the VOLUME instruction, we need to understand that this is not where we indicate the location of the Docker volume outside the container.
Instead, the VOLUME instruction is where we tell the target where inside the container we are going to mount our external Docker volume when the container is already running.
If we remember, when we run a container we can use the --volume or -v parameter to mount a specific volume. However, this parameter has other uses as well.
For example, using the --volume parameter we can specify where the Docker volume should be mounted.
Example command:
$ docker run --volume=/volumesd:/samplevol sushi_bake
In the above command, the /volumesd directory is the location of the unmounted Docker volume. The /samplevol directory is where we will mount the Docker volume in the container we create and run.
Additionally, the **/samplevol** directory is the correct value for the VOLUME instruction in the Dockerfile.
Alternatively, we can avoid confusion by not adding the VOLUME instruction in the Dockerfile. If we do not add a mount point in the VOLUME instruction, Docker assigns a default location for the volume to be mounted.
This location is usually under /var/lib/docker/volumes .
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
The difference between CMD and ENTRYPOINT in Docker
Publish Date:2025/03/25 Views:141 Category:Docker
-
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container
Use the Dockerfile to create a directory in the container using the Mkdir command
Publish Date:2025/03/25 Views:73 Category:Docker
-
Docker containers have become the de facto way to manage software and dependencies in different environments. When working with real-world applications, you will undoubtedly need to create a Dockerfile before you can build your application