docker - 在 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)。
我做错了什么?如果我能进一步澄清,请告诉我。
解决方案
您正在执行多阶段 docker 构建。您正在使用 nodejs(下载依赖项和缩小构建)构建您的应用程序,并在 nginx Web 服务器上复制和运行它。
推荐阅读
- javascript - 在为外部使用而设计的 JavaScript 库中使用 redux
- python - 在 Numba 函数中切片 Numpy 数组
- python - 当使用 curses.getkey() 箭头键在命令提示符和 vscode 集成终端中返回不同的值?
- javascript - Dayjs:每天对数据进行分组,同时仍保持格式
- spring-boot - Spring boot多个数据库通过集成测试导致NoSuchBeanDefinitionException
- python - 在不使用破折号的情况下创建多选下拉菜单
- javascript - Postman - 将项目添加到集合变量数组
- javascript - 从 api 访问数据
- excel - 使用 UserInterFaceOnly:=True 的 VBA 表保护不允许复制和粘贴
- excel - 连接到共享邮箱收件箱的子文件夹