Skip to content

feat(mongosh): implement multi-stage build and optimize image size#35

Open
jahirvidrio wants to merge 5 commits into
alpine-docker:masterfrom
jahirvidrio:master
Open

feat(mongosh): implement multi-stage build and optimize image size#35
jahirvidrio wants to merge 5 commits into
alpine-docker:masterfrom
jahirvidrio:master

Conversation

@jahirvidrio
Copy link
Copy Markdown

@jahirvidrio jahirvidrio commented Apr 7, 2026

I've implemented a multi-stage build to significantly reduce the final image size.

Changes:

  • Introduce multi-stage build to reduce final image footprint.
  • Implement node-prune and manual cleanup of .d.ts, .ts, and .map files.
  • Fetch mongosh tarball directly from NPM registry for a leaner build.

jahirvidrio and others added 5 commits April 6, 2026 18:26
- Introduce multi-stage build to reduce final image footprint.
- Implement node-prune and manual cleanup of .d.ts, .ts, and .map files.
- Fetch mongosh tarball directly from NPM registry for a leaner build.
- Achieved a size reduction of ~100MB compared to the previous version.
ARGs defined before the first FROM instruction are in the global scope but must be re-declared inside each stage to be accessible. This ensures the correct version of mongosh is fetched during the build process.
To ensure compatibility with scripts executed by CircleCI
@ozbillwang
Copy link
Copy Markdown
Contributor

ozbillwang commented Apr 8, 2026

Not big difference

# local build
mongosh latest 1ba69f95656b   About a minute ago   520MB

# existing alpine/mongosh
alpine/mongosh  latest 2d7a9cb13f43   2 years ago     559MB

@ozbillwang ozbillwang closed this Apr 8, 2026
@ozbillwang ozbillwang reopened this Apr 8, 2026
@jahirvidrio
Copy link
Copy Markdown
Author

jahirvidrio commented Apr 8, 2026

You’re right, the size difference isn't that big. I think I misleadingly framed this PR by focusing on 'optimization' in the title, but the real goal is to finally unblock the mongosh update, which has been stuck for two years.

The current approach in #31 is hitting a wall because the new mongosh needs glibc. Trying to force glibc into Alpine (using frolvlad or sgerrand) just adds technical debt and security risks, especially regarding base image trust, as you mentioned.

I am considering building our own Alpine + glibc base image in the future.

Main reason is, the image (https://hub.docker.com/r/frolvlad/alpine-glibc/) doesn't mention anything about where it is built from (it's own Github or gitlab source code and automation cicd pipeline to check). It will be high risk to use this type of image as base images

About the Node.js CVEs, I actually think using node:lts-alpine is safer. The standalone mongosh binary is a static-like bundle that includes a fixed Node.js runtime. If that Node version has a vulnerability, we’re stuck waiting for a new mongosh release. By using the official Node image, we can patch vulnerabilities immediately just by updating the base image. Plus, we get native musl support, which fixes the DNS and symbol errors (__res_nsearch) that we've been seeing in #31.

This PR is a clean, working way to get mongosh 2.x out there now, without the headache of maintaining a custom Alpine+glibc layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants