Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(docker): pull a built-in datastore image only when it is missing
Yudhi Armyndharis2b1c71894b56
diff --git a/src/modules/docker/compose-parity.spec.ts b/src/modules/docker/compose-parity.spec.tsindex 23ded829d..dd5e41813 100644--- a/src/modules/docker/compose-parity.spec.ts+++ b/src/modules/docker/compose-parity.spec.ts@@ -86,6 +86,7 @@ async function capture(profile: string, Service: typeof DockerService = DockerSejest.spyOn(service, 'getContainerByService').mockResolvedValue(null);let captured: CapturedConfig | undefined;const fakeDocker = {+ getImage: () => ({ inspect: () => Promise.reject(Object.assign(new Error('no such image'), { statusCode: 404 })) }),pull: (_image: string, cb: (err: Error | null, stream: null) => void) => cb(null, null),modem: { followProgress: (_stream: null, cb: (err: Error | null) => void) => cb(null) },createVolume: jest.fn().mockResolvedValue({}),diff --git a/src/modules/docker/docker.service.spec.ts b/src/modules/docker/docker.service.spec.tsindex 0d5225e4e..b8e882f56 100644--- a/src/modules/docker/docker.service.spec.ts+++ b/src/modules/docker/docker.service.spec.ts@@ -8,6 +8,11 @@ jest.mock('dockerode');// onModuleInit/isDockerAvailable tests steer the daemon by replacing its implementation.const DockerMock = Docker as unknown as jest.Mock;+/** A daemon with no local copy of any image: inspect answers 404, so createService pulls. */+const imageNotCached = () => ({+ inspect: jest.fn().mockRejectedValue(Object.assign(new Error('no such image'), { statusCode: 404 })),+});+describe('DockerService.getRunningBuiltinServices', () => {const container = (name: string, service: string, state: string) => ({id: name,@@ -186,6 +191,7 @@ describe('DockerService.onModuleInit', () => {const happyDocker = () => ({ping: jest.fn().mockResolvedValue(undefined),listContainers: jest.fn().mockResolvedValue([]),+ getImage: imageNotCached,pull: (_image: string, cb: (err: Error | null, stream: null) => void) => cb(null, null),modem: { followProgress: (_stream: null, cb: (err: Error | null) => void) => cb(null) },createVolume: jest.fn().mockResolvedValue({}),@@ -455,6 +461,7 @@ describe('DockerService.createService', () => {it('pulls, creates the volume and container, and starts it (a pre-existing volume is fine)', async () => {const start = jest.fn().mockResolvedValue(undefined);const docker = {+ getImage: imageNotCached,pull: (_image: string, cb: (err: Error | null, stream: null) => void) => cb(null, null),modem: { followProgress: (_stream: null, cb: (err: Error | null) => void) => cb(null) },// EEXIST races are normal — the volume may survive from an earlier run.@@ -475,6 +482,7 @@ describe('DockerService.createService', () => {it('returns false when the daemon rejects the image pull', async () => {const docker = {+ getImage: imageNotCached,pull: (_image: string, cb: (err: Error | null, stream: null) => void) =>cb(new Error('pull access denied'), null),};@@ -483,6 +491,42 @@ describe('DockerService.createService', () => {await expect(service.createService('redis')).resolves.toBe(false);});++ it('creates the container from a cached image without pulling it', async () => {+ const start = jest.fn().mockResolvedValue(undefined);+ const getImage = jest.fn(() => ({ inspect: jest.fn().mockResolvedValue({ Id: 'sha256:abc' }) }));+ const docker = {+ getImage,+ pull: jest.fn(),+ createVolume: jest.fn().mockResolvedValue({}),+ createContainer: jest.fn().mockResolvedValue({ start }),+ };+ const service = withDocker(docker);+ jest.spyOn(service, 'getContainerByService').mockResolvedValue(null);++ await expect(service.createService('redis')).resolves.toBe(true);++ expect(getImage).toHaveBeenCalledWith('redis:7-alpine');+ expect(docker.pull).not.toHaveBeenCalled();+ expect(start).toHaveBeenCalledTimes(1);+ });++ it('pulls the pinned image once when it is not on the host', async () => {+ const pull = jest.fn((_image: string, cb: (err: Error | null, stream: null) => void) => cb(null, null));+ const docker = {+ getImage: imageNotCached,+ pull,+ modem: { followProgress: (_stream: null, cb: (err: Error | null) => void) => cb(null) },+ createVolume: jest.fn().mockResolvedValue({}),+ createContainer: jest.fn().mockResolvedValue({ start: jest.fn().mockResolvedValue(undefined) }),+ };+ const service = withDocker(docker);+ jest.spyOn(service, 'getContainerByService').mockResolvedValue(null);++ await expect(service.createService('redis')).resolves.toBe(true);+ expect(pull).toHaveBeenCalledTimes(1);+ expect(pull).toHaveBeenCalledWith('redis:7-alpine', expect.any(Function));+ });});describe('DockerService.startService', () => {diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex 423f3c4db..26700d051 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -360,17 +360,29 @@ export class DockerService implements OnModuleInit {return true;}- // Pull image first- this.logger.log(`Pulling image: ${spec.image}`);- await new Promise<void>((resolve, reject) => {- void this.docker!.pull(spec.image, (err: Error | null, stream: NodeJS.ReadableStream) => {- if (err) return reject(err);- this.docker!.modem.followProgress(stream, (err2: Error | null) => {- if (err2) return reject(err2);- resolve();+ // Pull only when the image is not already on the host, as compose's default+ // `pull_policy: missing` does: a host that has the image keeps working when the registry is+ // unreachable or has withdrawn it. As with compose, a cached floating tag is not refreshed+ // here. Any inspect failure falls back to pulling.+ const cached = await this.docker+ .getImage(spec.image)+ .inspect()+ .then(+ () => true,+ () => false,+ );+ if (!cached) {+ this.logger.log(`Pulling image: ${spec.image}`);+ await new Promise<void>((resolve, reject) => {+ void this.docker!.pull(spec.image, (err: Error | null, stream: NodeJS.ReadableStream) => {+ if (err) return reject(err);+ this.docker!.modem.followProgress(stream, (err2: Error | null) => {+ if (err2) return reject(err2);+ resolve();+ });});});- });+ }// Create volume if neededif (spec.volumes) {