-2

I am trying to create a Docker for a PostgreSQL database and to import some data into it. I have followed the official documentation.

The Dockerfile looks like this:

FROM postgres:14.10

#Set env vars
ENV PGDATA=/var/pgdata
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=admin
ENV POSTGRES_HOST_AUTH_METHOD=trust

# Copy backup to container
COPY opr.dump /opr.dump
COPY entrypoint2.sh /docker-entrypoint-initdb.d/entrypoint2.sh

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]

The entrypoint script looks like this:

#!/bin/bash
set -e

# Create the Postgres database 
createdb opr 

# Extract the schema and data from the backup file
pg_restore -U postgres -d opr /opr.dump

Everything works fine but after importing the data the container stops, both in attached and detached mode. I copied the ENTRYPOINT and CMD from the official image but without success.

I should mention that I tried also without copying ENTRYPOINT and CMD and the container still stops.

My question is how do I make the container not stop after importing the data?

2
  • 3
    Backup/Restore a dockerized PostgreSQL database discusses several paths, though a little more focused on using docker exec psql after the database container is started up. The important detail here is that you can't run pg_restore until the database is actually running, which makes it hard to run in an entrypoint script. The stock entrypoint script has logic to handle this, which @ABWassim's answer uses. Commented Dec 21, 2023 at 15:01
  • My mistake then, thought it was some sort of entrypoint issue, I just edited my answer. Thanks for the clarification @DavidMaze ! Commented Dec 21, 2023 at 16:26

2 Answers 2

0

You just need to copy your entrypoint2.sh script in the docker-entrypoint-initdb.d directory, as according to the documentation (see 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 [...] it will run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.

Therefore no need to override the ENTRYPOINT and the CMD.

EDIT : looks like you will have to do it after the container starts, like David Maze pointed out.

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

3 Comments

I did that and the container still stops. That's why I copied ENTRYPOINT and CMD.
Take a look at David Maze's comment on your post, he actually has the right answer.
The important combination of this answer and my comment is that the postgres image's entrypoint already has logic to start a temporary database server, run the init script(s), and then restart the "real" database. Putting the script in /docker-entrypoint-initdb.d, or just the dump file if it's in SQL-statement format (make sure to name it *.sql), should work.
0

In the end I managed to create a custom entrypoint script that did the trick.

The Dockerfile looks like this:

FROM postgres:14.10

#Set env vars
ENV PGDATA=/var/pgdata
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=admin
ENV POSTGRES_HOST_AUTH_METHOD=trust

# Create a directory to store PostgreSQL data and logs
RUN mkdir -p ${PGDATA} /tmp /var/log/postgresql && chown -R postgres:postgres ${PGDATA} /tmp /var/log/postgresql

WORKDIR /data

# Expose the PostgreSQL port
EXPOSE 5432

# Copy the entrypoint script to the container
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Copy backup to container
COPY opr.dump /opr.dump

# Set the user to run the container
USER postgres

# Run the entrypoint script
CMD ["/entrypoint.sh"]

The entrypoint script looks like this:

#!/bin/bash

# Initialize the PostgreSQL data directory
initdb -D ${PGDATA}

#change hba_conf
echo "host all all all trust" >> /var/pgdata/pg_hba.conf

# Start PostgreSQL in the background
pg_ctl -D ${PGDATA} -l /var/log/postgresql/logfile start

# Wait for PostgreSQL to start
wait_postgresql() {
  while ! pg_isready -q; do
    echo "Waiting for PostgreSQL to start..."
    sleep 1
  done
}
wait_postgresql

# Create the Postgres database 
createdb opr 

# Extract the schema and data from the backup file
pg_restore -U postgres -d opr /opr.dump

# Keep PostgreSQL running
tail -f /var/log/postgresql/logfile

Comments

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.