I created the postgres docker container with my script file. Then, when I run sudo docker ps, my container is not running and I need to run manually. After I run, there is no issue and my container is keep running. But I want to automatically running my container after I run my script.
This is my script file
#!/bin/bash
set -e
# Start PostgreSQL container
docker run -d --name postgresql
--network mynet
--ip 172.18.0.33
-e POSTGRES_PASSWORD=mysecretpassword
-v postgres_data:/var/lib/postgresql/data
postgres:14
# Wait for the PostgreSQL container to fully initialize
echo "Waiting for PostgreSQL container to initialize..."
sleep 30
# Create the 'odoo' user and database
docker exec -it postgresql psql -U postgres -c "CREATE USER odoo WITH PASSWORD 'mypassword' CREATEDB;"
# Adjust PostgreSQL configuration within the container
docker exec -it postgresql sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /var/lib/postgresql/data/postgresql.conf
docker exec -it postgresql bash -c "echo 'host all all 172.18.0.0/16 password' >> /var/lib/postgresql/data/pg_hba.conf"
# Restart the PostgreSQL container
docker restart postgresql
echo "PostgreSQL installation completed successfully."
exit 0
This is my container logs after I run my script.
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2023-11-14 07:26:51.570 UTC [46] LOG: starting PostgreSQL 14.10 (Debian 14.10-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-11-14 07:26:51.570 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-11-14 07:26:51.573 UTC [47] LOG: database system was shut down at 2023-11-14 07:26:51 UTC
2023-11-14 07:26:51.575 UTC [46] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2023-11-14 07:26:51.704 UTC [46] LOG: received fast shutdown request
waiting for server to shut down....2023-11-14 07:26:51.704 UTC [46] LOG: aborting any active transactions
2023-11-14 07:26:51.708 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1
2023-11-14 07:26:51.708 UTC [48] LOG: shutting down
2023-11-14 07:26:51.714 UTC [46] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2023-11-14 07:26:51.821 UTC [1] LOG: starting PostgreSQL 14.10 (Debian 14.10-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-11-14 07:26:51.822 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2023-11-14 07:26:51.822 UTC [1] LOG: listening on IPv6 address "::", port 5432
2023-11-14 07:26:51.823 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-11-14 07:26:51.825 UTC [59] LOG: database system was shut down at 2023-11-14 07:26:51 UTC
2023-11-14 07:26:51.828 UTC [1] LOG: database system is ready to accept connections
2023-11-14 07:27:21.093 UTC [1] LOG: received fast shutdown request
2023-11-14 07:27:21.093 UTC [1] LOG: aborting any active transactions
2023-11-14 07:27:21.099 UTC [1] LOG: background worker "logical replication launcher" (PID 65) exited with exit code 1
2023-11-14 07:27:21.099 UTC [60] LOG: shutting down
2023-11-14 07:27:21.105 UTC [1] LOG: database system is shut down
PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-11-14 07:27:21.407 UTC [1] LOG: starting PostgreSQL 14.10 (Debian 14.10-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-11-14 07:27:21.407 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2023-11-14 07:27:21.407 UTC [1] LOG: listening on IPv6 address "::", port 5432
2023-11-14 07:27:21.408 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-11-14 07:27:21.410 UTC [26] LOG: database system was shut down at 2023-11-14 07:27:21 UTC
2023-11-14 07:27:21.413 UTC [1] LOG: database system is ready to accept connections
2023-11-14 07:29:45.372 UTC [1] LOG: received fast shutdown request
2023-11-14 07:29:45.373 UTC [1] LOG: aborting any active transactions
2023-11-14 07:29:45.377 UTC [35] FATAL: terminating connection due to administrator command
2023-11-14 07:29:45.377 UTC [34] FATAL: terminating connection due to administrator command
2023-11-14 07:29:45.393 UTC [1] LOG: background worker "logical replication launcher" (PID 32) exited with exit code 1
2023-11-14 07:29:45.394 UTC [27] LOG: shutting down
2023-11-14 07:29:45.400 UTC [45] FATAL: the database system is shutting down
2023-11-14 07:29:45.410 UTC [1] LOG: database system is shut down
2
Answers
you need to add your script to
docker-entrypoint-initdb.d
check if here is ansver to your question
Initialize PostgreSQL Container with docker-entrypoint-initdb.d script
Please add the script to the entrypoint
docker-entrypoint-initdb.d