問(wèn)題描述
使用docker運(yùn)行鏡像為容器時(shí),為了以后維護(hù)目標(biāo)容器中的文件簡(jiǎn)單方便反惕,常常會(huì)使用run -v參數(shù)尝艘,將宿主機(jī)的某個(gè)目錄掛在給docker容器中的某個(gè)目錄
示例:
docker run -v /data:/data --name test %image_id
上述命令將運(yùn)行鏡像id為%image_id
的鏡像為容器,容器name
為test
姿染,并且將宿主機(jī)的/data
目錄掛載給該容器的/data
目錄背亥,即實(shí)現(xiàn)了對(duì)容器/data目錄的共享。
假設(shè)容器內(nèi)/data目錄的權(quán)限為:
讀寫(xiě)權(quán)限 組名 用戶(hù)名
drwxr-xr-x. root root
而宿主機(jī)/data目錄權(quán)限為:
drwxr-xr-x. test test
這時(shí)容器內(nèi)/data目錄中有一個(gè)腳本會(huì)在容器啟動(dòng)時(shí)在當(dāng)前目錄下創(chuàng)建一個(gè)文件夾悬赏,此時(shí)操作系統(tǒng)就會(huì)給出Permission denied
提示狡汉,甚至?xí)?dǎo)致整個(gè)docker容器重啟失敗,這個(gè)問(wèn)題的原因有些基礎(chǔ)的同學(xué)便會(huì)一目了然闽颇。這是因?yàn)槿萜髂夸浐蛼燧d目錄的組名和用戶(hù)名不同盾戴,導(dǎo)致使用test
用戶(hù)在宿主機(jī)/data
目錄下沒(méi)有讀寫(xiě)權(quán)限。
參考內(nèi)容
網(wǎng)上有很多類(lèi)似的問(wèn)題兵多,解決方案大體一致尖啡,下面的鏈接是我解決該問(wèn)題時(shí)的參考內(nèi)容
多數(shù)網(wǎng)友的解決方案
docker掛載volume的用戶(hù)權(quán)限問(wè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ù)可以用來(lái)顯示的設(shè)置容器內(nèi)應(yīng)用的戶(hù)用畏梆,默認(rèn)是root或者可以在dockerfile中設(shè)置用戶(hù)
繼續(xù)上面的問(wèn)題,修改run命令
docker run -v /data:/data -u root --privileged=true --name test %image_id
這時(shí)容器內(nèi)/data的組名和用戶(hù)名就會(huì)是root,如下
drwxr-xr-x. root root
這時(shí)容器內(nèi)外的/data目錄讀寫(xiě)權(quán)限奠涌、組宪巨、用戶(hù)都保持一致,就不會(huì)再出現(xiàn)
Permission denied
問(wèn)題了
實(shí)際問(wèn)題(sonatype nuxes3 docker容器啟動(dòng)出錯(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不對(duì)天吓,于是查看日志
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.<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ù)惹出的麻煩,于是刪除了啟動(dòng)失敗的容器后重新從鏡像運(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í)容器啟動(dòng)成功了,于是好奇汰规,便進(jìn)入容器內(nèi)查看問(wèn)題目錄的權(quán)限信息汤功,如下
drwxr-xr-x. nexus nexus
由于docker官方是不推薦以root權(quán)限啟動(dòng)容器的,這也是為什么nexus升級(jí)到3以后默認(rèn)以用戶(hù)nexus啟動(dòng)容器的原因溜哮,所以第一次是想把宿主機(jī)掛載過(guò)去的目錄改為nexus滔金,但是發(fā)現(xiàn)宿主機(jī)根本沒(méi)有這個(gè)用戶(hù)(注意:只是沒(méi)有這個(gè)用戶(hù)而已,但是這個(gè)用戶(hù)的uid還是存在的)茂嗓,所以無(wú)法修改我的宿主機(jī)目錄用戶(hù)餐茵,于是利用了docker run的-u參數(shù),指定以root用戶(hù)啟動(dòng)述吸,命令如下
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)限忿族,上面提到過(guò)
隨后容器啟動(dòng)成功,沒(méi)有報(bào)錯(cuò)蝌矛,再次進(jìn)入docker容器查看剛才的問(wèn)題目錄用戶(hù)信息道批,如下:
drwxr-xr-x. root root
至此,問(wèn)題已解決入撒!如有描述不清的地方隆豹,還請(qǐng)大家多多指教!