Docker使用札記 - 使用中遇到的問題總結

1. 啓動容器時報錯誤「: No such file or directory」mysql

通常來講做爲容器應用的入口都是entrypoint.sh文件,也就是Dockerfile最後一條指令爲是:linux

ENTRYPOINT ["/entrypoint.sh"]

 

開始覺得是修改的部分哪裏不對,將內部內容改成只有一行命令:git

date && pwd

  

從新構建並啓動容器,仍然不行。網上有說是文件權限的,可是因爲Windows系統將文件放入Linux鏡像中是默認加了執行權限(+x),詳細解釋可參看這裏github

原文:sql

That warning was added, because the Windows filesystem does not have an option to mark a file as 'executable'. Building a linux image from a Windows machine would therefore break the image if a file has to be marked executable.docker

For that reason, files are marked executable by default when building from a windows client; the warning is there so that you are notified of that, and (if needed), modify the Dockerfile to change/remove the executable bit afterwards.shell

因此這解釋跳過。數據庫

後面在SO看到一條回答才猛然醒悟,這個問題每次在Windows上編寫shell腳本常常遇到。問題就出在換行符上,將CRLF改成LF,保持再次構建並啓動容器,問題獲得解決。windows

 2. 在使用Docker-compose中使用MySQL時,沒有按預期建立數據庫,用戶等信息bash

在設置了所需的環境變量MYSQL_DATABASE等沒有按預期建立數據庫等信息:

mysql:
     image: mysql:5.7
     volumes:
       - dbdata:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: test
       MYSQL_DATABASE: test
       MYSQL_USER: test
       MYSQL_PASSWORD: test  

 進過屢次實驗和查詢得知原來是因爲其中的volumes項致使的,固然並非不能使用該項,並且很是有必要。緣由出在以前構建時使用的volume中已經存在MySQL文件,則上述的環境變量再也不生效,須要刪掉volume,刪掉後從新建立啓動便可:

docker volume ls //得到相應的volume名稱
docker volume rm project_db 

或者:

docker-compose down //停掉當前項目的服務
docker volume prune //刪除全部未使用的volume

而後重建並重啓服務:

docker up -d

更多詳情請參看這裏

相關文章
相關標籤/搜索