Ad space (banner)
AD

Got the fundamentals? A structured Docker course is a fast way to level up.

Find Docker courses on Udemy

Affiliate link. Costs you nothing extra.

🐳Docker Lessons
Lesson 19 / 45

Mounting a Volume with docker run -v

When you need data to survive the container, use -v. It mounts a host directory or a named volume into the container.

The syntax is docker run -v host-path:container-path image.

Running docker run -v /data:/var/lib/data nginx makes the host's /data visible inside the container at /var/lib/data, so writes there persist after the container is gone.

A common stumble is losing database contents on docker rm because no volume was mounted. Anything you care about must live on a volume.

In real work, volumes hold database files and uploaded content, and mounting your source directory during development means edits appear in the container instantly.

Docker
Try it (type this into the pseudo terminal)
docker run -v /data:/var/lib/data nginx

🧑‍💻 You can type this command into the Docker pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)