0

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 2

0

Please add the script to the entrypoint docker-entrypoint-initdb.d

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

Comments

0

You're doing two bits of configuration in your script. One is creating a user, which the postgres image can do by environment variables. The other is trying to set up IP ACLing, which will be ineffective in this environment.

A container runs in a restricted network environment. Say the Docker network does in fact have CIDR block 172.18.0.0/16. The Docker network isn't directly connected to the outside network: if you connect to a docker run -p published port then Docker provides inbound NAT, and the connection will look like it's coming from the gateway address 172.18.0.1. That means the IP ACL won't really have an effect.

That means you should be able to reduce the setup to creating the user via environment variables, and deleting the database manual networking configuration. That in turn removes all of the docker exec steps, and you don't have to restart anything.

docker run -d --name postgresql \
  --network mynet \
  -e POSTGRES_USER=odoo \
  -e POSTGRES_PASSWORD=mypassword \
  -v postgres_data:/var/lib/postgresql/data \
  postgres:14

In general, the technique of "edit a configuration file then restart the service" doesn't map well to Docker. The database images tend to have complex entrypoint scripts that can dynamically generate some of the configuration, so changes can get lost. If you need to do things like this often the best approach is to docker cp the original configuration file(s) to the host, edit it there, and docker run -v injecting the modified files. Then the container starts once with the right configuration.

(docker exec should probably not be part of your routine startup sequence.)

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.