使用 Docker 在 CentOS 7 服务器上部署 ReactJS 应用程序

Deploy ReactJS application on CentOS 7 server using Docker

我已经在 CentOS 7 服务器上部署了一个 ReactJS(带有 neo4j 数据库)应用程序。我想使用 docker 在同一台服务器上部署同一应用程序的另一个实例。我已经在服务器(CentOS 7)上安装了docker(版本20.10.12)。

在服务器上,我克隆了我的 reactjs 项目并创建了以下 Docker 文件:

FROM node:16 as builder
WORKDIR /app

COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
RUN npm run build

FROM httpd:alpine
WORKDIR /var/www/html
COPY --from=builder /app/build/ .

和以下 docker-compose.yaml 文件:

version: '3.1'
services:
  app-test:
    hostname: my-app-test.com
    build: .
    ports:
      - '80:80'
    command: nohup node server.js &
    networks:
      - app-test-network
  app-test-neo4j:
    image: neo4j:4.4.2
    ports:
      - '7474:7474'
      - '7473:7473'
      - '7687:7687'
    volumes:
      - ./volumes/neo4j/data:/data
      - ./volumes/neo4j/conf:/conf
      - ./volumes/neo4j/plugins:/plugins
      - ./volumes/neo4j/logs:/logs
      - ./volumes/neo4j/import:/import
      - ./volumes/neo4j/init:/init
    networks:
      - app-test-network
    environment:
      - NEO4J_AUTH=neo/password
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
      - NEO4J_dbms_security_procedures_whitelist=apoc.*
      - NEO4J_dbms_default__listen__address=0.0.0.0
      - NEO4J_dbms_connector_bolt_listen__address=:7687
      - NEO4J_dbms_connector_http_listen__address=:7474
      - NEO4J_dbms_connector_bolt_advertised__address=:7687
      - NEO4J_dbms_connector_http_advertised__address=:7474
      - NEO4J_dbms_default__database=neo4j
      - NEO4JLABS_PLUGINS=["apoc"]
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_apoc_export_file_enabled=true
      - NEO4J_apoc_import_file_use__neo4j__config=true
      - NEO4J_dbms_shell_enabled=true
networks:
  app-test-network:
    driver: bridge

但是,在 运行 docker-compose up 之后,我得到以下错误:

ERROR: for app-repo_app-test Cannot start service app-test: driver failed programming external connectivity on endpoint app-repo_app-test (2cffe4fa4299d6e53a784f7f564dfa49d1a2cb82e4b599391b2a3206563d0e47): ErroCreating app-repo_app-test-neo4j ... done

ERROR: for app-test  Cannot start service app-test: driver failed programming external connectivity on endpoint app-repo_app-test (2cffe4fa4299d6e53a784f7f564dfa49d1a2cb82e4b599391b2a3206563d0e47): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use
ERROR: Encountered errors while bringing up the project.

谁能告诉我这里出了什么问题?而且,使用 Docker 在 CentOS 7 服务器上部署 reactjs 应用程序是正确的方法吗?

正如我在上面的评论中所说:您没有使用 Multi-Stage 容器中的第一个容器。

查看这篇文章:https://docs.docker.com/develop/develop-images/multistage-build/#name-your-build-stages

特别是示例代码:

# syntax=docker/dockerfile:1
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go    ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"] 

COPY --from=builder 将为您解决问题。