Published on

Docker Healthcheck for your Node.js App

Authors
Docker Healthcheck for your Node.js App

Your container is running... but is it working? You should have health checks defined for all your containers.

The HEALTHCHECK instruction tells Docker how to test a container to make sure it's working properly. As your infrastructure becomes increasingly complex, health checks become even more important.

Writing the HEALTHCHECK

Creating a health check is really easy and since we're already using Node for our application why not use it to write our health check as well?

healthcheck.js

const http = require('http');
const options = {
    host: '0.0.0.0',
    port: 80,
    timeout: 2000
};

const healthCheck = http.request(options, (res) => {
    console.log(`HEALTHCHECK STATUS: ${res.statusCode}`);
    if (res.statusCode == 200) {
        process.exit(0);
    }
    else {
        process.exit(1);
    }
});

healthCheck.on('error', function (err) {
    console.error('ERROR');
    process.exit(1);
});

healthCheck.end();

Looking at the code, you can see it's pretty straightforward. Obviously, you'll need to customize the host and port to fit your needs. The healthCheck function simply makes a request to the host and if the host does not return a status code of 200 something is broken.

Dockerfile

HEALTHCHECK --interval=10s --timeout=2s --start-period=15s \  
    CMD node /healthcheck.js

Finally, enable the health check by adding the HEALTHCHECK instruction to your Dockerfile. Notice the command that runs healthcheck.js. Update that path accordingly and you should be set!

Hope this helps! Hit me up on Twitter: @Mineo27 if you have any issues/questions.

© 2015-2022 AnthonyMineo.com