gpt4 book ai didi

postgresql - 如何在 docker Alpine Linux 容器中运行 Postgres?

转载 作者:IT老高 更新时间:2023-10-28 21:42:58 28 4
gpt4 key购买 nike

我正在尝试让 Postgresql 10.0 在 docker 容器中工作。我有以下 Dockerfile:

FROM postgres:10.0-alpine

RUN apk add openrc --no-cache

USER postgres

RUN /etc/init.d/postgresql start
RUN psql --command "IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = 'user') THEN CREATE USER user WITH SUPERUSER ENCRYPTED PASSWORD 'password'; END IF;"
RUN createdb main

EXPOSE 5432

VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

CMD ["/usr/lib/postgresql/10.0/bin/postgres", "-D", "/var/lib/postgresql/10.0/main", "-c", "config_file=/etc/postgresql/10.0/main/postgresql.conf"]

我收到以下错误:

/bin/sh: /etc/init.d/postgresql: not found

似乎 /etc/init.d/postgresql 真的不见了。我做错了什么?

最佳答案

我完成了这个 Dockerfile:

FROM postgres:10.0-alpine

USER postgres

RUN chmod 0700 /var/lib/postgresql/data &&\
initdb /var/lib/postgresql/data &&\
echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf &&\
echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf &&\
pg_ctl start &&\
psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'main'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE main" &&\
psql -c "ALTER USER postgres WITH ENCRYPTED PASSWORD 'mysecurepassword';"

EXPOSE 5432

此 dockerfile 创建数据库'main'(如果不存在),启动 postgres 并设置默认用户密码

关于postgresql - 如何在 docker Alpine Linux 容器中运行 Postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981073/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com