如何在 docker-compose 中制作反应应用程序?构建步骤完成后容器正在退出

Mtn*_*sty 2 node.js npm docker reactjs docker-compose

我正在尝试在 docker-compose 中制作一个简单的 React 应用程序。我正在使用此参考 我所做的是运行npx create-react-app frontend以生成默认的 React 应用程序。然后我添加了 Dockerfile。这一切都在一个目录中frontend

#Dockerfile

FROM node:14.9

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent

# add app
COPY . ./

# start app
CMD ["npm", "start"]
Run Code Online (Sandbox Code Playgroud)

在上面的目录中,我有我的 docker-compose

#docker-compose.yml

version: '3.7'

services:

  frontend:
    container_name: frontend
    build: ./frontend
    volumes:
      - './frontend:/app'
      - '/app/node_modules'
    ports:
      - 3000:3000
    environment:
      - CHOKIDAR_USEPOLLING=true
Run Code Online (Sandbox Code Playgroud)

我运行docker-compose up --build,在正常的构建过程之后,我收到此消息。

docker-compose up --build
Building frontend
Step 1/9 : FROM node:14.9
 ---> 1b2c72215052
Step 2/9 : WORKDIR /app
 ---> Using cache
 ---> 2bab04404275
Step 3/9 : ENV PATH /app/node_modules/.bin:$PATH
 ---> Using cache
 ---> ff4ce5f5ec47
Step 4/9 : COPY package.json ./
 ---> Using cache
 ---> b1d25c0b6c05
Step 5/9 : COPY package-lock.json ./
 ---> Using cache
 ---> 5b829feaf00d
Step 6/9 : RUN npm install --silent
 ---> Using cache
 ---> 835367c47253
Step 7/9 : RUN npm install react-scripts@3.4.1 -g --silent
 ---> Using cache
 ---> 015ccb2db237
Step 8/9 : COPY . ./
 ---> Using cache
 ---> e4a5285339b5
Step 9/9 : CMD ["npm", "start"]
 ---> Using cache
 ---> 3f91b16d34d6

Successfully built 3f91b16d34d6
Successfully tagged projectname_frontend:latest
Recreating frontend ... done
Attaching to frontend
frontend    |
frontend    | > frontend@0.1.0 start /app
frontend    | > react-scripts start
frontend    |
frontend    | ? ?wds?: Project is running at http://172.29.0.2/
frontend    | ? ?wds?: webpack output is served from
frontend    | ? ?wds?: Content not from webpack is served from /app/public
frontend    | ? ?wds?: 404s will fallback to /
frontend    | Starting the development server...
frontend    |
frontend exited with code 0
Run Code Online (Sandbox Code Playgroud)

似乎容器只是退出而没有错误消息。我不知道是什么原因造成的。 Docker version 19.03.12, build 48a66213fe

值得注意的是,在过去的几个月里,我已经能够在没有以下解决方案的情况下成功构建我的 React 应用程序。对解决方案中给出的命令的需求直到最近才成为我的一个问题。

Mtn*_*sty 7

我想我解决了这个问题。添加stdin_open: true到 docker-compose.yml 是解决方案的一半。我还补充道command: npm start。在此之后,容器停止退出。我认为 Dockerfile 中的命令就足够了。它现在似乎正在工作。

docker-compose.yml

version: '3.7'

services:

  frontend:
    container_name: frontend
    build: ./frontend
    volumes:
      - './:/app'
      - '/app/node_modules'
    ports:
      - 3000:3000
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    command: npm start
Run Code Online (Sandbox Code Playgroud)

文件

FROM node:14.9
 
WORKDIR /usr/src/app
 
COPY package*.json ./
 
RUN npm install
 
COPY . .
 
EXPOSE 3000
 
CMD [ "npm", "start" ]
Run Code Online (Sandbox Code Playgroud)


k-w*_*ski 5

这个简单Dockerfile总是对我有用:

FROM node:10
 
WORKDIR /usr/src/app
 
COPY package*.json ./
 
RUN npm install
 
COPY . .
 
EXPOSE 8083
 
CMD [ "npm", "start" ]
Run Code Online (Sandbox Code Playgroud)

我也喜欢在stdin_open: true我的容器中添加docker-compose.yml.