在 docker 容器中创建 postgres 扩展

ter*_*ywb 6 postgresql docker codenvy

我正在尝试使用以下 Dockerfile 在 Docker 容器中创建一个 postgres 实例。

FROM postgres

ENV POSTGRES_DB dspace
ENV POSTGRES_USER dspace
ENV POSTGRES_PASSWORD dspace

COPY init.sql /docker-entrypoint-initdb.d/
Run Code Online (Sandbox Code Playgroud)

这是我的 init.sql

create extension pgcrpyto
Run Code Online (Sandbox Code Playgroud)

我正在使用 Codenvy 服务来运行这个容器。初始化时,我看到以下错误。

[STDOUT] server started
[STDOUT] Reading package lists...
[STDOUT] Reading package lists...
[STDOUT] Building dependency tree...
[STDOUT] CREATE DATABASE
[STDOUT] 
[STDOUT] CREATE ROLE
[STDOUT] 
[STDOUT] 
[STDOUT] /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
[STDOUT] Reading state information...
[STDOUT] 2018-02-15 19:17:34.931 UTC [399] ERROR:  could not open extension control file "/usr/share/postgresql/10/extension/pgcrpyto.control": No such file or directory
[STDOUT] 2018-02-15 19:17:34.931 UTC [399] STATEMENT:  create extension pgcrpyto
[STDERR] psql:/docker-entrypoint-initdb.d/init.sql:1: ERROR:  could not open extension control file "/usr/share/postgresql/10/extension/pgcrpyto.control": No such file or directory
Run Code Online (Sandbox Code Playgroud)

请注意,我的解决方案基于以下帖子。如何在容器内创建 postgres 扩展?

ter*_*ywb 9

以下回购解决了我的问题。看来我需要提供一个shell脚本来执行SQL。

https://github.com/DSpace-Labs/DSpace-codenvy

文件

# From https://github.com/DSpace-Labs/dspace-dev-docker/tree/master/postgres
FROM postgres

ENV POSTGRES_DB dspace
ENV POSTGRES_USER dspace
ENV POSTGRES_PASSWORD dspace

COPY install-pgcrypto.sh /docker-entrypoint-initdb.d/
Run Code Online (Sandbox Code Playgroud)

安装-pgcrypto.sh

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
create extension pgcrypto;
EOSQL
Run Code Online (Sandbox Code Playgroud)