0

I'm a bit new to docker and it's the first time I'm trying to add healthcheck.

The docker application I'm using is the example from here: https://docs.docker.com/get-started/02_our_app/

I simply followed the steps to get a container with a service that runs locally on port 3000. I browsed to http://localhost:3000 and it does work.

The Dockerfile before any changes I've made:

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

And the original command to run the docker:

docker run -dp 3000:3000 getting-started

Then, I tried to add a healthcheck in a few ways.

First way: I changed the Dockerfile as follows, then re-build and re-ran:

# syntax=docker/dockerfile:1
FROM node:12-alpine
HEALTHCHECK --interval=3s --timeout=1s CMD curl --fail http://localhost:3000 || exit 1
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Second way: I changed the run command as follows:

docker run -dp 3000:3000 --health-cmd "curl --fail http://localhost:3000 || exit 1" getting-started

In both cases, I checked the health status using docker ps, and after it ended the "health: starting" phase, it always entered the "unhealthy" phase. Never "healthy".

In both cases, I made sure that http://localhost:3000 works and returns HTTP status 200.

While experimenting in all sorts of ways, I tried to remove the || exit 1 part but it did not help. I tried to replace it with || exit 0, and then indeed it displayed "healthy", but that doesn't really mean anything.

Does anyone have any idea what am I doing wrong? I need to do something more complex with healthcheck, but for starters I want to succeed in making it work for a simple thing.

More details:

I'm using Windows 10 Enterprise Version 20H2, Docker version 20.10.7, build f0df350. I'm running the commands from Git Bash.

Orielno
  • 409
  • 2
  • 8

0 Answers0