skip to Main Content

I have a script trying to restart postgresql docker container:

DOCKER_CONTAINER_NAME="timescaledb"
docker restart -t 1 $DOCKER_CONTAINER_NAME
timeout 9000000s bash -c "until docker exec $DOCKER_CONTAINER_NAME pg_isready ; do sleep 1 ; done"

However, this line (with any -t number)

docker restart -t 1 $DOCKER_CONTAINER_NAME

Takes extremely large amount of time to shutdown postgresql. Generally I have to go to the screen container postgresql (I used screens to manage my orchestration). and press Ctrl-C to force it shutdown and enter

docker run -ti --user 1000:1000  -p 5432:5432 --name timescaledb  --volume=/home/ubuntu/pgdata3:/home/postgresql/pgdata --rm -e POSTGRES_PASSWORD=dashdhwqehqwhhkshajdkh -e PGDATA=/home/postgresql/pgdata timescale/timescaledb-ha:pg13-latest;

Is it possible to complete this process of force restart without me entering into the "screen" and press Ctrl-C and restart manually?

A related question is posted here:
postgresql docker ctrl C seems to be better than docker kill

I wanted to find a way to mimic Ctrl-C shut down of postgresql.

2

Answers


  1. Chosen as BEST ANSWER

    ok, it seems this is sequence of command served my purpose. Thanks, everyone!

    docker stop -t 120 $DOCKER_CONTAINER_NAME
    docker kill $DOCKER_CONTAINER_NAME
    screen -S i2 -X stuff 'docker run -ti --user 1000:1000  -p 5432:5432 --name timescaledb  --volume=/home/ubuntu/pgdata3:/home/postgresql/pgdata --rm -e POSTGRES_PASSWORD=sahisahikqhwwkejkqwjehjhwqjh -e PGDATA=/home/postgresql/pgdata timescale/timescaledb-ha:pg13-latest;n'
    

  2. You can use docker-compose up to start your docker and add restart: always to restart the docker when it shuts down for any reason.

    Example of docker-compose.yml

    services:
       db:
           image: postgres:14
           restart: always
    

    EDIT. Replying to your comment, maybe you could prepare a bash script, forcing the docker to restart every x minutes:

    while true
    do
       docker restart posgresql
       sleep 15m
    done
    

    Or, probably better, you could program a task in crontab:

    crontab -e

    15 * * * * docker restart posgresql
    

    I’m assuming you’re working with Linux

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