[英]How to bundle a postgres db and docker image
USE-CASE: The use case is that you have say a small 'example' database that you want to provide to a user to practice/test apps with.用例:用例是你有一个小的“示例”数据库,你想提供给用户来练习/测试应用程序。 The user doesn't have postgres installation but they do have Docker.
用户没有安装 postgres,但他们确实有 Docker。 You want to provide the user with a painless way to access your tailored db without them having to install postgres.
您希望为用户提供一种轻松的方式来访问您定制的数据库,而无需他们安装 postgres。 You can provide users with exactly the same db and easily update it when required
您可以为用户提供完全相同的数据库,并在需要时轻松更新
PROPOSED SOLUTION: I would like to bundle a postgres db and postgres docker image so that I could give a user say a tar.gz and (assuming they have Docker) they could unpack the tar.gz and have a running postgres db that they could connect to.建议的解决方案:我想捆绑一个 postgres db 和 postgres docker 图像,这样我就可以给用户说一个 tar.gz 并且(假设他们有 Docker)他们可以解压缩 tar.gz 并有一个正在运行的 postgres db,他们可以连接至。
I have at least two challenges:我至少有两个挑战:
Question: Does this make sense?问:这有意义吗? If so, am I going about it the right way or am I missing something?
如果是这样,我是以正确的方式去做还是我错过了什么?
POSTMORTEM:验尸:
In the end and building off @davidmaze reply, the following was successful.最后并建立了@davidmaze 回复,以下是成功的。
version: "3"
services:
postgres:
image: postgres:12
container_name: testdb_container
volumes:
- ./pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5555:5432
volumes:
pgdata:
With the directory structure looking like this.目录结构看起来像这样。
I haven't been able to successfully name the db anything else but 'postgres' but that's okay.除了“postgres”之外,我无法成功地将数据库命名为其他任何名称,但这没关系。
I tar.gz the 'test-comp' directory after I've created the db and populated it.在我创建数据库并填充它之后,我 tar.gz 'test-comp' 目录。 The recipient unpacks it and runs 'start.sh' which is just a start message and 'docker-compose up -d'.
收件人将其解压缩并运行“start.sh”,这只是一条启动消息和“docker-compose up -d”。 The db is shut down with 'stop.sh'.
db 使用“stop.sh”关闭。
The 'init.sql' is set to produce an empty dB with a preset schema. 'init.sql' 设置为使用预设模式生成空分贝。 I deliver a populated dB but if the person wants to start fresh then they can just empty the directory (best to delete and create a new empty one) and running 'start.sh' will invoke 'init.sql'.
我提供了一个填充的分贝,但如果这个人想重新开始,那么他们可以清空目录(最好删除并创建一个新的空目录)并运行“start.sh”将调用“init.sql”。
If you're comfortable with distributing a tar file as the basis for this, you can include a working Docker Compose setup and its data.如果您对分发 tar 文件作为此基础感到满意,您可以包含一个有效的 Docker Compose 设置及其数据。 The
docker-compose.yml
file can be a very typical Compose setup: docker-compose.yml
文件可以是非常典型的 Compose 设置:
version: '3.8'
services:
postgres:
image: postgres:12
volumes:
- ./pgdata:/var/lib/postgresql/data
# ports: ['5432:5432']
app: { ... }
The volumes:
block tells Docker to mount the local pgdata
directory into the container on the PostgreSQL data path. volumes:
块告诉 Docker 将本地pgdata
目录挂载到 PostgreSQL 数据路径上的容器中。 The host content will replace that directory in the image, so you can distribute a preloaded data directory.主机内容将替换图像中的该目录,因此您可以分发预加载的数据目录。 Conversely, when the database writes back to the image, it will overwrite the host directory content and those changes will be persisted for future runs (but you can always delete it and start over from the tar file).
相反,当数据库写回映像时,它将覆盖主机目录内容,并且这些更改将保留以供将来运行(但您始终可以将其删除并从 tar 文件重新开始)。
tar cf myapp.tar \
myapp/docker-compose.yml \
myapp/pgdata
This doesn't redistribute the postgres
image per se , it just reuses the standard Docker Hub image.这不会重新分发
postgres
映像本身,它只是重用标准 Docker Hub 映像。 Unless you're in an isolated network environment this shouldn't be a practical problem.除非您处于孤立的网络环境中,否则这不应该是一个实际问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.