JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Understanding volume instructions in Docker

Author:JIYIK Last Updated:2025/03/25 Views:

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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial