Docker volume / Docker bind

·

2 min read

what is docker voulme :-

Volumes are persistent data stores for containers, created and managed by Docker. You can create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation.

exmple :-

When you ran your container without a volume:

docker run -it --rm storepythonapp:v1
  • The file server.txt was stored inside the container’s filesystem.

  • But every time you ran --rm, the container was deleted after exit.

  • That means all data in the container (including server.txt) was lost.

  • So, names you saved in one container would not persist in a new container unless you kept the same container alive.

That’s why when you reran without a volume, it only showed names saved within that container session.


2️⃣ How Docker volumes help

When you run:

docker run -it -v puthonVolume:/app --rm storepythonapp:v1
  • -v puthonVolume:/app mounts a persistent volume named puthonVolume to /app inside the container.

  • Now, any file created inside /app (like server.txt) is saved in the volume.

  • Even if you remove the container (--rm), the data in the volume remains.

So next time you run another container with the same volume:

docker run -it -v puthonVolume:/app --rm storepythonapp:v1


Docker Bind

what is docker bind :-

A bind mount is a way to link a directory or file from your host machine to a directory inside a Docker container. Changes made in the container reflect on the host, and vice versa.

Use / Purpose:

  • Persist data outside the container.

  • Share files between host and container.

  • Useful for development, so code changes on host are visible in the container immediately.

Example:

docker run -it -v /home/user/myproject:/app --rm storepythonapp:v1
  • /home/user/myproject → host folder

  • /app → container folder

  • Now server.txt or any files created in /app are saved directly on the host.


Refrence : - https://www.youtube.com/watch?v=OhnTMWmfTBE&t=7416s

https://docs.docker.com/engine/storage/volumes/

Comments (1)

Discuss on Hashnode

Great read! Understanding the differences between Docker volumes and bind mounts is crucial for effective containerized development. For smoother local development, I’ve started using ServBay (servbay.com) — it lets me spin up isolated environments quickly, so I can test changes without worrying about my main setup.