宿主機(jī)目錄掛載到docker容器后的訪問權(quán)限問題

問題描述

使用docker運(yùn)行鏡像為容器時(shí),為了以后維護(hù)目標(biāo)容器中的文件簡單方便,常常會使用run -v參數(shù),將宿主機(jī)的某個(gè)目錄掛在給docker容器中的某個(gè)目錄
示例:
docker run -v /data:/data --name test %image_id
上述命令將運(yùn)行鏡像id為%image_id的鏡像為容器,容器nametest,并且將宿主機(jī)的/data目錄掛載給該容器的/data目錄,即實(shí)現(xiàn)了對容器/data目錄的共享。
假設(shè)容器內(nèi)/data目錄的權(quán)限為:
讀寫權(quán)限 組名 用戶名
drwxr-xr-x. root root
而宿主機(jī)/data目錄權(quán)限為:
drwxr-xr-x. test test
這時(shí)容器內(nèi)/data目錄中有一個(gè)腳本會在容器啟動時(shí)在當(dāng)前目錄下創(chuàng)建一個(gè)文件夾,此時(shí)操作系統(tǒng)就會給出Permission denied提示,甚至?xí)?dǎo)致整個(gè)docker容器重啟失敗,這個(gè)問題的原因有些基礎(chǔ)的同學(xué)便會一目了然。這是因?yàn)槿萜髂夸浐蛼燧d目錄的組名和用戶名不同,導(dǎo)致使用test用戶在宿主機(jī)/data目錄下沒有讀寫權(quán)限。

參考內(nèi)容

網(wǎng)上有很多類似的問題,解決方案大體一致,下面的鏈接是我解決該問題時(shí)的參考內(nèi)容
多數(shù)網(wǎng)友的解決方案
docker掛載volume的用戶權(quán)限問題,理解docker容器的uid

我的解決方案

合理的使用run命令參數(shù)

  • --privileged,該參數(shù)可以設(shè)置是否給docker容器特權(quán),如果該參數(shù)為true,使得docker容器內(nèi)的root權(quán)限為宿主機(jī)的root權(quán)限,而非只是容器內(nèi)的root權(quán)限
  • -u,該參數(shù)可以用來顯示的設(shè)置容器內(nèi)應(yīng)用的戶用,默認(rèn)是root或者可以在dockerfile中設(shè)置用戶
    繼續(xù)上面的問題,修改run命令
    docker run -v /data:/data -u root --privileged=true --name test %image_id
    這時(shí)容器內(nèi)/data的組名和用戶名就會是root,如下
    drwxr-xr-x. root root
    這時(shí)容器內(nèi)外的/data目錄讀寫權(quán)限、組、用戶都保持一致,就不會再出現(xiàn)
    Permission denied問題了

實(shí)際問題(sonatype nuxes3 docker容器啟動出錯(cuò),提示權(quán)限不足)

筆者某天興致不錯(cuò),放下了手機(jī),準(zhǔn)備把maven私服搭建一下,于是在centos服務(wù)器上執(zhí)行一下命令
docker pull sonatype/nexus3
docker run -d --name nexus3 --restart=always -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 8084:8084 -v $(pwd):/nexus-data %image_id
docker ps
然后發(fā)現(xiàn)STATUS不對,于是查看日志
docker logs nexus3
發(fā)現(xiàn)以下錯(cuò)誤:

Unable to update instance pid: Unable to create directory /nexus-data/instances
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to No such file or directory
Warning: Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning: Forcing option -XX:LogFile=/tmp/jvm.log
java.io.FileNotFoundException: ../sonatype-work/nexus3/tmp/i4j_ZTDnGON8hezynsMX2ZCYAVDtQog=.lock (No such file or directory)
at java.io.RandomAccessFile.open0(Native Method)
at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at com.install4j.runtime.launcher.util.SingleInstance.check(SingleInstance.java:72)
at com.install4j.runtime.launcher.util.SingleInstance.checkForCurrentLauncher(SingleInstance.java:31)
at com.install4j.runtime.launcher.UnixLauncher.checkSingleInstance(UnixLauncher.java:88)
at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:67)
java.io.FileNotFoundException: /nexus-data/karaf.pid (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at org.apache.karaf.main.InstanceHelper.writePid(InstanceHelper.java:126)
at org.apache.karaf.main.Main.launch(Main.java:243)
at org.sonatype.nexus.karaf.NexusMain.launch(NexusMain.java:113)
at org.sonatype.nexus.karaf.NexusMain.main(NexusMain.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:85)
at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:69)
java.lang.RuntimeException: /nexus-data/log/karaf.log (No such file or directory)
at org.apache.karaf.main.util.BootstrapLogManager.getDefaultHandlerInternal(BootstrapLogManager.java:102)
at org.apache.karaf.main.util.BootstrapLogManager.getDefaultHandlersInternal(BootstrapLogManager.java:137)
at org.apache.karaf.main.util.BootstrapLogManager.getDefaultHandlers(BootstrapLogManager.java:70)
at org.apache.karaf.main.util.BootstrapLogManager.configureLogger(BootstrapLogManager.java:75)
at org.apache.karaf.main.Main.launch(Main.java:244)
at org.sonatype.nexus.karaf.NexusMain.launch(NexusMain.java:113)
at org.sonatype.nexus.karaf.NexusMain.main(NexusMain.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:85)
at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:69)
Caused by: java.io.FileNotFoundException: /nexus-data/log/karaf.log (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at org.apache.karaf.main.util.BootstrapLogManagerSimpleFileHandler.open(BootstrapLogManager.java:193) at org.apache.karaf.main.util.BootstrapLogManagerSimpleFileHandler.<init>(BootstrapLogManager.java:182)
at org.apache.karaf.main.util.BootstrapLogManager.getDefaultHandlerInternal(BootstrapLogManager.java:100)
... 12 more
Error creating bundle cache.
Unable to update instance pid: Unable to create directory /nexus-data/instances
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to No such file or directory
Warning: Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning: Forcing option -XX:LogFile=/tmp/jvm.log

這時(shí)我很快猜到是-v參數(shù)惹出的麻煩,于是刪除了啟動失敗的容器后重新從鏡像運(yùn)行新的容器,命令如下(基于剛才的命令刪除了-v參數(shù))
docker run -d --name nexus3 --restart=always -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 8084:8084 -v $(pwd):/nexus-data %image_id
這時(shí)容器啟動成功了,于是好奇,便進(jìn)入容器內(nèi)查看問題目錄的權(quán)限信息,如下
drwxr-xr-x. nexus nexus
由于docker官方是不推薦以root權(quán)限啟動容器的,這也是為什么nexus升級到3以后默認(rèn)以用戶nexus啟動容器的原因,所以第一次是想把宿主機(jī)掛載過去的目錄改為nexus,但是發(fā)現(xiàn)宿主機(jī)根本沒有這個(gè)用戶(注意:只是沒有這個(gè)用戶而已,但是這個(gè)用戶的uid還是存在的),所以無法修改我的宿主機(jī)目錄用戶,于是利用了docker run的-u參數(shù),指定以root用戶啟動,命令如下
docker run -d --name nexus3 --restart=always -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 8084:8084 -u root --privileged=true -v $(pwd):/nexus-data %image_id

注:加--privileged=true是為讓容器內(nèi)root權(quán)限為真的root權(quán)限,上面提到過

隨后容器啟動成功,沒有報(bào)錯(cuò),再次進(jìn)入docker容器查看剛才的問題目錄用戶信息,如下:
drwxr-xr-x. root root
至此,問題已解決!如有描述不清的地方,還請大家多多指教!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容