diff --git a/Dockerfile b/Dockerfile index 0e961fb..59145df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,11 +3,13 @@ FROM node:15-alpine ENV NODE_ENV=production ENV CORS="" +ENV PORT=80 ENV MJML_KEEP_COMMENTS=false ENV MJML_VALIDATION_LEVEL=soft ENV MJML_MINIFY=true ENV MJML_MINIFY=false +ENV HEALTHCHECK=true COPY package* ./ @@ -20,9 +22,8 @@ COPY index.js ./index.js COPY healthcheck.sh /healthcheck.sh -HEALTHCHECK --timeout=30s \ - CMD /healthcheck.sh || exit 1 +HEALTHCHECK --start-period=10s --retries=1 CMD /healthcheck.sh || exit 1 EXPOSE 80 -ENTRYPOINT [ "node", "index.js"] +ENTRYPOINT [ "node", "index.js" ] diff --git a/README.md b/README.md index f8a001d..d6b29bb 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Due to various challenges this image sports the following features: - [Defaults](#defaults) - [Development](#development) - [Troubleshooting](#troubleshooting) + - [Kubernetes](#kubernetes) ## Overview @@ -36,7 +37,7 @@ services: mjml: image: adrianrudnik/mjml-server ports: - - 8889:80 + - 8080:80 # environment: # to change the port: # - PORT=8080 @@ -58,6 +59,7 @@ MJML_KEEP_COMMENTS "false" MJML_VALIDATION_LEVEL "soft" MJML_MINIFY "true" MJML_BEAUTIFY "false" +HEALTHCHECK "true" ``` ## Development @@ -70,6 +72,7 @@ MJML_KEEP_COMMENTS "true" MJML_VALIDATION_LEVEL "strict" MJML_MINIFY "false" MJML_BEAUTIFY "true" +HEALTHCHECK "false" ``` This will escalate any issues you have with invalid mjml code to the docker log (`stdout` or `docker-compose logs`). @@ -79,3 +82,26 @@ This will escalate any issues you have with invalid mjml code to the docker log Make sure you pass along a plain Content-Type header and pass the mjml as raw body. Catch errors by looking at the HTTP response code. + +### Kubernetes + +As the default Dockerfile specific `HEALTHCHECK` directive is not supported by kubernetes, you might need to specify your own probes: + +``` +spec: + containers: + - name: ... + livenessProbe: + exec: + command: + - curl - -X POST - 'http://localhost:80/' + initialDelaySeconds: 30 + periodSeconds: 30 + readinessProbe: + exec: + command: + - curl - -X POST - 'http://localhost:80/' + initialDelaySeconds: 25 +``` + +Be aware that this does only check the connectivity and that the port might vary. If you want a functional check as well, you could shift to an approach like the ones used for docker with the result of the [healthcheck.sh](healthcheck.sh). But I'm not a kubernetes user, so feel free to do a pull request if you have a slim approach. diff --git a/healthcheck.sh b/healthcheck.sh index 5986d3d..7b47833 100755 --- a/healthcheck.sh +++ b/healthcheck.sh @@ -1,6 +1,10 @@ #!/bin/sh -HOST=http://localhost:80/ +if [ "$HEALTHCHECK" == "false" ]; then + exit 0 +fi + +HOST=http://127.0.0.1:${PORT}/ TOKEN=RmVXY49YwsRfuBBfiYcWOpq6Py57pfa2x RESULT=$(curl -s -f -X POST -H "Content-Type: text/plain" --data "${TOKEN}" ${HOST}) diff --git a/index.js b/index.js index 30926a5..8f8b9f2 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ const opts = { minify: (process.env.MJML_MINIFY === 'true'), beautify: (process.env.MJML_BEAUTIFY === 'true'), validationLevel: (['soft', 'strict', 'skip'].includes(process.env.MJML_VALIDATION_LEVEL) ? process.env.MJML_VALIDATION_LEVEL : 'soft'), + healthchecks: (process.env.HEALTHCHECK === 'true') } app.all('*', function (req, res) { @@ -79,6 +80,7 @@ Object.keys(signals).forEach((signal) => { console.log('self: ' + os.hostname() + ':' + server.address().port) console.log('cors: ' + (process.env.CORS || 'n/a')) +console.log('healthchecks: ' + opts.healthchecks) console.log('mjml keep comments: ' + opts.keepComments) console.log('mjml validation level: ' + opts.validationLevel) console.log('mjml minify: ' + opts.minify)