我的专题DockerDocker容器

容器挂载数据卷的几种情况

2018-06-20  本文已影响99人  Rethink
`宿主机存在文件:rainbow`
$ cat rainbow 
This is my rainbow

`运行容器`
$ docker run -d -v $PWD/rainbow:$HOME/rainbow --name test_1 alpine tail -f /dev/null
38f19e99a0e00e67a605adc6e6b0b8d039bc78a49cb2491871ccf6fb761f564e

`容器内查看文件,挂载成功`
$ docker exec -it test_1 cat ~/rainbow 
This is my rainbow

`挂载本地环境变量到容器`
$ docker run -d -v /etc/profile:/etc/profile --name test_2 alpine tail -f /dev/null
x569cae718223b1d1f506c2d7a597d1a1b7fb13dceadab379bf4065d89ba9605846

`挂载成功,容器内的文件内容会被覆盖!`
$ docker exec -it test_2 cat ~/rainbow
...
`容器启动失败`
$ docker run -d --name test_3 -v ~/test:/etc/apk/repositories alpine tail -f /dev/null
72b75e729dec991671daf54e4ed0985ba3c8fc93866066628446a697b64e2967
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"rootfs_linux.go:54: mounting \\\"/root/test\\\" to rootfs \\\"/var/lib/docker/overlay2/b989ba9605846a51027922c5656fb809c173b0617abcc0bb66
f8487ecbb22965/merged\\\" at \\\"/var/lib/docker/overlay2/b989ba9605846a51027922c5656fb809c173b0617abcc0bb66f8487ecbb22965/merged/etc/apk/repositories\\\" caused \\\"not a directory\\\"\""
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

`挂载本地jenkins_home目录到容器`
$ docker run -d --name test_4 -v /opt/jenkins_home:/var/jenkins_home alpine tail -f /dev/null
f709cae718223b1d1f506c2d7a597d1a1b7fb13dceadab379bf4065d982b462f

`挂载成功,容器内会自动新建此目录`
$ docker exec -it test_4 ls /var/jenkins_home
config.xml
copy_reference_file.log
hudson.model.UpdateCenter.xml
hudson.plugins.git.GitTool.xml
identity.key.enc
init.groovy.d
...

$ docker run -d --name test_5 -v /etc/cron.d:/etc/apk/ alpine tail -f /dev/null
b2c132e2f6cc3b7c726f6bfef9779543a8b031348cc2ba592353babacf781436

$ ls /etc/cron.d
0hourly

`挂载成功,容器内目录原内容被覆盖`
$ docker exec -it test_5 ls /etc/apk
0hourly

$ docker run -d --name test_6 -v ~/temp:/etc/temp alpine tail -f /dev/null
bd5c70a0f341ac1d4c6f6061ccb97c147e4ef9852835c4368aecc03d47c92572

`挂载成功,docker会自动在宿主机和容器内新建此目录`
$ docker exec -it test_6 ls /etc/temp
$ 

$ docker run -d --name test_7 -v ~/test/:/etc/apk/ alpine tail -f /dev/null
a19443399641e3461155afe1b5bc410f913dc4ecfe2eb50b9bc652a48307b715

`挂载成功,容器内目录内容被覆盖(空)`
$ docker exec -it test_7 ls /etc/apk
$ 

`容器启动失败`
$ docker run -d --name test_8 -v /etc/profile:/etc/apk/ alpine tail -f /dev/null
2dda82d7d7312f132aa6930fe919375685dc2904e0ecad312e28d1274c9add4b
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"rootfs_linux.go:54: mounting \\\"/etc/profile\\\" to rootfs \\\"/var/lib/docker/overlay2/9205ea29badfcf3f49ffa1058797ce31ac6e750fbc1bd6a09598737958b5d478/merged\\\" at \\\"/var/lib/docker/overlay2/9205ea29badfcf3f49ffa1058797ce31ac6e750fbc1bd6a09598737958b5d478/merged/etc/apk\\\" caused \\\"not a directory\\\"\""
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

`容器启动失败`
$ docker run -d --name test_10 -v ~/case/:/etc/apk/repositories alpine tail -f /dev/null
908c7bb8e387233733415a5da430526c5ddbe67775885d11014b607a31d07849
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"rootfs_linux.go:54: mounting \\\"/root/case\\\" to rootfs \\\"/var/lib/docker/overlay2/073f27fd5fe62ba821f178e2a5ac0137c197c16f9d5f5a73ce4e617eb80a3516/merged\\\" at \\\"/var/lib/docker/overlay2/073f27fd5fe62ba821f178e2a5ac0137c197c16f9d5f5a73ce4e617eb80a3516/merged/etc/apk/repositories\\\" caused \\\"not a directory\\\"\""
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

总结

宿主机 容器 运行结果
文件存在 文件不存在 挂载成功
目录存在 目录不存在 挂载成功
------ ------ ------
文件存在 文件存在 挂载成功,容器内的文件内容被覆盖
目录存在 目录存在 挂载成功,容器内目录内容被覆盖
------ ------ ------
文件存在 目录存在 💥 容器启动失败
目录存在 文件存在 💥 容器启动失败
------ ------ ------
目录不存在 目录不存在 挂载成功,docker会自动在宿主机和容器内新建此目录
目录不存在 文件存在 💥 容器启动失败
目录不存在 目录存在 挂载成功,容器内目录内容被覆盖(空)
上一篇下一篇

猜你喜欢

热点阅读