Sometimes you need to automate some things in Docker containers, run a script or check a result online, and I honestly dread dealing with cron to do it.
That’s where Ofelia comes in! It’s a modern scheduler built from the ground up for Docker, and it interacts directly with the Docker socket to run a container, run a script in a specific container, or even on the host!
Usage with labels
I wanted to run a script to update data in a JSON file. You can configure Ofelia in several ways, but I think by far the easiest is to just use docker labels with compose.
Labels are structured like this:
- job-exec: this job is executed inside of a running container.
- job-run: runs a command inside of a new container, using a specific image.
- job-local: runs the command inside of the host running ofelia.
- job-service-run: runs the command inside a new “run-once” service, for running inside a swarm
Available parameters for each job type here
Let me explain the setup here.
First we have
daemon --docker as command, because adding
--docker allows us to use labels as configuration, you don’t have to do that, you can also use an INI structured file as config.
And since it’s running inside a container, and it needs access to docker to start containers and run commands, in addition to reading container labels, we also mount the docker socket at
Then in the labels I defined a job to run every 24h, and the command is
/bin/sh -c "apk add curl jq && sh /update_json_file.sh /src/file.json" I had to manually open a shell with
/bin/sh -c to use
&& and run multiple commands.
And that’s it! You now have an automated Docker container without having to fiddle with cron!