如何为AWS Amplify React应用程序创建Docker文件

问题描述

上下文: 我已经为在AWS上运行的React应用程序设置了基本代码。我添加了身份验证和Graphql工具。基本上,我完成了本教程中指定的所有操作:https://aws.amazon.com/getting-started/hands-on/build-react-app-amplify-graphql/

我想对我的应用程序进行Docker化,并根据本教程https://medium.com/better-programming/heres-how-you-can-use-docker-with-create-react-app-3ee3a972b04e添加了Dockerfile,Dockerfile.dev和docker-compose.yml文件。我会将Dockerfile的内容在这文章底部

问题: 我在包含我的React应用程序的目录中,并运行了docker-compose up命令,但是我收到很多警告,我将在此段下面放置警告。如我在docker-compose.yml中指定的,我的应用程序在端口3000上成功运行。我的问题是我是否错过了导致Docker抛出所有这些警告的Dockerfile中的关键代码段。我已经将Docker用于我在实习期间成功构建的React应用程序,但是如果我在AWS上运行React应用程序,我不确定是否需要添加任何其他内容。如果有人有任何建议,将不胜感激。请让我知道我是否遗漏了一些关键信息,请原谅我,因为我对使用Docker还是很陌生。

Dockerfile

FROM node:13.12.0-alpine
workdir /app
ENV PATH /app/node_modules/.bin:$PATH
copY package.json ./
copY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent
copY . ./
CMD ["npm","start"]

Dockerfile.dev

FROM node:alpine
workdir /app
copY package.json /app
RUN npm install
copY . /app
CMD ["npm","run","start"]

docker-compose.yml

version: "3.8"
services:
    client:
        stdin_open: true
        build:
            context: .
            dockerfile: Dockerfile.dev
        ports:
            - "3000:3000"
        volumes:
            - "/app/node_modules"
            - "./:/app"

Docker Terminal警告

Step 4/6 : RUN npm install
 ---> Running in e245d3bb0bc3
npm WARN deprecated eslint-loader@3.0.3: This loader has been deprecated. Please use eslint-webpack-plugin
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated @types/testing-library__dom@7.5.0: This is a stub types deFinition. testing-library__dom provides its own type deFinitions,so you do not need this installed.
npm WARN deprecated request@2.88.2: request has been deprecated,see 

https://github.com/request/request/issues/3142
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the Now deprecated request package,see https://github.com/request/request/issues/3142
npm WARN deprecated @hapi/joi@15.1.1: joi is leaving the @hapi organization and moving back to 'joi' (https://github.com/sideway/joi/issues/2411)
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and Could be using insecure binaries. Upgrade to fsevents 2.
npm WARN deprecated buffer@4.9.1: This version of 'buffer' is out-of-date. You must update to v4.9.2 or newer
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated @hapi/address@2.1.4: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please,upgrade your dependencies to the actual version of core-js@3.
npm WARN deprecated left-pad@1.3.0: use String.prototype.padStart()
...
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/react-scripts/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/jest-haste-map/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"x64"})
npm WARN notsup Unsupported engine for watchpack-chokidar2@2.0.0: wanted: {"node":"<8.10.0"} (current: {"node":"14.13.1","npm":"6.14.8"})
npm WARN notsup Not compatible with your version of node/npm: watchpack-chokidar2@2.0.0
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"x64"})
npm WARN react-native-get-random-values@1.5.0 requires a peer of react-native@>=0.56 but none is installed. You must install peer dependencies yourself.
npm WARN @aws-amplify/datastore@2.6.0 requires a peer of @react-native-community/netinfo@^5.5.0 but none is installed. You must install peer dependencies yourself.
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer 
dependencies yourself.

added 1825 packages from 1181 contributors and audited 1829 packages in 53.175s

解决方法

这些警告与正在Docker.dev文件中添加到Docker映像中的npm软件包有关,步骤4-res.redirect('the-url'); ,实际上并不是docker警告。

无论是否已容器化,这些软件包都是react应用程序的一部分,因此,如果npm install是从本地终端运行的,警告也会显示。

如果您希望隐藏警告,则添加npm install(或者使用--silent-s),就像在步骤6中Dockerfile中的内容一样,将隐藏这些警告。