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

查看:573
本文介绍了如何使用填充数据库进行泊坞窗图像进行自动测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

后跟

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

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

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.

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

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

推荐答案

如果重新播种数据是一项昂贵的操作,另一个选项将是启动/停止一个 Docker 容器(以前使用数据库和种子数据构建)。我在几个月前就发表了这个话题 Postgres ,但是这个想法是一样的,可以翻译成的MySQL

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.

这篇关于如何使用填充数据库进行泊坞窗图像进行自动测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆