diff --git a/Dockerfile b/Dockerfile index 9be1437..747a3cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,7 @@ WORKDIR /app COPY . . RUN npm ci --quiet RUN npm run build + +HEALTHCHECK --interval=12s --timeout=12s --start-period=10s CMD npm run healthcheck + CMD [ "npm", "start" ] diff --git a/package.json b/package.json index b392e63..067db68 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "build": "aegir build --bundle false", "start": "node dist/src/index.js", "start:dev": "npm run build && node dist/src/index.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "healthcheck": "node dist/src/healthcheck.js" }, "type": "module", "repository": { diff --git a/src/healthcheck.ts b/src/healthcheck.ts new file mode 100644 index 0000000..a928043 --- /dev/null +++ b/src/healthcheck.ts @@ -0,0 +1,9 @@ +#!/usr/bin/env node +/** + * This healthcheck script is used to check if the server is running and healthy. + */ +const rootReq = await fetch(`http://localhost:${process.env.PORT ?? 8080}`, { method: 'GET' }) +const status = rootReq.status +// eslint-disable-next-line no-console +console.log(`Healthcheck status: ${status}`) +process.exit(status === 200 ? 0 : 1)