docker entrypoint script won't echo output inside `trap` function

334 Views Asked by At

I want to be able to trap a SIGINT, then perform a function, then stop the container, all with docker-compose.

Here is my script:

#!/bin/bash

backup_dir="/backup"

# Define a function to handle the SIGINT signal (graceful shutdown)
echo "[+] Postgres customer_entrypoint ran."

shutdown() {
    echo "[+] Backing up database before shutdown."
    pg_dumpall --on-conflict-do-nothing --column-inserts -U "$POSTGRES_USER" > "/backups/backup_$(date +%Y-%m-%d_%H-%M-%S).sql"
    pg_ctl -D "$PGDATA" -m fast -w stop
}

trap shutdown SIGINT
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
/usr/local/bin/docker-entrypoint.sh postgres &                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                               
wait $!

Here is my docker-compose file:

version: '3.3'                                                                                                                                                                           
services:                                                                                                                                                                               
  postgres:                                                                                                                                                                            
    container_name: postgres                                                                                                                                                                  
    build:                                                                                                                                                                                     
      context: ./postgres                                                                                                                                                                                        
      dockerfile: Dockerfile-postgres.dev                                                                                                                                                                                          
    image: postgres-my:latest                                                                                                                                                                                                                                                                                                                                                                                                                        
    restart: always                                                                                                                                                                                           
    env_file:                                                                                                                                                                                             
      - ./.env.dev.db                                                                                                                                                                                             
    ports:                                                                                                                                                                                          
        - '127.0.0.1:5432:5432'                                                                                                                                                                                       
    volumes:                                                                                                                                                                                                                                                                                                                                                                                                                                        
      - ./postgres/backups:/backups                                                                                                                                                                                     
      - postgres_data_dev:/var/lib/postgresql/data                                                                                                                                                                                                                                                                                                                                                                        
    stop_signal: SIGINT                                                                                                                                                                       
                                                                                                                                                        
volumes:                                                                                                                                                                                          
    postgres_data_dev:

I run the container in attached mode with docker compose like this:

sudo docker-compose -f docker-compose.dev up --build

I can see [+] Postgres customer_entrypoint ran. appear in the docker-compose output so I know the script is running. If I do ctrl-C, docker-compose output immediately shows this:

^CGracefully stopping... (press Ctrl+C again to force)
Stopping postgres  ... done

and no other output. However I know the shutdown function ran as it created the backup file. However I thought I would see [+] Backing up database before shutdown. before the output above.

If I run docker-compose, and then run docker logs postgres --follow then I see the output.

Why can't I see any echo output from my trap function with docker-compose? How can I adjust so I can see output? The main problem is I can't see stderr either so I don't know if my logic is broken when changing code. How can I fix this?

0

There are 0 best solutions below