【问题标题】:docker-compose stack ( React + Flask )docker-compose 堆栈(反应 + 烧瓶)
【发布时间】:2021-09-12 22:12:04
【问题描述】:

这是我第一次使用 Docker,更不用说 docker-compose。 我能否对我的 docker-compose 文件有所了解,以帮助我启动并运行两个 docker 容器。

文件夹结构

我的两个 Dockerfile 都可以分别运行每个应用程序(React 或 Flask)。例如:

反应容器

docker build -t wedding-client .
docker run -dp 3030:3030 wedding-client 

烧瓶容器

docker build -t wedding-server .
docker run -dp 5030:5030 wedding-server 

所以我认为我的 docker-compose 文件是问题所在。以下是 docker 文件:

客户端/DockerFile

FROM node:alpine as build
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build

FROM nginx:alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY --from=build /app/build .
COPY nginx/nginx.conf /etc/nginx/conf.d
EXPOSE 3030
CMD ["nginx", "-g", "daemon off;"]

服务器/DockerFile

FROM python:3.9

WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
ENV FLASK_APP server.py
EXPOSE 5030
CMD ["python", "server.py"]

docker-compose.yml

version: '3'
services:
  server:
    container_name: wedding-server
    build:
      context: server/
      dockerfile: Dockerfile
    network_mode: host
    ports:
      - 5030:5030
  client:
    container_name: wedding-client
    build:
      context: client/
      dockerfile: Dockerfile
    network_mode: host
    ports:
      - 3030:3030
    depends_on:
      - server

Docker 输出:

【问题讨论】:

  • 为什么在这个设置中禁用 Docker 网络?您是否成功连接到该容器以取回该 PNG 文件?还是您遇到(文本)错误? (看起来像是运行 flask run 的屏幕截图,但您的 Dockerfile 指定了不同的 CMD。)
  • 我使用 network_mode: host 因为我需要我的 docker 容器连接到我在 docker 之外的网络上的服务。您是否建议我在 Flask Dockerfile 中删除 CMD ["python", "server.py"] 并在我的 docker-compose.yml 文件中添加命令:flask run?我能够看到这个输出,因为我正在运行 docker compose up 而没有 -d 标志
  • 您不应该为此需要主机网络。将 CMD 放入 Dockerfile 比将其放入 docker-compose.yml 更好。 docker-compose up 的实际输出是什么(作为文本,而不是图像)?它是否与您在 Dockerfile 中的 CMD 匹配?

标签: reactjs docker flask docker-compose


【解决方案1】:

客户端 Docker 文件

# Stage 0, "build-stage", based on Node.js to build the frontend
FROM node:alpine as build
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY . /app/
RUN npm run build

# Stage 1, based on NGINX to provide a configuration to be used with react-router
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
COPY ./nginx/nginx.conf /etc/nginx/conf.d
# RUN apk update && apk add bash
EXPOSE 3005
CMD ["nginx", "-g", "daemon off;"]

Flask Docker 文件

FROM python:3.9
RUN mkdir /server
WORKDIR /server
COPY requirements.txt /server/requirements.txt
RUN pip install --upgrade pip && \
    pip install -r requirements.txt
COPY . .

docker-compose.yml

version: '3'

services:
  api:
    build: server
    command: "flask run --host=0.0.0.0 --port=5005"
    environment:
      - FLASK_ENV=production
      - FLASK_APP=server.py
    ports:
      - "5005:5005"

  client:
    build: client
    ports:
      - '3005:3005'
    links:
      - api
    depends_on:
      - api

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多