首页 > 解决方案 > Nginx 不能与 docker-compose 一起用于 React 应用程序

问题描述

我已经创建了相同的 nginx 配置、DockerFile、Docker-compose 文件。

nginx/nginx.conf

 server {
  listen 80;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
}

DockerFile

FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --silent
RUN npm install react-scripts@3.4.1 -g --silent
COPY . ./
RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

码头工人-compose.yml

version: "3.7"
services:
  client:
    container_name: client
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3001:3000

现在做完之后docker-compose up --build

我得到的日志为

client    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
client    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
client    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
client    | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
client    | 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packages version
client    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
client    | /docker-entrypoint.sh: Configuration complete; ready for start up

我不确定,如果问题是由于不同的软件包版本或其他原因造成的,但是当尝试访问该 url 时,它说无法访问该站点。

标签: dockernginxdocker-compose

解决方案


您没有指定您使用的 url,但看起来您的 nginx 在端口 80 上公开。您需要在 docker compose 中公开该端口。包括在端口下:

- 80:80

推荐阅读