[英]Dockerizing NodeJS – keeping node modules
I have an NodeJS express app that I want to dockerize.我有一个想要 dockerize 的 NodeJS express 应用程序。 For that I created a Dockerfile
:为此,我创建了一个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:我还有一个.dockerignore
文件:
node_modules/
client/node_modules/
Dockerfile
docker-compose.yml
.git
.gitignore
.dockerignore
.env
All is run with a help of docker-compose.yml
:一切都在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. .
当我按此顺序运行Dockerfile
命令时, COPY. .
COPY. .
seems to remove the node_modules
from the image, that are created with npm ci
that runs beforehand.似乎从图像中删除了node_modules
,它们是使用预先运行的npm ci
创建的。 I've tried it with first running COPY. .
我已经尝试过第一次运行COPY. .
COPY. .
and then npm ci
and node_modules
stays in the image.然后npm ci
和node_modules
留在图像中。
My question is – is it better to run npm ci
before COPY. .
我的问题是——在 COPY 之前运行npm ci
是否更好COPY. .
COPY. .
, and if the answer is yes, then how can I make the node_modules
stay? ,如果答案是肯定的,那么我怎样才能让node_modules
留下来?
I had a similar problem and used for that a "build container".我有一个类似的问题并用于“构建容器”。
You ignored the node_modules folder in your .dockerignore
.您忽略了 .dockerignore 中的.dockerignore
文件夹。 Therfore it does not get copied in your image.因此,它不会被复制到您的图像中。
# The instructions for the first stage
FROM node:16-alpine as builder
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
RUN apk --no-cache add python3 make g++
COPY ./package*.json ./
RUN npm install
# ------------------------------------
# The instructions for second stage
FROM node:16-alpine
WORKDIR /opt/OpenHaus/backend
COPY --from=builder node_modules node_modules
RUN apk --no-cache add openssl
COPY . ./
#COPY ./package.json ./
#ENV HTTP_PORT=8080
ENV NODE_ENV=production
#ENV DB_HOST=10.0.0.1
#EXPOSE 8080
CMD ["node", "index.js"]
With that the dependencies are "build" and copied from the build container inside your node.js image.这样,依赖项就会“构建”并从 node.js 映像中的构建容器复制。
More to read:更多阅读:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.