当前位置:网站首页>Docker - persistent database (data volume)
Docker - persistent database (data volume)
2022-08-08 12:35:00 【Nimbus 21】
Every time the container is started, the previously added data will disappear
Start the getting-started container image, browser access, and add a few items to it.Let's refresh the page, or visit another browser, the items added above are still there
But if you stop the container, restart a container, and access it with a browser, you will find that the newly added items are gone
The container's filesystem
When a container runs, it uses the layers in the image as its filesystem.Each container also has its own "scratch space" to create/update/delete files.No changes will be seen in the other container, even if they use the same image.
Data Volume
Each container starts with an image definition every time it starts.While a container can create, update, and delete files, that data is lost when a container is removed and all data changes are isolated from that container.
Data volumes provide the ability to connect a container's specific filesystem path back to the host.If a directory in the container is mounted, changes in that directory will also be seen on the host.If we mount the same directory when the container restarts, we see the same files.
Data volumes
are of two types: Named volumesnamed volumes
, bind mounts bind mounts
Data Volume
is a special directory available to one or more containers that bypasses UFS and provides many useful features:
Data volumes
can be shared and reused between containersData volume
changes will take effect immediately- The modification of
data volume
will not affect the image Data volumes
persist by default, even if the container is deleted
Persistent data
The data for the todo items is stored in a SQLite database in the container file system, directory: /etc/todos/todo.db
Since the database is a single file, if we can persist that file on the host and make it available to the next container, it should be able to pick up where it left off last.We can persist data by creating a data volume
and attaching (often called "mounting") it to the directory where the data is stored.When our container writes the todo.db
file, it will be persisted to the host in the data volume
.
1. Create a data volume.
$ docker volume create todo-db
2. Stop and delete the todo container again because it is still running and not using the data volume
.
docker psdocker rm -f
3. Start the To-Do project container and use -v
to specify the data volume
to mount.We will use this named volume
and mount it to /etc/todos
, which will capture all files created at that path.
You can also use the -v
flag to mount multiple data volumes multiple times in a docker run
command
If you don't run the docker volume create
command, but use the following command directly, docker will automatically create one for us
$ docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
4. After the container starts, open the application and add some items to the to-do list.
5. Stop and delete the container of to-do items.
docker psdocker rm -f
[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-YGTtfRFI-1659341392848) (https://raw.githubusercontent.com/yuyun-21/uplod-images/hyh/images/Flowable/image-20220617115819573.png)]
6. Start a new container using the same command as above.
$ docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
7. Open the browser and you should see the items added earlier in the list
View all data volumes
docker volume ls
View the specified data volume
docker volume inspect todo-db
Mountpoint
is the actual location on disk where the data is stored- Most devices require root access to access this directory from the host
边栏推荐
- 京东云无线宝产品部负责人张晓东 : 京东云无线宝与开源的亲密关系 | 《大神详解开源 BUFF 增益攻略》讲座回顾...
- Some optional strategies and usage scenarios for PWA application Service Worker caching
- 一起学习集合框架之 TreeSet
- 在半小时内从无到有开发并调试一款Chrome扩展(Chrome插件/谷歌浏览器插件)
- day02 -DOM - advanced events (register events, event listeners, delete events, DOM event flow, event objects, prevent default behavior, prevent event bubbling, event delegation) - commonly used mouse
- 鲲鹏开发者创享日2022:鲲鹏全栈创新 与开发者共建数字湖南
- 萤石、小米对垒智能摄像头
- 是不是只有字符串的数字水印能一直保留并且不影响计算,其他类型的字段导入数据库之后水印就会丢失?
- changes not staged for commit 解决办法
- 安科瑞预付费水电集团物业解决方案-Susie 周
猜你喜欢
随机推荐
MySQL Dual-Master 双向同步
易周金融分析 | 互联网系小贷平台密集增资;上半年银行理财子公司综合评价指数发布
Study: Toxic PFAS chemicals make rainwater unsafe to drink around the world
(原创)[C#] GDI+ 之鼠标交互:原理、示例、一步步深入、性能优化
5S软件就是将软件应用全维度简单化的软件系统
Software testing testing on behalf of the user
SQL的INSERT INTO和INSERT INTO SELECT语句
八月粉丝福利来了!大疆手机云台你爱了吗?
(原创)[C#] GDI+ 之鼠标交互:原理、示例、一步步深入、性能优化
处理器的调试接口
史上最全JVM性能调优:线程+子系统+类加载+内存分配+垃圾回收
ets声明式ui开发,怎么获取当前系统时间
phpstyle安装管理mysql
一些常见的web小功能
安装MinGW-w64
ssh 安全 之 密钥登录
Five-faced Alibaba rated P6 after taking the offer: share his interview experience
一名合格的程序员是如何优雅地解决线上问题的?
curl获取harbor镜像仓库项目下的镜像列表
Pattern Recognition Study Notes: Chapter 6 Other Classification Methods (Continuously updated...)