How to access files outside the Docker container

If the containers are isolated, how do they communicate with the host, perhaps to store data? Because when we create a container from an image, all data generated after deleting the container will be lost.

Therefore, we need a permanent storage method.

We can useBound mountwithNumber of rolls.

There is not much difference between the two, except that Bind Mounts can point to any folder on the host and are not directly managed by Docker.

Let's start with them. A classic example is the log. Suppose your application creates a log file in the following location in the container:/usr/src/app/logs. you can use it-v(it's the same as--volume) When you run the container with the following commanddocker run, like this:-v ~/logs:/usr/src/app/logs

This will map the folder to the logs subfolder in the user's home directory.

node:-mor--mountThe logo works very similarly

This is the same asexamplenodeThe image we created earlier:

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

Therefore, now we can run the Node application and all logs will be stored in the host instead of in the Docker container.

Please noteexamplenodeThe application does not generate any login information/usr/src/app/logs, This is just an example, you need to set up this logging first.

Detailed information about the volume will be listed at runtimedocker inspectUnder "Mount" in the container name:

"Mounts": [
    {
        "Type": "bind",
        "Source": "/Users/flavio/logs",
        "Destination": "/usr/src/app/logs",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    }
],

Can you see "Type": "bind"? That means that we have created a bind mount.

Now, let’s talk about Volumes.

The difference between Bind Mounts and Volumes is that by creating volumes, Docker will store the data in a folder it manages, which means it will take care of file permissions and ownership, and it will give you the tools to manage those volumes. While bind mounts are based on filesystem paths, and Docker can’t provide the tooling around them.

For example, Docker lets you remove all unused volumes by running docker volume prune or docker system prune --volumes.

To create a volume, we first need to run docker volume create:

docker volume create logs

Now you can use docker volume ls and docker volume inspect to get more data about the system volumes:

Now run docker run with the option -v logs:/usr/src/app/logs (tell the volume name instead of a folder)

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

Now running docker inspect on the image will show the mounted volume:

"Mounts": [
    {
        "Type": "volume",
        "Name": "logs",
        "Source": "/var/lib/docker/volumes/logs/_data",
        "Destination": "/usr/src/app/logs",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
],

See? Now the logs will be stored in the /var/lib/docker/volumes/logs/_data folder.

Volumes will be essential when it will be time to deploy a container on a cloud service, for example.

You can remove a volume running docker volume rm <name>.


More docker tutorials: