【问题标题】:Docker Compose with React and NginxDocker Compose 与 React 和 Nginx
【发布时间】:2020-07-23 17:11:43
【问题描述】:

我正在尝试使用 docker-compose 部署我的 React 应用程序,该应用程序使用 express 后端和 Postgres 数据库。我的想法是从我的 docker-compose 共享卷。然后从我的 Dockerfile 构建到卷中,以便 Nginx 能够提供文件。现在的问题是,当我第一次构建项目时它可以工作,但是如果我在我的 React 客户端中更改某些内容并运行“docker-compose up --build”,看起来一切都在按应有的方式构建,但文件已提供还是一样。我的 dockerfile 中的 COPY 命令是否不会覆盖旧文件?

我的 React 客户端项目中的 Dockerfile

FROM node:13.12.0-alpine as build
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM node:13.12.0-alpine
COPY --from=build /app/build /var/lib/frontend

码头工人撰写

version: "3.7"
services:
 callstat_backend:
  build: ./callstat-backend
  restart: always
  ports:
    - "3000:3000"
  env_file:
   - keys.env
  depends_on:
  - postgres
 callstat_frontend:
  build: ./callstat-client
  volumes:
   - frontend/:/var/lib/frontend
 postgres:
  image: postgres:11.2-alpine
  ports:
   - "5432:5432"
  volumes:
   - pgdata:/var/lib/postgresql/data
  environment:
   POSTGRES_USER: postgres
   POSTGRES_PASSWORD: postgres
   POSTGRES_DB: callstat
 nginx:
  image: nginx
  volumes:
   - frontend:/usr/share/nginx/html
   - ./nginx.conf:/etc/nginx/conf.d/default.conf
  ports:
   - "80:80"
  depends_on:
   - callstat_frontend
volumes:
 pgdata:
 frontend:

也许我在这里采取了完全错误的方法?

【问题讨论】:

  • docker 不会自动刷新已经存在的卷。因此,当您第二次执行 docker_compose up -d 时,由于该卷已经存在,因此将按原样使用它
  • 我可以改变一些东西来实现这一点吗?我正在运行“docker-compose up --build”,我可以看到构建过程正在发生。但是卷中的文件自第一次启动以来根本没有改变。我可以采取任何其他模式来解决这个问题吗?

标签: reactjs docker nginx docker-compose dockerfile


【解决方案1】:

您可以按以下顺序运行命令:

# stop down the services
docker-compose stop

# remove the previously created docker resources
docker-compose rm

# bring up the services again
docker-compose up --build

这是您之前的卷被删除,新卷将使用更新的更改创建。

注意:从开发的角度来看,这没问题,但 docker 卷确实希望在部署之间保持不变。对于像代码更改这样的工件,理想情况下,图像应该作为构建过程的一部分发布。要更深入地了解这个主题,您可以参考https://github.com/docker/compose/issues/2127

【讨论】: