如何仅使用 docker-compose.yml 创建数据容器? [英] how can I create a data-container only using docker-compose.yml?
问题描述
这个问题来自 Docker 存储库的一个问题:
https://github.com/docker/compose/issues/942
我不知道如何使用 docker compose 创建数据容器(没有进程运行).
更新:过去几年情况发生了变化.请参阅@Frederik Wendt 的回答,以获得良好且最新的解决方案.
我的旧答案:具体怎么做在一定程度上取决于您为纯数据容器使用的图像.如果你的图像有一个 entrypoint
,你需要在你的 docker-compose.yml
中覆盖它.例如,这是来自 docker hub 的官方 MySql 镜像的解决方案:
数据库数据:图像:mysql:5.6.25入口点:/bin/bash数据库服务器:图像:mysql:5.6.25卷来自:- 数据库数据环境:MYSQL_ROOT_PASSWORD: blabla
当你在上面执行 docker-compose up
时,你会得到一个像 ..._DatabaseData_1
这样的容器,它显示了 Exited
的状态> 当您调用 docker ps -a
时.使用 docker inspect
进一步调查将显示,它的时间戳为 0
.这意味着容器从未运行过.就像 docker 的所有者所说的那样,请在 此处.>
现在,只要您不执行 docker-compose rm -v
,您的纯数据容器 (..._DatabaseData_1
) 就不会丢失其数据.因此,您可以根据需要随时执行 docker-compose stop
和 docker-compose up
.
如果您喜欢使用像 tianon/true
这样的专用数据图像,这也是一样的.这里不需要覆盖entrypoint
,因为它不存在.该图像和 docker compose 似乎存在一些问题.我没试过,但是 这篇文章 值得一读,以防您遇到任何问题.
一般来说,为纯数据容器使用与用于访问它的容器相同的图像似乎是个好主意.有关更多详细信息,请参阅仅限数据的容器疯狂.
This question is coming from an issue on the Docker's repository:
https://github.com/docker/compose/issues/942
I can't figure it out how to create a data container (no process running) with docker compose.
UPDATE: Things have changed in the last years. Please refer to the answer from @Frederik Wendt for a good and up-to-date solution.
My old answer: Exactly how to do it depends a little on what image you are using for your data-only-container. If your image has an entrypoint
, you need to overwrite this in your docker-compose.yml
. For example this is a solution for the official MySql image from docker hub:
DatabaseData:
image: mysql:5.6.25
entrypoint: /bin/bash
DatabaseServer:
image: mysql:5.6.25
volumes_from:
- DatabaseData
environment:
MYSQL_ROOT_PASSWORD: blabla
When you do a docker-compose up
on this, you will get a container like ..._DatabaseData_1
which shows a status of Exited
when you call docker ps -a
. Further investigation with docker inspect
will show, that it has a timestamp of 0
. That means the container was never run. Like it is stated by the owner of docker compose here.
Now, as long as you don't do a docker-compose rm -v
, your data only container (..._DatabaseData_1
) will not loose its data. So you can do docker-compose stop
and docker-compose up
as often as you like.
In case you like to use a dedicated data-only image like tianon/true
this works the same. Here you don't need to overwrite the entrypoint
, because it doesn't exist. It seems like there are some problems with that image and docker compose. I haven't tried it, but this article could be worth reading in case you experience any problems.
In general it seems to be a good idea to use the same image for your data-only container that you are using for the container accessing it. See Data-only container madness for more details.
这篇关于如何仅使用 docker-compose.yml 创建数据容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!