2015-10-23 101 views
3

我的目標是擁有一個運行MySQL服務的docker容器。所以每當我啓動容器時,數據庫都處於初始狀態,MySQL正在運行。 幾乎一切都很好,但是當我啓動容器時,MySQL服務沒有運行。每次在控制檯中,我都必須像這樣啓動它:service mysql start。 下面是我如何構建和運行容器的步驟:如何構建一個運行MySQL的Docker容器?

$ docker build -t executer:mysql . 
Sending build context to Docker daemon 15.87 kB 
Sending build context to Docker daemon 
Step 0 : FROM debian:wheezy 
# 
# ... Many steps without error 
# 
Step 17 : RUN mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/ 
---> Using cache 
---> 1c71bf4524f0 
Step 18 : RUN service mysql start 
---> Running in b4643765b79b 

...... 
MySQL Community Server 5.6.24 is started. 
---> ac26b749a3c0 
Removing intermediate container b4643765b79b 
Successfully built ac26b749a3c0 

$ docker run --rm -it executer:mysql /bin/bash 
[email protected]:/# mysql 
ERROR 2002 (HY000): Cant connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) 
[email protected]:/# service mysql start 
No directory, logging in with HOME=/ 
...... 
[info] MySQL Community Server 5.6.24 is started. 
[email protected]:/# mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 1 
Server version: 5.6.24 MySQL Community Server (GPL) 

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 

mysql> 

這裏是Dockerfile的內容:我應該如何改變我的Dockerfile有運行後,運行MySQL服務

FROM debian:wheezy 

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added 
RUN groupadd -r mysql && useradd -r -g mysql mysql 

# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: 
# File::Basename 
# File::Copy 
# Sys::Hostname 
# Data::Dumper 
RUN apt-get update && apt-get install -y perl --no-install-recommends && rm -rf /var/lib/apt/lists/* 

# gpg: key 5072E1F5: public key "MySQL Release Engineering <[email protected]>" imported 
RUN apt-key adv --keyserver pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 

ENV MYSQL_MAJOR 5.6 
ENV MYSQL_VERSION 5.6.24 

RUN echo "deb http://repo.mysql.com/apt/debian/ wheezy mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list 

# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) 
# also, we set debconf keys to make APT a little quieter 
RUN { \ 
     echo mysql-community-server mysql-community-server/data-dir select ''; \ 
     echo mysql-community-server mysql-community-server/root-pass password ''; \ 
     echo mysql-community-server mysql-community-server/re-root-pass password ''; \ 
     echo mysql-community-server mysql-community-server/remove-test-db select false; \ 
    } | debconf-set-selections \ 
    && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* && rm -rf /var/lib/apt/lists/* \ 
    && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql 

RUN apt-get update && apt-get install -y procps 

# comment out a few problematic configuration values 
RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf 

# VOLUME /var/lib/mysql 
# Install database 
ADD ./database.sql /var/db/database.sql 

# Set Standard settings 
ENV user student 
ENV password secret 
ENV url file:/var/db/database.sql 
ENV right READ 

# Install starting script 
ADD ./start-database.sh /usr/local/bin/start-database.sh 
RUN chmod +x /usr/local/bin/start-database.sh 

#EXPOSE 3306 

RUN mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/ 

RUN service mysql start 

我的容器?

+1

看到這個答案:http://stackoverflow.com/a/25150809/1309035 '運行'在編譯期間,'CMD'在運行時間 – michaelbahr

+0

可能重複的[如何在運行時自動啓動服務碼頭容器?](http://stackoverflow.com/questions/25135897/how-to-automatically-start-a-service-when-running-a-docker-container) – michaelbahr

回答

2

我想你的設置

看到https://github.com/BITPlan/docker-stackoverflowanswers/tree/master/33299303

首先我得到

E: Version '5.6.24*' for 'mysql-server' was not found 

這與你的問題無關,但需要修復。 所以我註釋掉了大部分的Dockerfile的補充道:

RUN apt-get install mysql-server-5.5 

這些都給: E:無法找到包的MySQL服務器-5。5

這是發行特定所以我改變了FROM線

FROM ubuntu:14.04 

其中我比較熟悉

然後我修改Dockerfile改變

RUN service mysql start 

CMD service mysql start 

在michaelbahr的評論中提及了與

./run 

[email protected]:/# service --status-all 2>&1 | grep mysql 

檢查結果顯示:

[ ? ] mysql 

有你 - mysql的運行。 您可能需要相應地修復Dockerfile。

+0

對我來說,這個解決方案引入了另一個問題。在安裝過程中,我必須提供密碼。 但是我可以這樣修復: 'RUN DEBIAN_FRONTEND =非交互式apt-get -y安裝mysql-server' –

+0

你也可以通過Dockerfile頂部的'ENV DEBIAN_FRONTEND noninteractive'設置非交互式前端 – James

1

另一種解決方案是使用MySQL的碼頭工人形象: https://hub.docker.com/_/mysql/

所有你需要做的是:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag 

其中:

  • some-mysql是你想要的名稱分配給您的貨櫃

  • my-secret-pw是MySQL root用戶

  • tag要設置的密碼標籤指定MySQL版本

相關問題