使用 docker-compose 时忽略容器退出

Ignore container exit when using docker-compose

我正在使用 docker-compose 设置测试基础设施。我想使用 docker-compose 选项 --exit-code-from 来 return 来自正在 运行ning 测试的容器的退出代码。但是,我还有一个容器 运行s 使用 sequelize cli 在我的数据库容器上进行迁移。当迁移完成时,此迁移容器以代码 0 退出,然后是我的测试 运行。这会导致 --exit-code-from--abort-on-container-exit 选项出现问题。迁移容器退出时有没有办法忽略?

--exit-code-from 表示 --abort-on-container-exit,根据 documentation

--abort-on-container-exit Stops all containers if any container was stopped.

不过你可以试试:

docker inspect <container ID> --format='{{.State.ExitCode}}'

您可以使用

获取所有(包括已停止)容器的列表
docker container ls -a

这是一个很好的例子:Checking the Exit Code of Stopped Containers

不要将 docker-compose up 用于 运行 one-off 任务。如文档所示,请改用 docker-compose run

The docker-compose run command is for running “one-off” or “adhoc” tasks. It requires the service name you want to run and only starts containers for services that the running service depends on. Use run to run tests or perform an administrative task such as removing or adding data to a data volume container. The run command acts like docker run -ti in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.

来源:https://docs.docker.com/compose/faq/

例如:

docker-compose build my_app
docker-compose run db_migrations # this starts the services it depends on, such as the db
docker-compose run my_app_tests