如何使用填充数据库进行泊坞窗图像进行自动测试?(How to make a docker image with a populated database for automated tests?)

463 IT屋

I want to create containers w/ a MySQL db and a dump loaded for integration tests. Each test should connect to a fresh container, with the DB in the same state. It should be able to read and write, but all changes should be lost when the test ends and the container is destroyed. I'm using the "mysql" image from the official docker repo.

1) The image's docs suggests taking advantage of the "entrypoint" script that will import any .sql files you provide on a specific folder. As I understand, this will import the dump again every time a new container is created, so not a good option. Is that correct?

2) This SO answer suggests extending that image with a RUN statement to start the mysql service and import all dumps. This seems to be the way to go, but I keep getting

mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

followed by

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

when I run build, even though I can connect to mysql fine on containers of the original image. I tried sleep 5 to wait for the mysqld service to startup, and adding -h with 'localhost' or the docker-machine ip.

How can I fix "2)"? Or, is there a better approach?

解决方案

If re-seeding the data is an expensive operation another option would be starting / stopping a Docker container (previously build with the DB and seed data). I blogged about this a few months ago Integration Testing using Spring Boot, Postgres and Docker and although the blog focuses on Postgres, the idea is the same and could be translated to MySQL.

我想创建一个容器,带有一个MySQL数据库和一个加载用于集成测试的转储。每个测试应连接到一个新的容器,DB处于相同的状态。它应该能够读写,但是当测试结束并且容器被破坏时,所有更改都应该丢失。我正在使用官方Docker repo中的" mysql "图像。



1)图片的文档建议利用"entrypoint"脚本,将导入您在特定文件夹中提供的任何.sql文件。据了解,这将在每次创建新容器时再次导入转储,所以不是一个很好的选择。这是正确的吗?



2)此 SO答案表明延伸该映像使用RUN语句启动mysql服务并导入所有转储。这似乎是要走的路,但是我从pid文件/var/run/mysqld/mysqld.pid结束了



mysqld_safe mysqld



后跟



ERROR 2002(HY000) :无法连接到本地MySQL服务器通过套接字'/var/run/mysqld/mysqld.sock'(2)



当我运行构建,即使我可以连接到原始图像的容器上的mysql罚款。我尝试 sleep 5 等待mysqld服务启动,并使用'localhost'或docker-machine ip添加-h。



如何修复"2"?或者,是否有更好的方法?


解决方案

如果重新播种数据是一项昂贵的操作,另一个选项将是启动/停止一个 Docker 容器(以前使用数据库和种子数据构建)。我在几个月前就发表了这个话题本文地址:IT屋 » 如何使用填充数据库进行泊坞窗图像进行自动测试?