Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule daily Docker container restart / reset

Tags:

linux

docker

I have a Linux based Docker container running an application which seems to have a memory leak. After around a week requests to the application start to fail and the container requires a restart to reset its state and get things working again.

The error reported by the application is:

java.lang.OutOfMemoryError: Java heap space

Is there a generic method that can be used to trigger a restart, resetting it's state, regardless of which service is being used to host it? If there's not a good generic solution, I'm about to give DigitalOcean a whirl so maybe there's a DigitalOcean specific solution that may work instead?

like image 774
Gavin Avatar asked Dec 17 '25 13:12

Gavin


2 Answers

If you need more fine-grained scheduling, I took @Marvin's answer here and modified it for an in-line sh script. This example schedules a daily reset of the containers at within 60 seconds of 8pm UTC:

version: "3"
services:

  myservice:
    container_name: myservice
      
  restarter:
    image: docker:cli
    restart: unless-stopped
    volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
    entrypoint: ["/bin/sh","-c"]
    command:
      - |
        while true; do
          if [ "$$(date +'%H:%M')" = '20:00' ]; then
            docker restart myservice
          fi
          sleep 60
        done
like image 87
n8jadams Avatar answered Dec 20 '25 10:12

n8jadams


have you ever seen this answer below : Restart container from docker compose

you should to try, i've tried that for my project & work properly.

or you can try this on your docker compose file

version: '3'
services:
  voice_to_text:
    build: 
      context: .
      dockerfile: dockerfile
    image: marvinhris/vtt_fw
    env_file:
      - .env
    container_name: ${CONTAINER_NAME}
    stdin_open: true
    tty: true
    restart: ${RESTART}
    deploy:
      resources:
        limits:
          memory: ${MEMORY}
    memswap_limit: ${SWAP_LIMIT}
    expose:
      - ${EXPOSE_PORT}
    ports:
      - ${PORT}:${EXPOSE_PORT}
    command: >
      bash -c "gunicorn --workers=${WORKER_TOTAL} --threads=${THREADS} --bind=${HOST}:${EXPOSE_PORT} --worker-class=${WORKER} --timeout=${TIMEOUT} wsgi:server"
  restarter:
    image: docker:cli
    volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
    command: ["/bin/sh", "-c", "while true; do sleep 86400; docker restart ${CONTAINER_NAME}; done"]
    restart: ${RESTART}

on my code above, its running a stack named "voice_to_text" with two service, one as a main service, second is restarter.

if you have a docker compose file, you only need add code for restarter services, and i set it up for 86400 seconds or 24 Hours to restart the main service

hope this help.

like image 25
Marvin Avatar answered Dec 20 '25 09:12

Marvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!