0

I have the following dockerfile (elided):

    FROM postgres:latest
  1 ARG VERSION
  2
  3 # install clojure
  4 RUN apt-get update
  5 RUN apt-get -y install curl
  6 RUN curl -O https://download.clojure.org/install/linux-install-1.10.3.933.sh
  7 RUN chmod +x linux-install-1.10.3.933.sh
  8 RUN ./linux-install-1.10.3.933.sh
  9
 10 # postgres
 11 RUN apt-get -y install sudo
 12 USER postgres
 13 RUN initdb
 14 RUN postgres -D /var/lib/postgresql/data
 15
 16 RUN psql -f bin/sql/postgres-db.sql -U postgres
 17 RUN psql -f bin/sql/postgres-table.sql -U postgres -d datomic
 18 RUN psql -f bin/sql/postgres-user.sql -U postgres -d datomic

But after running the command on line 14, the docker container stops executing the rest of the commands, which manually should be run in another shell while the postgres -D command is active. How do I run the postgres command in the background?

I tried & at the end of line 14 to send it in background, but get this:

[+] Building 4.4s (14/25)
 => [internal] load build definition from Dockerfile_db                    0.0s
 => => transferring dockerfile: 1.29kB                                     0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/postgres:latest         3.7s
 => [internal] load build context                                          0.0s
 => => transferring context: 175B                                          0.0s
 => [ 1/21] FROM docker.io/library/postgres:latest@sha256:6647385dd9ae11a  0.0s
 => CACHED [ 2/21] RUN apt-get update                                      0.0s
 => CACHED [ 3/21] RUN apt-get -y install curl                             0.0s
 => CACHED [ 4/21] RUN curl -O https://download.clojure.org/install/linux  0.0s
 => CACHED [ 5/21] RUN chmod +x linux-install-1.10.3.933.sh                0.0s
 => CACHED [ 6/21] RUN ./linux-install-1.10.3.933.sh                       0.0s
 => CACHED [ 7/21] RUN apt-get -y install sudo                             0.0s
 => CACHED [ 8/21] RUN initdb                                              0.0s
 => [ 9/21] RUN postgres -D /var/lib/postgresql/data &                     0.3s
 => ERROR [10/21] RUN psql -f bin/sql/postgres-db.sql -U postgres          0.4s
------
 > [10/21] RUN psql -f bin/sql/postgres-db.sql -U postgres:
#13 0.367 psql: error: could not connect to server: No such file or directory
#13 0.367   Is the server running locally and accepting
#13 0.367   connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
------
executor failed running [/bin/sh -c psql -f bin/sql/postgres-db.sql -U postgres]: exit code: 2
zendevil.eth
  • 974
  • 2
  • 9
  • 28
  • 1
    Manipulating your database during image build seems weird unless you want a very static database that is never updated. – Hans Kilian Aug 05 '21 at 10:50
  • ...and the standard Docker Hub database images largely prevent you from building a derived image with preloaded content. Do you need to run the standard `postgres` image, with files bind-mounted into `/docker-entrypoint-initdb.d`; [How to create User/Database in script for Docker Postgres](https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres)? – David Maze Aug 05 '21 at 11:15
  • 1
    Could you guys answer the question? – zendevil.eth Aug 05 '21 at 11:59
  • @DavidMaze don't worry about the bin/sql path, there will be a COPY between 14 and 16 that copies the bin/sql path – zendevil.eth Aug 05 '21 at 12:16

1 Answers1

0

From the Postgres DockerHub Page Section Initialization Scripts.

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.

Max
  • 6,821
  • 3
  • 43
  • 59