首页 > 解决方案 > 在 React Container 中使用一个 NodeJs docker 镜像

问题描述

第 1步:我创建了一个 NodeJS 应用程序的本地 docker 映像。这是此应用程序的 dockerfile -

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

#EXPOSE 8080
CMD [ "npm", "start" ]

**第 2 步:**然后我为这个 Node 应用程序构建了一个 docker 镜像。这是构建命令输出 -

C:\Users\shibathethinker\Documents\GitHub\NodeProjects\API_Proxy_ABN>docker build -t api-proxy .
Sending build context to Docker daemon  8.637MB
Step 1/6 : FROM node:8
 ---> 0bf36d7ccc1e
Step 2/6 : WORKDIR /usr/src/app
 ---> Running in 7187d65639f1
Removing intermediate container 7187d65639f1
 ---> 0e34dc93439c
Step 3/6 : COPY package*.json ./
 ---> 47c0d0ca8c77
Step 4/6 : RUN npm install
 ---> Running in d7e5163371df
npm WARN api_proxy@1.0.0 No repository field.

added 98 packages from 91 contributors and audited 194 packages in 8.598s
found 0 vulnerabilities

Removing intermediate container d7e5163371df
 ---> 72da705ae792
Step 5/6 : COPY . .
 ---> 0293df6aa27d
Step 6/6 : CMD [ "npm", "start" ]
 ---> Running in 251e98c0a0ae
Removing intermediate container 251e98c0a0ae
 ---> a92d8a95b8cd
Successfully built a92d8a95b8cd
Successfully tagged api-proxy:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

**第 3 步:**然后,我想在另一个“React”应用程序中使用这个 docker 镜像。这是应用程序的 Dockerfile -

 FROM   api-proxy:latest
WORKDIR /app
RUN npm install
CMD [ "npm", "start" ]

# stage: 2 — the production environment
FROM nginx:alpine
#COPY —from=react-build /app/build /usr/share/nginx/html
#COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY /build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

第 4 步:现在我构建并运行了第 3 步生成的这个 docker 映像。

问题:

看起来节点应用程序没有在新创建的 docker 容器上运行。如果我 'ssh' 进入 docker 容器,我看不到任何节点服务器在那里运行。我也找不到在此容器的 step1 中创建的 WORKDIR (/usr/src/app)。

我做错了什么?如果我能进一步澄清,请告诉我。

标签: dockerdockerfile

解决方案


您正在执行多阶段 docker 构建。您正在使用 nodejs(下载依赖项和缩小构建)构建您的应用程序,并在 nginx Web 服务器上复制和运行它。


推荐阅读