postgresql:dockerfile从入口点脚本创建用户和数据库
我有一份文件postgresql:dockerfile从入口点脚本创建用户和数据库,postgresql,docker,docker-entrypoint,Postgresql,Docker,Docker Entrypoint,我有一份文件 COPY ./docker-entrypoint.sh / RUN chmod +x /docker-entrypoint.sh USER postgres ENTRYPOINT ["/docker-entrypoint.sh"] 其中docker-entrypoint.sh为: #!/bin/sh # Before PostgreSQL can function correctly, the database cluster must be initialized: i
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
USER postgres
ENTRYPOINT ["/docker-entrypoint.sh"]
其中docker-entrypoint.sh为:
#!/bin/sh
# Before PostgreSQL can function correctly, the database cluster must be initialized:
initdb -D /var/lib/postgres/data
#start postgres server
/usr/bin/postgres -D /var/lib/postgres/data &
# create a user or role
psql -d postgres -c "CREATE USER someuser WITH PASSWORD 'jkhkjah';"
# create database
psql -d postgres -c "CREATE DATABASE dockertest OWNER 'someuser';"
它不会创建someuser和dockertest数据库。如何做到这一点查看
它有这样的工作流程:
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
USER postgres
ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
#!/bin/sh
# Before PostgreSQL can function correctly, the database cluster must be initialized:
initdb -D /var/lib/postgres/data
# internal start of server in order to allow set-up using psql-client
# does not listen on external TCP/IP and waits until start finishes
pg_ctl -D "/var/lib/postgres/data" -o "-c listen_addresses=''" -w start
# create a user or role
psql -d postgres -c "CREATE USER someuser WITH PASSWORD 'jkhkjah';"
# create database
psql -v ON_ERROR_STOP=1 -d postgres -c "CREATE DATABASE dockertest OWNER 'someuser';"
# stop internal postgres server
pg_ctl -v ON_ERROR_STOP=1 -D "/var/lib/postgres/data" -m fast -w stop
exec "$@"
然后修改docker-entrypoint.sh,如下所示:
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
USER postgres
ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
#!/bin/sh
# Before PostgreSQL can function correctly, the database cluster must be initialized:
initdb -D /var/lib/postgres/data
# internal start of server in order to allow set-up using psql-client
# does not listen on external TCP/IP and waits until start finishes
pg_ctl -D "/var/lib/postgres/data" -o "-c listen_addresses=''" -w start
# create a user or role
psql -d postgres -c "CREATE USER someuser WITH PASSWORD 'jkhkjah';"
# create database
psql -v ON_ERROR_STOP=1 -d postgres -c "CREATE DATABASE dockertest OWNER 'someuser';"
# stop internal postgres server
pg_ctl -v ON_ERROR_STOP=1 -D "/var/lib/postgres/data" -m fast -w stop
exec "$@"
我认为你的主要错误是在后台启动postgresql
/usr/bin/postgres -D /var/lib/postgres/data &
并在服务器启动之前立即开始执行查询
另外,我建议在psql中添加
-v ON_ERROR\u STOP=1
参数,以查看详细信息,并在发生错误时停止进程您可以提供整个Dockerfile(或至少是基本映像)吗?它是如何工作的?它们是在启动期间还是在数据库日志中出错?或者它什么都不做,图像就工作了?图像工作了。如果还试图在后台启动postgres,但postgresql需要时间启动。那么,只有在postgresql启动后才能运行创建用户
和创建数据库
为什么我们同时启动和停止服务器。我需要运行postgres,因为我的django同事在使用docker Composer时需要访问。第一次启动只是“技术启动”,用于将数据库准备到必要的状态,此时,您的数据库还没有准备好接受连接,因为它的模式还不正确。第二个是“生产”开始,当模式准备好时,我们准备好接受连接