需求:利用dockerfile的entrypoint在容器啟動時自動執行指令碼
# version: 0.0.1
from dialogserver
maintainer bob "[email protected]"
env mypath /root/dialog_core
workdir $mypath
expose 80
entrypoint ["./startup.sh"]
docker build -t sh_test .
#!/bin/bash
echo
'hello diango'
uwsgi dialog.xml
nginx
docker run -it -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh --rm 7782e94816a6
/usr/bin/docker-current: error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "exec: \"./startup.sh\": permission denied"
.
分析原因:許可權不足
解決:給容器新增特權
在dockerfile中新增run指令執行chmod a+x startup.sh
# version: 0.0.2
from dialogserver
maintainer bob "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run chmod a+x startup.sh
expose 80
entrypoint ["./startup.sh"]
chmod: cannot access 'startup.sh'
: no such file or directory
重新編寫dockerfile
# version: 0.0.2
from dialogserver
maintainer wanglong "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run mkdir startup && chmod -r 755 startup
expose 80
entrypoint ["./startup.sh"]
構建新映象
# 位置在dockerfile所在的目錄
docker build -t sh_test2 .
啟動新映象
docker run -it -p 8001:80 -v /home:/root/dialog_core/startup --rm bf5d1253388f
報錯 :原因還是沒有檔案,這才意識到entrypoint執行的命令必須是已有映象中存在的
/usr/bin/docker-current: error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "exec: \"./startup.sh\": stat ./startup.sh: no such file or directory"
.
重新編寫dockerfile0.3版本# version: 0.0.3
from dialogserver
maintainer bob "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run touch startup.sh && chmod a+x startup.sh
expose 80
entrypoint ["./startup.sh"]
構建並啟動
docker build -t sh_test3 .
# 啟動
docker run -it -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh --rm sh_test3
報錯:依然是檔案許可權問題
普通啟動
docker run -it sh_test3 /bin/bash
報錯:
standard_init_linux.go:178: exec user process caused "exec format error"
解決:未解決
修改docker中阻止selinux轉換的無新特權的解決方法
vim /etc/sysconfig/docker
–selinux-enabled 刪除 或者–selinux-disabled
構建dockerfile0.5版本
# version: 0.0.5
from dialogserver
maintainer wanglong "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run touch startup.sh && chmod a+x startup.sh
expose 80
cmd ["./startup.sh"]
啟動
docker run -u root -it -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh sh_test /bin/bash -c 'sh ./startup.sh'
新增引數後可以啟動,但是不是理想的樣子,繼續嘗試
構建dockerfile0.6版本,在entrypoint中加入引數『sh』
# version: 0.0.6
from dialogserver
maintainer wanglong "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run touch startup.sh
expose 80
entrypoint ["sh","./startup.sh"]
~
啟動1
docker run -u root -it -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh sh_test6
啟動2
docker run -it --rm -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh sh_test6
終於可以正常啟動,達到設想的效果
# version: 0.0.7
from sh_test6
maintainer bob "[email protected]"
env mypath /root/dialog_core
workdir $mypath
run touch startup.sh
expose 80
cmd [
"sh","./startup.sh"
]
基礎映象是基於原有的 sh_test6,cmd可以換成entrypoint,只是我想在docker run後接引數可以覆蓋cmd裡的命令
啟動容器的命令
docker run -it --rm -p 8001:80 -v /home/startup.sh:/root/dialog_core/startup.sh dialogweb
宿主機上的指令碼內容為
#!/bin/bash
echo
'hello diango'
cd /root/dialog_core/
uwsgi dialog.xml
nginx
echo
'server start'
/bin/bash
啟動容器後,輸入ctrl +p+q,退出bash
1、cmd和entrypoint裡想要執行的指令碼檔案必須是在容器構建過程中的某一層容易裡存在的檔案。
2、指令碼要保證大於1個程序,如果指令碼中不寫最後一條,容器啟動後就會自動關閉
3、docker run 後面必須跟的引數是 -it,沒有也不行(具體原因,暫時未知)
dockerfile檔案構建映象踩坑記錄
1 docker映象拉取錯誤碼 c users administrator docker pull mysql 5.7 5.7 pulling from library mysql no matching manifest for unknown in the manifest list entri...
Python 踩坑記錄
1.浮點數判斷 工作中遇到類似下面邏輯判斷 i 1 while i 1.5 i i 0.1 print i在想象中i應該停止在1.5就不輸出了,但是實際的輸出結果是無限迴圈。這是因為在計算機的邏輯中,浮點數的儲存規則決定了不是所有的浮點數都能準確表示,有些是不準確的,只是無限接近。如0.1轉換為二進...
Java踩坑記錄
1.quartz整合spring框架service層物件注入為null解決方案 jobdetailfactorybean中注入的是乙個cn.itcast.quartz.hellojob實現類的全路徑,底層會反射建立出乙個hellojob的物件,但是該物件不是由spring管理的,所以業務層的物件無法...