Meteor-base, docker

Hello!

I’m using GitHub - disney/meteor-base: Base Docker image for use by Meteor apps to run meteor inside a docker container. The problem is that this image runs node as root and therefor all files written by the node process have root as their owner.

But the owner of the files should be a non-root user. So I tried to add “USER node” to the Dockerfile (4th line from the bottom):

FROM geoffreybooth/meteor-base:2.11.0

COPY ./app/package*.json $APP_SOURCE_FOLDER/
RUN bash $SCRIPTS_FOLDER/build-app-npm-dependencies.sh
COPY ./app $APP_SOURCE_FOLDER/
RUN bash $SCRIPTS_FOLDER/build-meteor-bundle.sh

FROM node:14.21.3-alpine

ENV APP_BUNDLE_FOLDER /opt/bundle
ENV SCRIPTS_FOLDER /docker

RUN apk --no-cache add \
		bash ca-certificates \
        imagemagick libwebp libwebp-tools exiftool

COPY --from=0 $SCRIPTS_FOLDER $SCRIPTS_FOLDER/
COPY --from=0 $APP_BUNDLE_FOLDER/bundle $APP_BUNDLE_FOLDER/bundle/

RUN bash $SCRIPTS_FOLDER/build-meteor-npm-dependencies.sh
USER node
ENTRYPOINT ["/docker/entrypoint.sh"]

CMD ["node", "main.js"]

After docker compose build and docker compose up -d the container keeps restarting with the following error:

Connecting to MongoDB...
internal/modules/cjs/loader.js:934
  throw err;
  ^

Error: Cannot find module 'mongodb'
Require stack:
- /docker/[stdin]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:931:15)
    at Function.Module._load (internal/modules/cjs/loader.js:774:27)
    at Module.require (internal/modules/cjs/loader.js:1003:19)
    at require (internal/modules/cjs/helpers.js:107:18)
    at [stdin]:1:21
    at Script.runInThisContext (vm.js:134:12)
    at Object.runInThisContext (vm.js:310:38)
    at internal/process/execution.js:81:19
    at [stdin]-wrapper:6:22
    at evalScript (internal/process/execution.js:80:60) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/docker/[stdin]' ]
}

(btw: many thanks to @GeoffreyBooth and @diavrank95 for this great meteor image)

I found a solution:

...
COPY --chown=node:node --from=0 $SCRIPTS_FOLDER $SCRIPTS_FOLDER/
COPY --chown=node:node --from=0 $APP_BUNDLE_FOLDER/bundle $APP_BUNDLE_FOLDER/bundle/

USER node
...
2 Likes

Thanks for sharing the solution!