无法使用 docker compose 执行 rake rspec 单元测试

问题描述

当我对这个 rake 文件执行 rake spec 时,docker compose 出现,然后在执行单元测试之前关闭。 如果我删除了最后一个运行 docker-compose down 的命令,它工作正常

如何修改脚本,使其可以运行 docker-compose up --detach,然后是 run the unit tests,然后在单元测试完成后运行 docker-compose down

我尝试添加睡眠,但效果不佳。

在我的Rakefile:

Spec::Core::RakeTask.new(:spec)

# start docker compose.
# we use the system command to wait until docker compose is up and running before starting unit tests
system("docker-compose up --detach")

# run unit tests
task :default => :spec

# stop docker compose
system("docker-compose down")

解决方法

我认为您的问题不是关于 docker,而是关于 rake。 Rake(类似于make)允许你给一个任务添加依赖,我们可以使用依赖链接来安排任务的顺序:

task :docker_up do
 system("docker-compose up --detach")
end

task :docker_down do
 system("docker-compose down")
end

task :default => [:docker_up,:spec,:docker_down]

这里是您可以了解的主题 how to use rake

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...