tursodatabase / libsql

libSQL is a fork of SQLite that is both Open Source, and Open Contributions.
https://turso.tech/libsql
MIT License
9.54k stars 252 forks source link

docker: add healthcheck #1559

Open MNThomson opened 2 months ago

MNThomson commented 2 months ago

Context

Added a Docker HEALTHCHECK to both the production & dev Dockerfiles.

Now ok, this bash is absolutely cursed and could be removed by just running apt install curl. I didn't install curl since no other apt packages are installed in the runtime image and I'm assuming we want to keep the production image small. If that's acceptable though, adding curl is probably a better route.

Test

$ docker build -t libsql . && docker run --name libsql libsql

# another terminal
$ docker ps
CONTAINER ID   IMAGE    COMMAND                  CREATED              STATUS
6ac6656767ec   libsql   "/usr/local/bin/dock…"   About a minute ago   Up About a minute (healthy)

$ docker inspect --format='{{json .State.Health}}' libsql | jq
{
  "Status": "healthy",
  "FailingStreak": 0,
  "Log": [
    {
      "Start": "2024-07-15T19:43:48.64977628-07:00",
      "End": "2024-07-15T19:43:48.703180793-07:00",
      "ExitCode": 0,
      "Output": ""
    }
  ]
}

Modifying docker-health.sh to point to a port that clearly isn't running or to /doesnotexist will cause the healthcheck to fail (to simulate the /health endpoint not responding):

$ docker ps
CONTAINER ID   IMAGE    COMMAND                  CREATED              STATUS
9c889dd1ef0d   libsql   "/usr/local/bin/dock…"   About a minute ago   Up About a minute (unhealthy)
haaawk commented 2 months ago

What's the benefit of having those health checks @MNThomson ?

haaawk commented 2 months ago

Could you please rebase the PR and resolve conflicts @MNThomson ?

flexchar commented 2 months ago

I can chime in regards to benefits of health check - it enables modern reverse proxies such as Træfik to postpone routing traffic to unhealthy containers and monitoring services to listen and send notifications when container becomes unhealthy.

On Kubernetes environment, it is how K knows when to restart the container or which one to route from service.

It's actually a good thing. :)

haaawk commented 2 months ago

Thanks @flexchar . It seems that we could merge this once @MNThomson resolves the conflicts

MNThomson commented 2 months ago

Thanks @flexchar! Healthchecks help the container scheduler know when to recreate containers (and especially help in container dependency graphs).

Rebased @haaawk, should be good to go!