1

I am trying to create custom image from postgres:11.5-alpine so i made this 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/

This is init.sql file:

/*--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*/

I make image by this command: docker build -t safagress:11.5 . This is image list:

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

and make container :

docker run -d --name=pg-docker -p 5433:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=s123 safagres

and container 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- When i am creating container i got this error:

/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- How can i pass for example ENV DBNAME pglocations from dockerfile to init.sql to use it's value?

1
  • Have you tried $DBNAME? Commented Aug 26, 2019 at 6:59

1 Answer 1

1

Your problem here is that you use shell commands (psql -U ...) in a SQL file, which leads to syntax errors when the image's entrypoint tries to run it.

Try to put a file like this in your initdb.d folder instead and name it 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

Other than that, a good instruction to how to use the Postgres Docker image (and its environment variables, too) can be found on its DockerHub site.

Sign up to request clarification or add additional context in comments.

3 Comments

--dbname and --username are replaced with ENV DBNAME and ENV USERNAME? @bellackn
I replaced COPY command in dockerfile with COPY init.sh /docker-entrypoint-initdb.d/ and by using of your code when i make container i got this error:server started /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh /usr/local/bin/docker-entrypoint.sh: /docker-entrypoint-initdb.d/init.sh: /bin/sh^M: bad interpreter: No such file or directory @bellackn
What you declare as ENV in your Dockerfile can be retrieved with $VARIABLE in your entrypoint or anywhere else in your container later. The error you get indicates that your file has CRLF line endings, when it needs LF line endings, probably because you are on Windows. Try to run dos2unix on your file before building the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.