Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(docker): probe the configured port in the healthcheck (#1811)

Yudhi Armyndharis544a719f860a
diff --git a/.env.example b/.env.example
index 6c00d41fd..7b5b237d4 100644
--- a/.env.example
+++ b/.env.example
@@ -27,6 +27,7 @@
NODE_ENV=production
# Port the app binds to when run directly (bare metal / `npm run start:prod`). In the bundled Docker
# Compose the container always listens on 2785; the API_PORT below is only the HOST-published port.
+# To run the image itself on another port, pass `-e PORT=...`: the image healthcheck never reads .env.
PORT=2785
# Docker Compose only: the host-side port mapped to the container's 2785 (no effect on a bare-metal run).
API_PORT=2785
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78b7f8853..cfa9921d5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Probe the configured `PORT` in the Docker image healthcheck instead of a fixed 2785.
- Cap the total bytes one storage import writes at `STORAGE_IMPORT_MAX_TOTAL_BYTES`.
- Close the Baileys session proxy's fetch dispatcher when the session disconnects, logs out or is destroyed.
- Apply the Java SDK request timeout to the response body as well as the headers.
diff --git a/Dockerfile b/Dockerfile
index d508425a2..da4f2d81f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -303,9 +303,11 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Expose port
EXPOSE 2785
-# Health check
+# Probe the port the app binds: PORT from the container environment, else the app's own default.
+# The probe shell sees only that environment, never a PORT set in a mounted .env file, so a
+# non-default PORT must be passed with -e/--env. Shell form on purpose: exec form would not expand it.
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
- CMD curl -f http://localhost:2785/api/health/ready || exit 1
+ CMD curl -f "http://localhost:${PORT:-2785}/api/health/ready" || exit 1
# dumb-init is PID 1 and handles signal forwarding.
# It execs docker-entrypoint.sh (as root), which fixes volume ownership and
diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.md
index c9a2f24ea..ed2736b7c 100644
--- a/docs/10-devops-infrastructure.md
+++ b/docs/10-devops-infrastructure.md
@@ -56,7 +56,7 @@ RUN npm ci --omit=dev --ignore-scripts \
&& npm cache clean --force
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
- CMD curl -f http://localhost:2785/api/health/ready || exit 1
+ CMD curl -f "http://localhost:${PORT:-2785}/api/health/ready" || exit 1
# dumb-init is PID 1; the entrypoint runs as root, fixes /app/data ownership, then drops to the
# openwa user with gosu before it execs the command.
@@ -69,6 +69,11 @@ walks every production dependency (#1045), so the entrypoint re-owns only the wr
and then drops privileges. Chromium comes from Chrome for Testing on amd64 and from Debian's
`chromium` package on arm64; see the `Dockerfile` for the multi-arch build.
+The healthcheck probes the `PORT` of the container environment, falling back to 2785. To run the
+image on another port, pass `PORT` as an environment variable (`docker run -e PORT=8080 ...`): the
+probe never reads a mounted `.env`, so a `PORT` set only there moves the app but not the probe, and
+the container stays `unhealthy`.
+
### Docker Compose (Development)
The repo's own `docker-compose.dev.yml` is a single-container local smoke test that runs the
diff --git a/src/modules/docker/compose-parity.spec.ts b/src/modules/docker/compose-parity.spec.ts
index fac048b6a..745a5f6dc 100644
--- a/src/modules/docker/compose-parity.spec.ts
+++ b/src/modules/docker/compose-parity.spec.ts
@@ -1,3 +1,4 @@
+import { execFileSync } from 'child_process';
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
@@ -397,7 +398,7 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {
expect(api?.ports).toEqual([expect.stringMatching(/:\$\{API_PORT:-2785\}:2785$/)]);
});
- it('openwa-api uses the same readiness healthcheck in both compose files and the image', () => {
+ it('openwa-api uses the same readiness healthcheck in both compose files', () => {
const root = join(__dirname, '../../..');
const apiHealthcheck = (file: string): string[] | undefined => {
const parsed = yaml.load(readFileSync(join(root, file), 'utf8')) as ComposeFile;
@@ -406,8 +407,25 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {
const url = 'http://localhost:2785/api/health/ready';
expect(apiHealthcheck('docker-compose.yml')).toEqual(['CMD', 'curl', '-f', url]);
expect(apiHealthcheck('docker-compose.dev.yml')).toEqual(apiHealthcheck('docker-compose.yml'));
- const dockerfile = readFileSync(join(root, 'Dockerfile'), 'utf8');
- expect(dockerfile).toContain(`CMD curl -f ${url} || exit 1`);
+ });
+
+ // Both compose files pin PORT=2785, but the image also runs on its own (`docker run -e PORT=8080`),
+ // where a probe fixed at 2785 marks a healthy app unhealthy. Run the probe against a stub curl.
+ it.each([
+ [undefined, 'http://localhost:2785/api/health/ready'],
+ ['8080', 'http://localhost:8080/api/health/ready'],
+ ])('the image healthcheck probes PORT=%s at %s', (port, url) => {
+ const dockerfile = readFileSync(join(__dirname, '../../../Dockerfile'), 'utf8');
+ const probe = /^HEALTHCHECK .*\\\n\s*CMD (.+)$/m.exec(dockerfile)?.[1];
+ expect(probe).toContain('curl -f');
+ const bin = mkdtempSync(join(tmpdir(), 'openwa-healthcheck-'));
+ try {
+ writeFileSync(join(bin, 'curl'), '#!/bin/sh\necho "$@"\n', { mode: 0o755 });
+ const env = { PATH: `${bin}:${process.env.PATH}`, ...(port && { PORT: port }) };
+ expect(execFileSync('sh', ['-c', probe!], { env, encoding: 'utf8' }).trim()).toBe(`-f ${url}`);
+ } finally {
+ rmSync(bin, { recursive: true, force: true });
+ }
});
it('redis: sets the noeviction maxmemory policy BullMQ requires, on both launch paths', async () => {