gpt4 book ai didi

docker - 创建自定义docker postgresql镜像并修改它的entryPoint

转载 作者:行者123 更新时间:2023-12-02 19:24:03 27 4
gpt4 key购买 nike

我试图从postgres:11.5-alpine创建自定义图像,所以我做了这个dockerfile:

FROM postgres:11.5-alpine

LABEL MAINTAINER groot
ENV LANG en_US.utf8
ENV DBNAME pglocations
ENV USERNAME postgres

COPY init.sql /docker-entrypoint-initdb.d/

这是init.sql文件:
/*--psql -U posgtres -p 5432
psql -U <username> -d <dbname> -1 -f <filename>.sql*/
psql -U posgtres -p 5432
CREATE DATABASE pglocations
WITH OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'English_United States.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
TEMPLATE template0;
/*psql -U posgtres -d pglocations -1 -f pglocations.sql*/

我通过以下命令制作图像: docker build -t safagress:11.5 . 这是图像列表:
PS D:\Software\Windows\Docker\Images\AndroidService> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
safagress 11.5 26d34fa7c2c1 About an hour ago 71.9MB
postgres 11.5-alpine 78b21f6420c0 5 days ago 71.9MB
alpine latest 961769676411 5 days ago 5.58MB
dpage/pgadmin4 latest 489972d75226 6 days ago 248MB

并制作容器:
docker run -d --name=pg-docker -p 5433:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=s123 safagres
和容器 list :
PS D:\Software\Windows\Docker\Images\AndroidService> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a6bdf49312 safagress:11.5 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5433->5432/tcp pg-docker
3de509771297 dpage/pgadmin4 "/entrypoint.sh" 2 hours ago Up 2 hours 443/tcp, 127.0.0.1:8081->80/tcp priceless_black
PS D:\Software\Windows\Docker\Images\AndroidService>

1- 当我创建容器时,出现此错误:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
2019-08-26 05:25:55.650 UTC [39] ERROR: syntax error at or near "psql" at character 88
2019-08-26 05:25:55.650 UTC [39] STATEMENT: /*--psql -U posgtres -p 5432
psql -U <username> -d <dbname> -1 -f <filename>.sql*/

psql -U posgtres -p 5432

CREATE DATABASE pglocations
WITH OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'English_United States.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
TEMPLATE template0;
psql:/docker-entrypoint-initdb.d/init.sql:12: ERROR: syntax error at or near "psql"
LINE 4: psql -U posgtres -p 5432

2-如何将 ENV DBNAME pglocations例如从dockerfile传递到init.sql以使用其值?

最佳答案

您的问题在于,您在SQL文件中使用了shell命令(psql -U ...),这会在图像的入口点尝试运行它时导致语法错误。

尝试将这样的文件放在您的initdb.d文件夹中,并将其命名为init.sh:

#!/bin/sh
set -e

psql -v ON_ERROR_STOP=1 --dbname template1 --username postgres <<-EOSQL
CREATE DATABASE "$DBNAME"
WITH OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'English_United States.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
TEMPLATE template0;
EOSQL

除此之外,还可以在其 DockerHub site上找到有关如何使用Postgres Docker镜像(及其环境变量)的良好说明。

关于docker - 创建自定义docker postgresql镜像并修改它的entryPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653050/

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