如何在容器内创建postgres扩展? [英] How to create postgres extension inside the container?
问题描述
我必须在docker postgres中安装 hstore
这是我在shell中执行我的需要的普通命令
I have to install hstore
to my docker postgres
Here is my ordinary command in the shell to execute my need
psql -d template1 -c 'create extension hstore';
如果我删除容器中的那行,它可以工作,但是我已经执行了 hstore
我自己安装,我必须告诉项目中的每个人都这样做,但这不是一个好习惯
If I remove that line my container, it works, but I have execute hstore
installation by myself and I have to tell everyone in my project to do so which is not a good practice
这是我的 yml文件
postgres:
build:
context: .
dockerfile: dockerfiles/devdb.dockerfile
environment:
POSTGRES_USER: uih
POSTGRES_PASSWORD: uIhbod!
POSTGRES_DB: uih_portal
ports:
- "5433:5432"
这是我的docker文件 devdb.dockerfile
Here is my docker file devdb.dockerfile
FROM postgres:9.5
RUN mkdir -p /var/lib/postgresql-static/data
ENV PGDATA /var/lib/postgresql-static/data
# psql -d template1 -c 'create extension hstore;'
CMD ["psql", "-d", "template1", "-c", "'create extension hstore;'"]
RUN echo "hstore extension installed"
构建后,我无法运行它
After build I can not get it run
$ docker-compose up postgres
Recreating uihportal_postgres_1
Attaching to uihportal_postgres_1
postgres_1 | psql: could not connect to server: No such file or directory
postgres_1 | Is the server running locally and accepting
postgres_1 | connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
uihportal_postgres_1 exited with code 2
问题:
如何从dockerfile安装 hstore
?
我要制作映像并重复使用对于我团队的整个项目来说都是这样
How to install hstore
from the dockerfile?
I want to make an image and re-use it for the entire project for my team
推荐答案
失败了,因为在构建过程中Postgres不在容器中运行,它只是在容器运行时从 CMD
开始。
It's failing because Postgres isn't running in the container during the build, it's only started in the CMD
when a container runs.
Docker映像的入口点脚本支持运行设置步骤-任意.sql或.sh文件 /docker-entrypoint-initdb.d
目录将在容器启动时执行。
The entrypoint script for the Docker image has support for running setup steps - any .sql or .sh files in the /docker-entrypoint-initdb.d
directory will be executed when the container starts.
因此您可以执行通过将扩展程序设置放入SQL脚本中,然后将该脚本复制到init目录中的映像中,即可:
So you can do this by putting your extension setup in a SQL script, and copying the script into the image in the init directory:
> cat hstore.sql
create extension hstore
> cat Dockerfile
FROM postgres:9.5
COPY hstore.sql /docker-entrypoint-initdb.d
构建该映像时,SQL脚本将在正确的位置执行,因此,每当从该映像运行容器时,它将安装扩展名。
When you build that image, the SQL script will be in the right place to be executed, so whenever a container runs from the image it will install the extension.
这篇关于如何在容器内创建postgres扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!