docker重启后数据是否会丢失
大家在使用Docker部署web应用或者mysql数据库时,会发现当容器重启后,容器运行过程中产生的日志或者数据库数据都会被清空。
解决方法:
docker可以通过挂载宿主机磁盘目录,来永久存储数据。
1. 创建容器时执行Docker Volume
使用 docker run 命令,可以运行一个 Docker容器,使用镜像ubuntu/nginx,挂载本地目录/tmp/source到容器目录/tmp/destination
docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash
基于ubuntu/nginx镜像创建了一个Docker容器。
指定容器的名称为test,由 ––name 选项指定。
Docker Volume 由 ––volume (可以简写为-v)选项指定,主机的 /tmp/source 目录与容器中的 /tmp/destination 目录一一对应。
2. 查看Docker Volume
使用 docker inspect 命令,可以查看 Docker容器 的详细信息:
docker inspect --format=’{{json .Mounts}}'test | python -m json.tool[{“Destination”: “/tmp/destination”,“Mode”: “”,“Propagation”: “”,“RW”: true,“Source”: “/tmp/source”,“Type”: “bind”}]
使用 ––format 选项,可以选择性查看需要的容器信息。 .Mount 为容器的 Docker Volume 信息。
python -m json.tool 可以将输出的json字符串格式化显示。
Source 表示主机上的目录,即 /tmp/source 。
Destination 为容器中的目录,即 /tmp/destination。
3. 本机文件可以同步到容器
在本机/tmp/source目录中新建hello.txt文件
touch /tmp/source/hello.txtls /tmp/source/hello.txt
hello.txt文件在容器/tmp/destination/目录中可见
使用 docker exec 命令,可以在容器中执行命令。
docker exectest ls /tmp/destination/hello.txt
所以在宿主机对目录 /tmp/source/ 的修改,可以同步到容器目录 /tmp/destination/ 中。
4. 容器文件可以同步到宿主机
在容器/tmp/destination目录中新建world.txt文件
docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt
world.txt文件在宿主机/tmp/source/目录中可见
ls /tmp/source/hello.txt world.txt
更多相关教程,请关注PHP中文网docker教程栏目。
以上是docker重启后数据是否会丢失的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可切换到国内镜像源,步骤如下:1. 编辑配置文件 /etc/docker/daemon.json,添加镜像源地址;2. 保存退出后,重启 Docker 服务 sudo systemctl restart docker,即可提升镜像下载速度和稳定性。

如何使用 Docker Desktop?Docker Desktop 是一款工具,用于在本地机器上运行 Docker 容器。其使用步骤包括:1. 安装 Docker Desktop;2. 启动 Docker Desktop;3. 创建 Docker 镜像(使用 Dockerfile);4. 构建 Docker 镜像(使用 docker build);5. 运行 Docker 容器(使用 docker run)。

创建 Docker 镜像步骤:编写包含构建指令的 Dockerfile。在终端中构建镜像,使用 docker build 命令。标记镜像,使用 docker tag 命令分配名称和标签。

要获取 Docker 版本,您可以执行以下步骤:运行 Docker 命令“docker --version”来查看客户端和服务器版本。对于 Mac 或 Windows,还可以通过 Docker Desktop GUI 的“版本”选项卡或“关于 Docker Desktop”菜单查看版本信息。

查看 Docker 日志的方法包括:使用 docker logs 命令,例如:docker logs CONTAINER_NAME使用 docker exec 命令运行 /bin/sh 并查看日志文件,例如:docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log使用 Docker Compose 的 docker-compose logs 命令,例如:docker-compose -f docker-com

在 Docker 中保存镜像,可以使用 docker commit 命令创建新的镜像,包含指定容器的当前状态,语法为:docker commit [选项] 容器ID 镜像名称。要保存镜像到仓库,可以使用 docker push 命令,语法为:docker push 镜像名称[:标签]。要导入已保存的镜像,可以使用 docker pull 命令,语法为:docker pull 镜像名称[:标签]。

您可以构建 Docker 私有仓库以安全地存储和管理容器镜像,提供严格的控制和安全性。步骤包括:创建存储库、授予访问权限、部署仓库、推送镜像和拉取镜像。优点包括安全性、版本控制、减少网络流量和定制化。
