Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart containers on a schedule with ofelia? #280

Open
steilerDev opened this issue Jun 7, 2024 · 4 comments
Open

Restart containers on a schedule with ofelia? #280

steilerDev opened this issue Jun 7, 2024 · 4 comments

Comments

@steilerDev
Copy link

Hello Everyone :)

Is it possible to use ofelia to restart a running container on a schedule? Alternatively I need to find a way to kill it through job-exec.

Any pointer would be appreciated!

Thanks!

@jsnfwlr
Copy link

jsnfwlr commented Jun 26, 2024

if your target running container has a shell with pkill (or ps and kill) installed, and you know the name of the core process that is used to determine when the container will restart if restart: unless-stopped is set in the compose yaml, you could schedule a command to run to kill the process gracefully which would trigger the container to restart.

If your target container is using the s6 overlay, it will be harder to restart the container itself, but you could restart the service within the container.

Finally, if you know how to set container names (or have enough shell mastery to get container ids from docker ps), and if you are okay with running docker-in-docker you could implement a job-run config like:

[job-run "restart-other-container"]
schedule = @daily
image = docker:latest
command = sh -c 'docker restart <container-name>'
volume = /var/run/docker.sock:/var/run/docker.sock

@taraspos
Copy link
Collaborator

Ofelia currently doesn't support this, however functionality to send a signal to the container on schedule might be a good idea. Something like job-signal

@expertonium
Copy link

expertonium commented Dec 25, 2024

Thanks @jsnfwlr

One enhancement to the essential last suggestion is using docker:cli image. It's a lighter image and it is all you need.

This is really useful when you wish to emulate the behavior of a remote Scheduled Job.

The following docker-compose extract demonstrates the approach a bit further.

The use case is rotating a key via a custom cli baked into a distroless image. The reason to continuously rotate a key locally is to ensure developers don't cache a key in a way that is incompatible with expected rotation in the remote environment.

Agreed it would be handy if ofelia, which has an internal docker-client package, exposed some cli function to do this, but the following works just fine.

# docker-compose.yml
# ...

# this container runs command and exits
# env vars, mounts etc omitted
  example-key-rotation:
   image: your-cli-image:local
   command:
      - key-rotation
   container_name: example-key-rotation

# ofelia
  example-key-rotation-scheduled:
    image: mcuadros/ofelia:latest
    depends_on:
      example-key-rotation:
        condition: service_completed_successfully
    command: daemon --docker
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      ofelia.job-run.example-key-rotation.schedule: "@every 10m"
      ofelia.job-run.example-key-rotation.command:  "sh -c 'docker restart example-key-rotation'"
      ofelia.job-run.example-key-rotation.image:  "docker:cli"
      ofelia.job-run.example-key-rotation.volume:  "/var/run/docker.sock:/var/run/docker.sock"

# ...

@jsnfwlr
Copy link

jsnfwlr commented Jan 1, 2025

@expertonium thanks for improving on and completing the work around I suggested

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

No branches or pull requests

4 participants