I have created the systemd service inside the docker container. When the container restarts , that service cannot able to start automatically.
I am also using the below command to start on Reboot, it is not working .
systemd is not really ment for containers, and you should not use it.
The reason for this, is that the container should be your process.
You normally dont want to have multiple services running in 1 container, you want 1 container pr service.
So whatever application you’re trying to start, it should be started by the container in the ENTRYPOINT script.
If you want me to help you, i need a bit more info on what image you’re using, if its your own dockerfile, then please share that dockerfile
I am using the below images for Postgresql Replication and failover,
bitnami/postgresql-repmgr:14
bitnami/pgpool:4
Now I want to install the pagent in bitnami/postgresql-repmgr:14, when i start the script file in Entrypoint , the script file cannot start the pgagent. So I decided to add the pgagent as service. (Systemd service) . Could you give me the proper solution to me…
Here I provided my dockerfile and the script file.
FROM bitnami/postgresql-repmgr:14
USER root
# Install pgAgent
RUN apt-get update && \
apt-get install -y pgagent && \
apt-get install -y vim && \
apt-get install -y dos2unix && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Create the directory, change ownership, and modify permissions
RUN mkdir -p /var/log/pgagent && \
chown -R postgres:postgres /var/log/pgagent && \
chmod g+w /var/log/pgagent
COPY pgagent.service /etc/systemd/system/pgagent.service
# Copy the start-pgagent.sh script
COPY start_pgagent.sh /opt/bitnami/scripts/postgresql-repmgr/start_pgagent.sh
RUN dos2unix /opt/bitnami/scripts/postgresql-repmgr/start_pgagent.sh
RUN chmod +x ./opt/bitnami/scripts/postgresql-repmgr/start_pgagent.sh
USER 1001
# Custom entry point to run start-pgagent.sh
ENTRYPOINT ["/bin/bash", "-c", "/opt/bitnami/scripts/postgresql-repmgr/entrypoint.sh /opt/bitnami/scripts/postgresql-repmgr/run.sh /opt/bitnami/scripts/postgresql-repmgr/start_pgagent.sh"]
and this my start_pgagent.sh file
#!/bin/bash
REPMGR_HOST=$(echo $REPMGR_NODE_NAME)
REPMGR_PORT="5432"
REPMGR_DBNAME="repmgr"
REPMGR_USERNAME="repmgr"
REPMGR_PASSWORD="repmgrpassword" # Provide the password securely (e.g., through an environment variable)
POSTGRES_HOST="localhost"
POSTGRES_PORT="5432"
POSTGRES_DBNAME="postgres"
POSTGRES_USERNAME="postgres"
POSTGRES_PASSWORD="adminpassword" # Provide the password securely (e.g., through an environment variable)
INTERVAL=2
# Function to stop the pgAgent service
stop_pgagent_service() {
# Find the PIDs of all pgagent processes
pgrep -f "pgagent" | while read -r pid; do
echo "Terminating pgagent process with PID $pid"
kill -TERM "$pid"
done
}
# Check if pgAgent is running
check_pgagent_status() {
# Use pgrep to check if the pgagent process is running
local NODE_NAME="$1"
# Trim whitespace and other characters from variables
REPMGR_HOST_CLEAN=$(echo "$REPMGR_HOST" | tr -d '[:space:]')
NODE_NAME_CLEAN=$(echo "$NODE_NAME" | tr -d '[:space:]')
if [ "$REPMGR_HOST_CLEAN" = "$NODE_NAME_CLEAN" ]; then
# Check if pgagent is running
if pgrep -f 'pgagent -s /var/log/pgagent/pgagent.log'; then
echo "pgagent is already running."
else
echo "pgagent is not running."
pgagent -s /var/log/pgagent/pgagent.log -l 1 host=$POSTGRES_HOST dbname=$POSTGRES_DBNAME user=$POSTGRES_USERNAME port=$POSTGRES_PORT password=$POSTGRES_PASSWORD &
fi
fi
}
SQL_QUERY="SELECT node_name FROM repmgr.nodes WHERE active=true AND type='primary';"
# Function to execute the SQL query and assign the result to a variable
execute_sql_query() {
PGPASSWORD=$REPMGR_PASSWORD psql -h $REPMGR_HOST -p $REPMGR_PORT -d $REPMGR_DBNAME -U $REPMGR_USERNAME -t -c "$SQL_QUERY" 2>/dev/null
}
# Main loop to run the query if pgAgent is running
while true; do
# Execute the SQL query and assign the result to a variable
node_name=$(execute_sql_query)
check_pgagent_status "$node_name"
sleep $INTERVAL
done
and this is service file
[Unit]
Description=PgAgent for PostgreSQL
[Service]
Type=simple
ExecStart=/start_pgagent.sh # Replace with the actual path to your script
Restart=always
[Install]
WantedBy=multi-user.target
You might want to use Podman as container runtime. It supports Systemd in rootful containers when run with --cap-add audit_write,audit_control, allowing it to run multiple service processes within a single container.