[Docker] Create a Volume
We can create volumn to keep the data, even we stop the container and restart again, the data won't get lost.
To create a link between the folder /my-files
on your host machine and the htdocs
folder in the container. This also runs the container in the background.
docker run -d -p 80:80 -v /my-files:/usr/local/apache2/htdocs web-server:1.1
After runnning the container, Let’s see what this looks like from inside the container.
Attache a shell to the container:
docker container exec -it elegant_noether /bin/bash
cd to folder:
cd /usr/local/apache2/htdocs
Now we can use 'ls -la' to see what is inside the folder.
Examples:
// Create a Volume named "webdata" docker volume create --name webdata // Run a container points to "webdata" we just created docker run -d --name web1 -v webdata:/usr/share/nginx/html -p 8000:/80 nginx // Change index.html thought docker exec web1 bash -c 'echo "foo" > /usr/share/nginx/html/index.html' // you can check the index.html curl localhost:8000 // Now let's remove the container docker stop web1 docker rm web1 // Verify there is no running container docker ps -a // Create a new container with the same name as before docker run -d --name web1 -v webdata:/usr/share/nginx/html/index.html -p 8000:80 nginx // Chceck the data again curl localhost:8000 // And we can see that it prints out "foo" // We can export the same file to other container docker run -d --name web2 -v webdata:/usr/share/nginx/html/index.html -p 8001:80 nginx // Check the index.html file on web2 curl localhost:8001 // And we can see it prints out "foo" too // Change the volume on one of the container docker exec web1 bash -c 'echo "bar" > /usr/sgare/nginx/html/index.html' // And we can see both container's data changed curl localhost:8000 curl lcoalhost:8001 // To remove a volume, we need to stop all the containers docker stop web1 web2 && docker rm web1 web2 docker volume rm webdata // Verify the volume was removed docker volume ls
If you want to chck whether there is a volume inside a container:
docker inspect -f '{{.Mounts}}' web1
Inspect volume itself:
docker volume inspect webdata
分类:
Docker
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具