"正确"在Docker中管理数据库模式的方式 [英] "correct" way to manage database schemas in docker

查看:110
本文介绍了"正确"在Docker中管理数据库模式的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发由Java Web应用程序和postgresql数据库组成的开源应用程序。理想情况下,它将可部署类似于造船厂快速入门中详细介绍的流程:


  1. 运行仅数据容器

  2. 运行数据库容器

  3. 运行应用程序容器

有建议的时间来设置数据库模式吗?我正在考虑使数据库映像的Dockerfile在构建时创建模式,但是postgres在这个时候不会运行。

解决方案

我们使用Postgres和Docker,我在这里工作,我们结束了以下操作:


  1. 从官方Postgres repo复制Dockerfile所以你可以制作自己的图片。

  2. 修改docker-entrypoint.sh( https://github.com/docker-library/postgres/blob/8f80834e934b7deaccabb7bf81876190d72800f8/9.4/docker-entrypoint.sh ),这是什么叫什么时候容器启动。

在docker-entrypoint.sh的顶部,我放了以下内容:

 #获取架构
url = $(curl -s -u $ {GIT_USER}:$ {GIT_PASSWORD}$ {SQL_SCRIPT_URL}| python -c'import sys,json; print json.load(s ys.stdin)[download_url]')
curl $ {url}> db.sh
chmod + x db.sh
cp db.sh ./docker-entrypoint-initdb.d

这个基本上从Github下载一个shell脚本来初始化数据库的架构。我们这样做来管理模式的版本,所以当您启动容器时,您可以通过ENV变量来指出要使用的模式。



有关代码的一些注释:


  1. 我们需要重构使用私钥而不是用户凭据从Github中提取东西。

  2. ./docker-entrypoint-initdb.d目录是一个docker-entrypoint.sh将运行init数据库的脚本的地方。您可以将文件移动到该位置,但您想要的。如果从Github下载不适用,请执行此操作。


I'm developing an open source application consisting of a Java web application and a postgresql database. Ideally it would be deployable similar to the process detailed in the shipyard quickstart:

  1. run a data-only container
  2. run the DB container
  3. run the application container

Is there a recommended time to set up the database schema? I was thinking on making the Dockerfile for the database image create the schema when it is built but postgres isn't running at this time obviously.

解决方案

We use Postgres and Docker where I work and we ended up doing the following:

  1. Copy the Dockerfile from the official Postgres repo so you can make your own image.
  2. Modify docker-entrypoint.sh (https://github.com/docker-library/postgres/blob/8f80834e934b7deaccabb7bf81876190d72800f8/9.4/docker-entrypoint.sh), which is what is called when the container starts.

At the top of docker-entrypoint.sh, I put in the following:

# Get the schema
url=$(curl -s -u ${GIT_USER}:${GIT_PASSWORD} "${SQL_SCRIPT_URL}" | python -c 'import sys, json; print json.load(sys.stdin)["download_url"]')
curl ${url} > db.sh
chmod +x db.sh
cp db.sh ./docker-entrypoint-initdb.d

This basically downloads a shell script from Github that initializes the schema for the database. We do this to manage versions of the schema, so when you start your container you can tell it which schema to use via an ENV variable.

Some notes about the code:

  1. We need to refactor to pull stuff from Github using a private key instead of user credentials.
  2. The ./docker-entrypoint-initdb.d directory is a place where docker-entrypoint.sh will look to run init scripts for the database. You can move files to that location however you want. Do this if downloading from Github is not applicable.

这篇关于"正确"在Docker中管理数据库模式的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆