skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search