扩展 CouchDB Docker 镜像 [英] Extending CouchDB Docker image
问题描述
我正在尝试扩展 CouchDB docker 映像以预填充 CouchDB(包含初始数据库、设计文档等).
I’m trying to extend CouchDB docker image to pre-populate CouchDB (with initial databases, design documents, etc.).
为了创建一个名为db
的数据库,我首先尝试了这个初始的Dockerfile
:
In order to create a database named db
, I first tried this initial Dockerfile
:
FROM couchdb
RUN curl -X PUT localhost:5984/db
但由于 couchdb 服务在构建时尚未启动,因此构建失败.于是我把它改成了这样:
but the build failed since couchdb service is not yet started at build time. So I changed it into this:
FROM couchdb
RUN service couchdb start &&
sleep 3 &&
curl -s -S -X PUT localhost:5984/db &&
curl -s -S localhost:5984/_all_dbs
注意:
sleep
是我发现使它工作的唯一方法,因为它不适用于 curl 选项--connect-timeout
,- 第二个
curl
只是为了检查数据库是否已经创建.
- the
sleep
was the only way I found to make it work, since it did not work with curl option--connect-timeout
, - the second
curl
is only to check that the database was created.
构建似乎工作正常:
$ docker build . -t test3 --no-cache
Sending build context to Docker daemon 6.656kB
Step 1/2 : FROM couchdb
---> 7f64c92d91fb
Step 2/2 : RUN service couchdb start && sleep 3 && curl -s -S -X PUT localhost:5984/db && curl -s -S localhost:5984/_all_dbs
---> Running in 1f3b10080595
Starting Apache CouchDB: couchdb.
{"ok":true}
["db"]
Removing intermediate container 1f3b10080595
---> 7d733188a423
Successfully built 7d733188a423
Successfully tagged test3:latest
奇怪的是,现在当我将它作为容器启动时,数据库 db
似乎没有保存到 test3
图像中:
What is weird is that now when I start it as a container, database db
does not seem to be saved into test3
image:
$ docker run -p 5984:5984 -d test3
b34ad93f716e5f6ee68d5b921cc07f6e1c736d8a00e354a5c25f5c051ec01e34
$ curl localhost:5984/_all_dbs
[]
推荐答案
大多数标准 Docker 数据库映像都包含一个 VOLUME
行,可防止使用预填充数据创建派生映像.对于 官方 couchdb
图片 你可以看到 其 Dockerfile
中的相关行.与关系数据库映像不同,此映像不支持在首次启动时运行的脚本.
Most of the standard Docker database images include a VOLUME
line that prevents creating a derived image with prepopulated data. For the official couchdb
image you can see the relevant line in its Dockerfile
. Unlike the relational-database images, this image doesn’t have any support for scripts that run at first startup.
这意味着您需要从主机或另一个容器进行初始化.如果您可以使用它的 HTTP API 直接与它进行交互,那么这可能看起来像:
That means you need to do the initialization from the host or from another container. If you can directly interact with it using its HTTP API, then this could look like:
# Start the container
docker run -d -p 5984:5984 -v ... couchdb
# Wait for it to be up
for i in $(seq 20); do
if curl -s http://localhost:5984 >/dev/null 2>&1; then
break
fi
sleep 1
done
# Create the database
curl -XPUT http://localhost:5984/db
这篇关于扩展 CouchDB Docker 镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!