简体   繁体   中英

force restart postgresql docker

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.

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM