首页 > 解决方案 > docker-compose 堆栈(反应 + 烧瓶)

问题描述

这是我第一次使用 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 文件是问题所在。这是码头工人文件:

客户端/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"]

码头工人-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

码头工人输出:

码头工人输出

标签: reactjsdockerflaskdocker-compose

解决方案


客户端 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;"]

烧瓶 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 . .

码头工人-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

推荐阅读