Docker容器:无法连接react到python

问题描述 投票:0回答:1

如果有人能解决我的以下问题,我将不胜感激。

我正在尝试使用容器技术同时学习react/python/nosql。我创建了一个 docker-compose.yaml 文件来构建react/python/nosql。我已成功使用 URL http://local-dynamodb:8000/

从 python-hug 应用程序连接到本地 dynamdb

使用相同的概念,我尝试使用 URL http://books-python_hug:8080/books 从 React 连接到 python-hug 应用程序,但无济于事

docker-compose.yaml


networks:
  my_network:
    driver: bridge

services:
  dynamo_db:
    container_name: local-dynamodb
    image: amazon/dynamodb-local
    ports:
      - "8001:8000"
    volumes:
      - dynamodata:/home/dynamodblocal
    working_dir: /home/dynamodblocal
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ."
    networks:
      - my_network

  python_hug:
    build:
      context: ./python
      dockerfile: Dockerfile
    container_name: books-python_hug
    ports:
      - "8080:8080"
    depends_on:
      - dynamo_db
    networks:
      - my_network

  react:
    build:
      context: ./react
      dockerfile: Dockerfile
    container_name: books-react

    ports:
      - "3000:3000"
    networks:
      - my_network

volumes:
  dynamodata:

python 的 Dockerfile


# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the Python script into the container at /app
COPY . /app/

# Mount local AWS credentials file into the container
COPY ./credentials/credentials /root/.aws/credentials

# Install hug
RUN pip install --no-cache-dir hug
# Install boto3
RUN pip install --no-cache-dir boto3


# Expose the port hug listens on
EXPOSE 8080

# Run hug when the container launches
CMD ["hug", "-f", "app.py", "-p", "8080"]

react 的 Dockerfile



# Use official Node.js image as base
FROM node:16

# Set working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to container
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Define the command to run your app
CMD ["npm", "start"]

python reactjs docker
1个回答
0
投票

希望这对您的项目有所帮助。

这是我项目的 docker 文件,您需要更改一些值字段以适合您的项目。

使用堆栈React、Python、graphene、apollo-client、dynamo localdb

docker-compose.yml

version: "3.7"

services:
  frontend:
    image: gluck0101/mini-kanban-frontend:latest
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app/frontend
    environment:
      - REACT_APP_GRAPHQL_API_URL=http://backend:5000/graphql
    depends_on:
      - backend

  backend:
    image: gluck0101/mini-kanban-backend:latest
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - "5000:5000"
    volumes:
      - ./backend:/app/backend
    environment:
      - DYNAMODB_ENDPOINT=http://db:8000
    depends_on:
      - db

  db:
    image: amazon/dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - dynamodb_data:/home/dynamodblocal
    command: ["-jar", "DynamoDBLocal.jar", "-sharedDb"]

volumes:
  dynamodb_data:

后端Python石墨烯

# Dockerfile for backend service
FROM python:3.10.12-slim-bullseye as base

# Define a build argument named PROJECT with a default value of 'api'.
ARG PROJECT=backend

# Create a non-root user to run the app with.
RUN groupadd --gid 1000 user &&  adduser --disabled-password --gecos '' --uid 1000 --gid 1000 user

# Set the working directory for the subsequent commands.
WORKDIR /app


# Switch to the non-root user for security purposes.
USER user

# Create a new stage named 'dev' based on the 'base' stage.
FROM base as dev

USER root
# Copy the requirements.txt file and install the Python dependencies.
COPY --chown=user:user ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

USER user
# Copy the project files into the container.
COPY --chown=user:user . ./
# Define the command to run when the container starts.
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000", "--    reload"]

# Create a new stage named 'test' based on the 'base' stage.
FROM base as test

# Copy the requirements.txt file and install the Python dependencies.
COPY --chown=user:user /backend/requirements-test.txt ./
RUN pip install --no-cache-dir -r requirements-test.txt

USER root
# Copy the project files into the container.
COPY --chown=user:user ./$PROJECT /app/$PROJECT
# Create a directory for mypy cache and change its ownership to the non-    root user.
RUN mkdir /app/.mypy_cache && chown user:user -R /app/.mypy_cache
# Install pytest within the test stage
RUN pip install pytest

# Define the default command to run when the container starts in the test     stage
CMD ["pytest"]
# Switch back to the non-root user
USER user

# Set the default target stage to 'dev'.
FROM dev

前端反应

# Use official Node.js image as the base image
FROM node:16 as base

# Define a build argument named PROJECT with a default value of 'api'.
ARG PROJECT=frontend

# Set the working directory in the container
WORKDIR /app


FROM base as dev

# Copy package.json to the container
COPY ./package*.json ./
# Install project dependencies
RUN npm install --legacy-peer-deps

# Copy all files from the current directory to the container
COPY . ./
# Start the app
CMD ["npm", "start"]


FROM base as test

# Copy package.json to the container
COPY /frontend/package*.json ./
# Install project dependencies
RUN npm install --legacy-peer-deps

# Copy all files from the current directory to the container
COPY ./$PROJECT ./
# Build the React app
RUN npm run build
# Start the test
CMD [ "npm", "test" ]

# Set the default target stage to 'dev'.
FROM dev

问候。

© www.soinside.com 2019 - 2024. All rights reserved.