/

How to Access Files Outside a Docker Container: A Guide to Bind Mounts and Volumes

How to Access Files Outside a Docker Container: A Guide to Bind Mounts and Volumes

When working with Docker containers and the need arises to access files from the host machine, there are a few ways to accomplish this. One common approach is to use Bind Mounts and Volumes. These mechanisms allow for the storage of data from within the container in a way that persists even after the container is removed.

Bind Mounts, denoted by the -v or --volume flag, are a convenient way to map folders on the host computer to specific locations within the container. For example, if your application generates log files within the container’s /usr/src/app/logs folder, you can map this folder to a corresponding location on the host machine using the -v flag in the following manner: -v ~/logs:/usr/src/app/logs. This will establish a connection between the two folders, with the logs subfolder residing in the user’s home directory on the host machine.

When it comes to running a container, the -m or --mount flag can be used in a similar manner as the -v flag. Continuing with the earlier example, you can execute the following command to run the container using the examplenode image:

1
docker run -d -p 80:3000 -v ~/logs:/usr/src/app/logs --name node-app examplenode

In doing so, any logs generated by the Node application will now be stored on the host computer rather than within the Docker container. It’s important to note that the examplenode app used here does not generate any logs by default in /usr/src/app/logs; you would need to set up logging within your application accordingly.

For a more detailed view of the volume configuration, running docker inspect on the container name will display the relevant information under the “Mounts” section. In this case, you would see "Type": "bind", indicating the creation of a bind mount.

Now, let’s explore Volumes. Volumes differ from Bind Mounts in that Docker manages the data storage in a folder of its own, taking care of file permissions, ownership, and providing tools for volume management. While Bind Mounts are tied to specific filesystem paths, Docker does not provide the same level of tooling for them.

To create a volume, the docker volume create command is used:

1
docker volume create logs

By running commands such as docker volume ls and docker volume inspect, you can obtain additional information about the system volumes.

Next, when running the docker run command, you can specify the volume name instead of a folder using the -v flag. For example:

1
docker run -d -p 80:3000 -v logs:/usr/src/app/logs --name node-app examplenode

Inspecting the image with docker inspect will now reveal the mounted volume:

1
2
3
4
5
6
7
8
9
10
11
12
"Mounts": [
{
"Type": "volume",
"Name": "logs",
"Source": "/var/lib/docker/volumes/logs/\_data",
"Destination": "/usr/src/app/logs",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],

As shown, the logs will be stored in the /var/lib/docker/volumes/logs/\_data folder.

Volumes play a crucial role when deploying containers on cloud services. Additionally, you can remove a volume by running the docker volume rm <name> command.

In summary, Bind Mounts and Volumes are effective ways to access files outside Docker containers and enable the persistence of data beyond the lifespan of a container. Properly utilizing these mechanisms will enhance your containerized development workflow.

Tags: docker, containers, bind mounts, volumes, file storage, persistent data