I have an NodeJS express app that I want to dockerize. For that I created a Dockerfile
:
FROM node:18 AS server
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json /
RUN npm ci
COPY . .
I also have a .dockerignore
file:
node_modules/
client/node_modules/
Dockerfile
docker-compose.yml
.git
.gitignore
.dockerignore
.env
All is run with a help of docker-compose.yml
:
version: '3.8'
services:
app:
container_name: my-app
image: my-org/my-app
build:
context: .
dockerfile: Dockerfile
command: node index.js
ports:
- "3030:3030"
environment:
HELLO: world
env_file:
- .env
When I run the Dockerfile
commands in this order, the COPY . .
seems to remove the node_modules
from the image, that are created with npm ci
that runs beforehand. I've tried it with first running COPY . .
and then npm ci
and node_modules
stays in the image.
My question is – is it better to run npm ci
before COPY . .
, and if the answer is yes, then how can I make the node_modules
stay?