Docker container is always unhealthy

In my Dockerfile I have:

FROM node:22.1.0 AS build
<...>

FROM nginx:1.25.5 AS runtime
<...>

Now for the frontend service in docker-compose.yml I set:

 frontend:
    image: frontend
    restart: unless-stopped
    volumes:
      - ./frontend-nginx.conf:/etc/nginx/nginx.conf
    networks:
      - proxyNetwork
    healthcheck:
      test: curl -f http://localhost || exit 1
      interval: 30s
      timeout: 10s
      retries: 3 
      start_period: 30s

Running docker ps shows my container as unhealthy, even though itā€™s working when visiting http://localhost.

Also running :

docker inspect --format='{{json .State.Health.Status}}' frontend

returns that it does not exist.

What am I missing? Thanks.

It is right as Docker Compose prefixes services with the project name and adds a replica number as suffix so frontend is not a container but a composer service and the container is probably something like yourproject-frontend-1

Regarding the issue, if the container is actually running, execute the health check manually and see what it returns without -f.

Hi, as I understood I had to execute health check manually inside the container via docker-compose exec frontend sh?

After curl http://localhost I receive:

curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server

Using curl just in CMD returns html stuff.

What CMD? ENTRYPOINT and CMD is responsible for what wil run in the container. If you run curl in CMD, you will not have the server. Although your error message proves it is not running now either or constantly restarts and you managed to access it at the right time in the browser.

1 Like

oh my days, it was running on a different port, i had to curl localhost:3000; what a mistake. thanks for help.