Fly cli is deploying devDependencies from package.json

Apparently, the fly cli/fly deploy is putting the devDependencies from the package.json file into the container image.

When I remove the devDependencies from the package.json file the image size drops from around 300 MB to 260 MB. I did this two times in the same project to confirm that.

Try adding node_modules to the .dockerignore file.

1 Like

My .dockerignore is as follows:


Putting node_modules in .dockerignore will avoid copying node_modules from a local directory to the container image, but Dockerfile could still install devDependencies.

@merciof - Can you show us your Dockerfile? We made some changes around devDependencies recently ( / cc @rubys).

Once flyctl creates Dockerfile, that’s yours and you can change that whatever you want. It would be better to edit your Dockerfile, rather than getting rid of devDependencies from package.json. Your “for development” dependencies should be locked by package.json along with production dependencies.

1 Like

Of course! Here’s the Dockerfile generated by fly deploy command :

FROM debian:bullseye as builder

ENV PATH=/usr/local/node/bin:$PATH

RUN apt-get update; apt install -y curl python-is-python3 pkg-config build-essential && \
    curl -sL | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
rm -rf /tmp/node-build-master

RUN mkdir /app

COPY . .

RUN npm install

FROM debian:bullseye-slim

LABEL fly_launch_runtime="nodejs"

COPY --from=builder /usr/local/node /usr/local/node
COPY --from=builder /app /app

ENV NODE_ENV production
ENV PATH /usr/local/node/bin:$PATH

CMD [ "npm", "run", "start" ]

That’s not the latest. If you were to update your flyctl and relaunch you will get a better Dockerfile which will produce a significantly smaller image. Alternately, you can add --omit dev to the npm install line

1 Like

Thank you!