首页 > 解决方案 > 运行 Docker 容器时无法显示我的 React UI

问题描述

我有一个想要容器化的 React 客户端和 Go 服务器应用程序。这是我第一次使用 Docker,所以我很难让它工作。我的 Dockerfile 看起来像我从指南中获得的

# Build the Go API-
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .

# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build

# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main

我的服务器代码采用PORT环境变量来设置要监听的端口,因此我使用了以下 Docker 运行命令 docker run -p 3000:8080 --env PORT=8080 test1:但是,当我转到localhost:3000时,我得到一个404 page not found响应,而不是我的 React 应用程序。我已经尝试了几种不同的 run 命令组合,但我似乎无法弄清楚如何让我的 UI 出现。这与我的 Dockerfile 有关还是我的运行命令缺少参数?或两者?

标签: node.jsreactjsdockergo

解决方案


推荐阅读