如何在Docker容器中初始化一个带有模式的MySQL数据库?

我正试图创建一个带有MySQL数据库的容器,并向这些数据库添加一个模式。

我目前的Docker文件是。

FROM mysql
MAINTAINER  (me) <email>

# Copy the database schema to the /data directory
COPY files/epcis_schema.sql /data/epcis_schema.sql

# Change the working directory
WORKDIR data

CMD mysql -u $MYSQL_USER -p $MYSQL_PASSWORD $MYSQL_DATABASE < epcis_schema.sql

为了创建容器,我按照Docker上提供的文档,执行了这个命令。

docker run --name ${CONTAINER_NAME} -e MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} -e MYSQL_USER=${DB_USER} -e MYSQL_PASSWORD=${DB_USER_PASSWORD} -e MYSQL_DATABASE=${DB_NAME} -d mvpgomes/epcisdb

但是当我执行这个命令时,容器没有被创建,在容器状态中可以看到CMD没有被成功执行,实际上只有mysql命令被执行。

总之,有没有办法用模式来初始化数据库,或者我需要手动执行这些操作?

解决办法

我很抱歉给你这个超长的答案,但是,你还有一段路要走,才能达到你想要的效果。 我想说的是,通常情况下,你不会把数据库的存储放在与数据库本身相同的容器中,你会装载一个主机卷,以便数据在docker主机上持续存在,或者,也许可以使用一个容器来保存数据(/var/lib/mysql)。 另外,我是mysql的新手,所以,这可能不是非常有效。这就是说...

我认为这里可能有几个问题。 Dockerfile是用来创建一个镜像的。 你需要执行构建步骤。 至少,从包含Docker文件的目录中,你可以做一些事情,比如:

docker build .

Dockerfile描述了要创建的镜像。 我对mysql了解不多(我是一个postgres粉丝),但是,我在互联网上搜索了一下'我如何初始化mysql docker容器'。 首先,我创建了一个新的目录来工作,我称之为mdir,然后我创建了一个文件目录,我把epcis_schema.sql文件存入其中,它创建了一个数据库和一个表。

create database test;
use test;

CREATE TABLE testtab
(
id INTEGER AUTO_INCREMENT,
name TEXT,
PRIMARY KEY (id)
) COMMENT='this is my test table';

然后我在files目录下创建了一个名为init_db的脚本。

#!/bin/bash

# Initialize MySQL database.
# ADD this file into the container via Dockerfile.
# Assuming you specify a VOLUME ["/var/lib/mysql"] or `-v /var/lib/mysql` on the `docker run` command…
# Once built, do e.g. `docker run your_image /path/to/docker-mysql-initialize.sh`
# Again, make sure MySQL is persisting data outside the container for this to have any effect.

set -e
set -x

mysql_install_db

# Start the MySQL daemon in the background.
/usr/sbin/mysqld &
mysql_pid=$!

until mysqladmin ping >/dev/null 2>&1; do
  echo -n "."; sleep 0.2
done

# Permit root login without password from outside container.
mysql -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION"

# create the default database from the ADDed file.
mysql < /tmp/epcis_schema.sql

# Tell the MySQL daemon to shutdown.
mysqladmin shutdown

# Wait for the MySQL daemon to exit.
wait $mysql_pid

# create a tar file with the database as it currently exists
tar czvf default_mysql.tar.gz /var/lib/mysql

# the tarfile contains the initialized state of the database.
# when the container is started, if the database is empty (/var/lib/mysql)
# then it is unpacked from default_mysql.tar.gz from
# the ENTRYPOINT /tmp/run_db script

(这个脚本的大部分内容是从这里引用的:https://gist.github.com/pda/9697520)

下面是我创建的files/run_db脚本。

# start db

set -e
set -x

# first, if the /var/lib/mysql directory is empty, unpack it from our predefined db
[ "$(ls -A /var/lib/mysql)" ] && echo "Running with existing database in /var/lib/mysql" || ( echo 'Populate initial db'; tar xpzvf default_mysql.tar.gz )

/usr/sbin/mysqld

最后,Docker文件将它们全部绑定。

FROM mysql
MAINTAINER  (me) 

# Copy the database schema to the /data directory
ADD files/run_db files/init_db files/epcis_schema.sql /tmp/

# init_db will create the default
# database from epcis_schema.sql, then
# stop mysqld, and finally copy the /var/lib/mysql directory
# to default_mysql_db.tar.gz
RUN /tmp/init_db

# run_db starts mysqld, but first it checks
# to see if the /var/lib/mysql directory is empty, if
# it is it is seeded with default_mysql_db.tar.gz before
# the mysql is fired up

ENTRYPOINT "/tmp/run_db"

所以,我cd'ed到我的mdir目录(其中有Dockerfile和文件目录)。然后我运行了这个命令。

docker build --no-cache .

你应该看到这样的输出。

Sending build context to Docker daemon 7.168 kB
Sending build context to Docker daemon 
Step 0 : FROM mysql
 ---> 461d07d927e6
Step 1 : MAINTAINER (me) 
 ---> Running in 963e8de55299
 ---> 2fd67c825c34
Removing intermediate container 963e8de55299
Step 2 : ADD files/run_db files/init_db files/epcis_schema.sql /tmp/
 ---> 81871189374b
Removing intermediate container 3221afd8695a
Step 3 : RUN /tmp/init_db
 ---> Running in 8dbdf74b2a79
+ mysql_install_db
2015-03-19 16:40:39 12 [Note] InnoDB: Using atomics to ref count buffer pool pages
...
/var/lib/mysql/ib_logfile0
 ---> 885ec2f1a7d5
Removing intermediate container 8dbdf74b2a79
Step 4 : ENTRYPOINT "/tmp/run_db"
 ---> Running in 717ed52ba665
 ---> 7f6d5215fe8d
Removing intermediate container 717ed52ba665
Successfully built 7f6d5215fe8d

你现在有一个图像'7f6d5215fe8d'。 我可以运行这个图像。

docker run -d 7f6d5215fe8d

并且图像开始,我看到一个实例字符串。

4b377ac7397ff5880bc9218abe6d7eadd49505d50efb5063d6fab796ee157bd3

然后我可以'停止'它,并重新启动它。

docker stop 4b377
docker start 4b377

如果你看一下日志,第一行会包含。

docker logs 4b377

Populate initial db
var/lib/mysql/
...

然后,在日志的最后。

Running with existing database in /var/lib/mysql

这些是来自/tmp/run_db脚本的信息,第一个表明数据库是从保存的(初始)版本中解压的,第二个表明数据库已经在那里了,所以使用了现有的副本。

下面是我上面描述的目录结构的ls -lR。 注意,init_db和run_db是设置了execute位的脚本。

gregs-air:~ gfausak$ ls -Rl mdir
total 8
-rw-r--r--  1 gfausak  wheel  534 Mar 19 11:13 Dockerfile
drwxr-xr-x  5 gfausak  staff  170 Mar 19 11:24 files

mdir/files:
total 24
-rw-r--r--  1 gfausak  staff   126 Mar 19 11:14 epcis_schema.sql
-rwxr-xr-x  1 gfausak  staff  1226 Mar 19 11:16 init_db
-rwxr-xr-x  1 gfausak  staff   284 Mar 19 11:23 run_db
评论(10)

我尝试了Greg的答案,但没有成功,我一定是做错了什么,因为我的数据库在所有步骤之后都没有数据。我使用的是MariaDB的最新镜像,只是为了以防万一。

然后我决定读取官方[MariaDB镜像]的入口(https://github.com/docker-library/mariadb/blob/c64262339972ac2a8dadaf8141e012aa8ddb8c23/10.1/docker-entrypoint.sh),并使用它来生成一个简单的*docker-compose*文件

database:
  image: mariadb
  ports:
     - 3306:3306
  expose:
     - 3306
  volumes:
     - ./docker/mariadb/data:/var/lib/mysql:rw
     - ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
  environment:
     MYSQL_ALLOW_EMPTY_PASSWORD: "yes"

现在,我能够坚持我的数据,并且用我自己的模式生成一个数据库!

评论(2)

最简单的解决方案是使用tutum/mysql

Step1

docker pull tutum/mysql:5.5

Step2

docker run -d -p 3306:3306 -v /tmp:/tmp  -e STARTUP_SQL="/tmp/to_be_imported.mysql" tutum/mysql:5.5

Step3

获取上述CONTAINER_ID,然后执行docker logs命令,查看生成的密码信息。

docker logs #
评论(6)