docker鏡像掛載目錄_docker-修改容器挂载目录的3种方法小结

方式一:修改配置文件(需停止docker服务)

sx6免费资源网

1、停止docker服务

sx6免费资源网

systemctl stop docker.service(关键,修改之前必须停止docker服务)

sx6免费资源网

2、vim /var/lib/docker/containers/container-ID/config.v2.json

sx6免费资源网

修改配置文件中的目录位置,然后保存退出

sx6免费资源网

"MountPoints":{"/home":{"Source":"/docker","Destination":"/home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"//docker/","Target":"/home"}}}

sx6免费资源网

3、启动docker服务

sx6免费资源网

systemctl start docker.service

sx6免费资源网

4、启动docker容器

sx6免费资源网

docker start

sx6免费资源网

方式二:提交现有容器为新镜像,然后重新运行它

sx6免费资源网

$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

5a3422adeead ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) About a minute ago agitated_newton

$ docker commit 5a3422adeead newimagename

$ docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

然后停止旧容器,并使用这个新容器,如果由于某种原因需要新容器使用旧名称,请在删除旧容器后使用docker rename。

sx6免费资源网

方式三:export容器为镜像,然后import为新镜像

sx6免费资源网

$docker container export -o ./myimage.docker 容器ID

$docker import ./myimage.docker newimagename

$docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

然后停止旧容器,并使用这个新容器,如果由于某种原因需要新容器使用旧名称,请在删除旧容器后使用docker rename。

sx6免费资源网

补充知识:Docker如何重启后数据不丢失,教你挂载数据卷Volume

sx6免费资源网

大家在使用Docker部署web应用或者mysql数据库时,会发现当容器重启后,容器运行过程中产生的日志或者数据库数据都会被清空,那么我们如何保存这些数据呢?

sx6免费资源网

这就需要了解docker如何挂载宿主机磁盘目录,用来永久存储数据。

sx6免费资源网

1. 创建容器时执行Docker Volume

sx6免费资源网

使用 docker run 命令,可以运行一个 Docker容器,使用镜像ubuntu/nginx,挂载本地目录/tmp/source到容器目录/tmp/destination

sx6免费资源网

docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash

sx6免费资源网

基于ubuntu/nginx镜像创建了一个Docker容器。

sx6免费资源网

指定容器的名称为test,由 ––name 选项指定。

sx6免费资源网

Docker Volume 由 ––volume (可以简写为-v)选项指定,主机的 /tmp/source 目录与容器中的 /tmp/destination 目录一一对应。

sx6免费资源网

2. 查看Docker Volume

sx6免费资源网

使用 docker inspect 命令,可以查看 Docker容器 的详细信息:

sx6免费资源网

docker inspect --format='{{json .Mounts}}'test | python -m json.tool[{“Destination”: “/tmp/destination”,“Mode”: “”,“Propagation”: “”,“RW”: true,“Source”: “/tmp/source”,“Type”: “bind”}]

sx6免费资源网

使用 ––format 选项,可以选择性查看需要的容器信息。 .Mount 为容器的 Docker Volume 信息。

sx6免费资源网

python -m json.tool 可以将输出的json字符串格式化显示。

sx6免费资源网

Source 表示主机上的目录,即 /tmp/source 。

sx6免费资源网

Destination 为容器中的目录,即 /tmp/destination。

sx6免费资源网

3. 本机文件可以同步到容器

sx6免费资源网

在本机/tmp/source目录中新建hello.txt文件

sx6免费资源网

touch /tmp/source/hello.txtls /tmp/source/hello.txt

sx6免费资源网

hello.txt文件在容器/tmp/destination/目录中可见

sx6免费资源网

使用 docker exec 命令,可以在容器中执行命令。

sx6免费资源网

docker exectest ls /tmp/destination/hello.txt

sx6免费资源网

所以在宿主机对目录 /tmp/source/ 的修改,可以同步到容器目录 /tmp/destination/ 中。

sx6免费资源网

4. 容器文件可以同步到宿主机

sx6免费资源网

在容器/tmp/destination目录中新建world.txt文件

sx6免费资源网

docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt

sx6免费资源网

world.txt文件在宿主机/tmp/source/目录中可见

sx6免费资源网

ls /tmp/source/hello.txt world.txt

sx6免费资源网

以上这篇docker-修改容器挂载目录的3种方法小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持免费资源网。

sx6免费资源网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值