当前位置: 首页 > 科技观察

WhyisDockerfilemountnotrecommended-

时间:2023-03-13 12:23:35 科技观察

Today,afriendinthegroupsentaquestion.Itstartednginxthroughdocker.Somerelatedconfigurationfilesofnginxaremappedthroughfiles.Whyisthecontentofthefilemappedtothecontainerunchanged?Notmuchtosay,startacontainerandseedockerrun--namenginx-p8082:80-v/opt/nginx/nginx.conf:/etc/nginx.conf-dnginxUnabletofindimage'nginx:latest'locallylatest:Pullingfromlibrary/nginxa2abf6c4d29d:Pullcompletea9edb18cadd1:Pullcomplete589b7251471a:Pullcomplete186b1aaa4aa6:Pullcompleteb4df32aa5a72:Pullcompletea0bcbecc962e:PullcompleteDigest:sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31Status:Downloadednewerimagefornginx:latest3d669ab21cfc6845f8f7105233215c9ab9002ddd06af1132fba257e14f93121e我这里直接映射了个nginx的主配置文件,别管目录对不对,只是验证文件修改然后Thecontentofthefileatthistimeshouldbeconsistent.Nowdirectlymodifythe/opt/nginx/nginx.conffileonthehostmachine,addacommenttothefirstlineandsaveit,enterthecontainer,andlookatthecontentofthenginx.conffileinthecontainer.Youcandirectlyexecutethecommandtoviewandenterthecontainerdockerexec-it3d/bin/bashtoviewthehead/etc/nginx.confatthebeginningofthefileordirectlyviewdockerexec3dcat/etc/nginx.confonthehostmachine.Youcanseethatthecontentofthefilehasnotchanged.Why?ThisproblemisrelatedtotheinodeoftheLinuxfile.Whenstartingthecontainerforthefirsttime,lookattheinodesofthetwofiles.Thehostcontainercanseethatitisthesameinode,thatis,thesamefile.Afterthehostmodifiesthefilecontent,Comparethehost两个容器不是一个文件。如果此时想让容器中的文件生效,需要重启容器,重新加载新文件并检查目录映射,或者启动一个容器dockerrun--namenginx-p8082:80-v/opt/nginx:/opt/-dnginx537fc0c513d3bb83bb78d4f8333ede6a8a9d53070108ab55be5a8b9f2347ac9c这次挂载的是目录,本地/opt/nginx挂载在容器/opt/下,然后重复上面的操作,查看inode,修改文件,对比inode看目录挂载没有问题。为什么容器中的文件可以随宿主机文件实时变化?其实就是那句经典的话。在Linux下,一切都是文件,目录也是一种特殊的文件。其实它也有自己的inode,挂载目录的时候,实际上是把容器中目录的inode指向宿主机的目录,所以目录中文件的变化会实时变化,而如果挂载了文件,容器中实际指向的inode还是原来的,而当前文件实际上已经不是inode了。本文转载自微信“运维研究院”,可通过以下二维码关注。转载本文请联系运维院公众号。