2017-01-11 51 views
0

我想創建一個簡單的容器,其中包含一個帶初始化數據庫的MySQL服務器。我Dockerfile目前看起來是這樣的:在Dockerfile中創建MySQL數據庫時出錯

FROM ubuntu:16.10 

RUN apt-get update 

# install MySQL; root user with no password 
ENV DEBIAN_FRONTEND noninteractive 
RUN apt-get install -y mysql-server mysql-client 

RUN service mysql start 
RUN mysqladmin -u root create mydb 

CMD [ "bash" ] 

然而,當我通過docker build -t mysql-test .我收到以下錯誤構建映像:

Step 7 : RUN mysqladmin -u root create mydb 
---> Running in a35c3d176d4f 
mysqladmin: connect to server at 'localhost' failed 
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)' 
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists! 
The command '/bin/sh -c mysqladmin -u root create mydb' returned a non-zero code: 1 

當我的評論,我想創建數據庫行(RUN mysqladmin -u root create mydb )我可以構建映像,啓動它並從命令行執行mysqladmin命令:

# this works 
> docker run -it mysql-test 
[email protected]> service mysql start 
[email protected]> mysqladmin -u root create mydb 

爲什麼在創建da時出現錯誤Dockerfile中的tabase?謝謝!

+1

我相信這已經解決了。看到這裏http://stackoverflow.com/questions/29145370/docker-initialize-mysql-database-with-schema –

+0

可能重複的[碼頭 - 初始化MySQL數據庫與架構](http://stackoverflow.com/questions/29145370/docker-initialize-mysql-database-with-schema) –

+0

我試圖重新闡述一下這個問題:我沒有看到在其他問題中如何解決這個錯誤 – Michael

回答

0

RUN instruction的文件清楚:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

所以每次運行指令創建一個新的形象和啓動MySQL服務器並創建數據庫應在同一RUN指令組合:

RUN service mysql start && mysqladmin -u root create mydb 

這解決了這個問題。

相關問題