How to use Docker Volumes
The following command launches a fresh Ubuntu 22.04 container and connects your terminal to it (-it), enabling you to execute example commands in the ensuing stages. Within the container, a volume named demo_volume is mounted to /data. Use the following command right now:
$ docker run -it -v demo_volume:/data ubuntu:22.06
- Give a list of everything in the /data directory of your container:
$ ls /data
- Include a test file with any random content:
$ echo "foobar" > /data/foo
$ cat /data/foo
foobar
- Launch a fresh container with the same volume attached now:
$ docker run -it -v demo_volume:/app alpine:latest
- Add the –mount option to the docker run command in order to mount a data volume to a container.
- It stores the data created inside the virtual environment by adding volume to the designated container.
- To launch a container and mount a data drive to it, use the following syntax:
$ docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]
What Is Docker Volume?
Docker containers enable apps to execute in an isolated environment. All modifications made inside the container are lost by default when it ends. Docker volumes and bind mounts can be useful for storing data in between runs. One way to store data outside of containers is with volumes. All volumes are kept in a specific directory on your host, typically /var/lib/docker/volumes for Linux systems, and are controlled by Docker.
Contact Us