Open In App

How to Mount a File in Docker?

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A popular technology called Docker lets you package and execute programs in isolated environments known as containers. These containers ensure consistency across many contexts by incorporating all the components required to run the application, such as the runtime, code, system tools, and libraries.

Understanding Docker Volumes

Volumes in Docker provide a means of storing data produced and utilized by Docker containers. They offer a way for data to be shared and stored between several containers or between a Docker container and the host computer. Volumes survive container stops and removal since they are independent of the container’s file system.

Binding Files Using Docker CLI

One tool you use to work with containers is the Docker run command. Docker functions as a toolbox. In essence, you are instructing Docker to start and create a container based on a certain image when you perform a Docker run.

Imagine that you now possess this magical tool, Docker Run, which is capable of much more than merely starting a container. It can also link a folder from your computer to your container (–volume or -v) and attach a virtual hard disk to your container (–mount).

This is similar to providing your container with extra storage space with the –mount option. It functions similarly to connecting a USB disk to your PC. This allows you to link your container to other storage systems, like cloud drives or network shares, or even to attach files or folders from your PC to the container.

Comparable to the –mount option, but simpler, is the –volume option (abbreviated -v). Comparable to your PC and container sharing a folder. Docker handles the connection between the folders you specify on your computer and inside your container.

Using the –mount Option

Creating a Sample File: We start by making a sample file in the working directory we are currently in. The purpose of this file is to show how to mount a local file inside of a Docker container.

Sample file

Mounting the File into the Container: We use the docker run command with the –mount option to mount this local file inside a container. The following describes each component of the –mount option:

type=bind: This indicates that we are mounting files or directories from the host system into the container via a bind mount.

source=”$(pwd)”/sample.txt: The file on the host system that we wish to mount inside the container is specified here, along with its absolute path. Our fictitious file is called file.txt, and the shell command to obtain the current working directory is $(pwd).

target=/sample.txt: This tells us the exact location of the file to be mounted inside the container.

$ docker run -d -it \
--mount type=bind,source="$(pwd)"/sample.txt,target=/sample.txt,readonly \
nginx:latest

Mounting the File into the Container

With our local file file.txt mounted into the container at the path /sample.txt, this command starts and creates a new container based on the nginx:latest image. We also indicate that the mount needs to be read-only (readonly).

Verification: By running the following command within the container to view the contents of the mounted file, we can confirm that the mounting was successful:

$ docker exec -it <container_name> sh

verify

Using the –volume Option or (-v)

Three-Field Syntax: Three fields are specified when using the -v option, and they are separated by colons (:):

-v "$(pwd)"/sample2.txt:/sample2.txt:ro

The first field represents the path to a file or directory on the host machine (“$(pwd)”/sample2.txt).

The second field provides the path inside the container where the file or directory will be mounted (/sample2.txt).

file2

The third field, if provided, specifies options for the mount. In this case, ro indicates that the mount should be read-only.

Usage Example: Let’s use the -v option to mount a local file into a container:

$ docker run -d -it -v "$(pwd)"/sample2.txt:/sample2.txt:ro busybox:latest

This command creates and starts a new container based on the busybox:latest image, with the local file sample2.txt mounted into the container at the path /sample2.txt. The ro option ensures that the mount is read-only.

mount file using the -vContainers

Binding Files Using Docker Compose

Binding files using Docker Compose involves configuring volumes within a Docker Compose YAML file (docker-compose.yml). Docker Compose is a tool for defining and running multi-container Docker applications.

In the docker-compose.yml file, you can define services, networks, volumes, and other configurations for your application. When you define volumes, you specify the binding options for each service, allowing you to share files or directories between your host machine and containers.

Create File: Create the docker compose file as shown image below.

compose file

Run the docker compose: Initially check the docker compose version by using this command “docker-compose –version”. After successful conformation run the docker compose file by following the below command.

docker-compose up -d

docker-compose up -d

  • docker-compose: This is the tool you’re using to manage your multi-container Docker applications. It reads your docker-compose.yml file to figure out how to create and manage your containers.
  • up: This is the command you’re giving to Docker Compose. It tells Docker Compose to start up your containers based on the configuration specified in your docker-compose.yml file.
  • -d: This is an option you’re passing to Docker Compose. It stands for “detached” mode. When you run your containers in detached mode, they run in the background, and you get your command prompt back immediately. This means you can continue using your terminal for other tasks while your containers are running.
  • Verify: After the successfully running the container using the docker compose you can verify the file successfully mounted or not by following the below procedure.
docker ps #Running container's

containers

docker exec <container-id> cat <file-name>

output

  • docker: This is the command you use to interact with Docker, which is a platform for developing, shipping, and running applications in containers.
  • exec: This sub-command allows you to execute a command inside a running container.
  • container-id: This is the unique identifier for the Docker container in which you want to execute a command. Each container running on your system has its own ID.
  • cat: This is a command used in Unix-like operating systems to concatenate and display the contents of files.
  • file-name: This is the name of the file whose contents you want to display. You replace <file-name> with the actual name of the file you’re interested in.

Mount a file in Docker – FAQs

Can I mount a file in Docker?

Yes, you can mount a file into a Docker container using the -v or --volume option when running the docker run command.

How to mount data in Docker?

You can mount data into a Docker container using the -v or --volume option followed by the host directory path and the container directory path when running the docker run command.

What does mount mean in Docker?

In Docker, “mounting” refers to attaching a directory or file from the host machine to a directory within the container, enabling data sharing between the host and the container.

How to mount code in Docker container?

To mount code into a Docker container, use the -v or --volume option followed by the host directory containing the code and the container directory where you want to access the code when running the docker run command.

How to access local files from docker container?

You can access local files from a Docker container by using the -v or --volume option when running the docker run command, specifying the host directory containing the files and the container directory where you want to access them.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads