Skip to content

Instantly share code, notes, and snippets.

@kizzx2
Last active January 12, 2025 21:19
Show Gist options
  • Save kizzx2/782b500a81ce46b889903b1f80353f21 to your computer and use it in GitHub Desktop.
Save kizzx2/782b500a81ce46b889903b1f80353f21 to your computer and use it in GitHub Desktop.
Restart a docker container periodically with docker-compose
version: '3'
services:
app:
image: nginx:alpine
ports: ["80:80"]
restart: unless-stopped
restarter:
image: docker:cli
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
command: ["/bin/sh", "-c", "while true; do sleep 86400; docker restart app_app_1; done"]
restart: unless-stopped
@marvin-hris
Copy link

@kizzx2 Thats Work as simple as that

thanks

docker restart

@counterbundle32
Copy link

Thanks. This is exactly what I was looking for.

@rootkit7628
Copy link

Thanks. Best solution I've found.

@clickade
Copy link

Excellent. Thank you for the work.

@0xYao
Copy link

0xYao commented Dec 13, 2024

What is the exact container name that we should specify? The restarter is telling me no such container

@0xYao
Copy link

0xYao commented Dec 13, 2024

What is the exact container name that we should specify? The restarter is telling me no such container

Looks like you can specify the container name using the container_name attribute under the service.

So the example can actually be modified to

version: '3'
services:
  app:
    image: nginx:alpine
    ports: ["80:80"]
    container_name: app_app_1
    restart: unless-stopped

  restarter:
    image: docker:cli
    volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
    command: ["/bin/sh", "-c", "while true; do sleep 86400; docker restart app_app_1; done"]
    restart: unless-stopped
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment