【问题标题】:Docker: How do I use access a service that's in another container from the frontend?Docker:我如何使用从前端访问另一个容器中的服务?
【发布时间】:2020-07-10 01:53:12
【问题描述】:

我在 8000 端口上运行 RESTful Django 项目,在 3000 端口上运行 React 项目。
对于开发,我将所有的网址都放在了前端 href='localhost:8000/api/name'href='localhost:8000/api/address'

现在我要投入生产了,我希望我的 href 是 href='mysite.com/api/name'href='mysite.com/api/address'。我不知道该怎么做。

如何访问我的 RESTful 数据哪个在另一个容器上?
我找到了这个article,但认为它不适合生产。

docker-compose.yml


version: "3.2"
services:
  backend:
    build: ./backend
    volumes:
      - ./backend:/app/backend
    ports:
      - "8000:8000"
    stdin_open: true
    tty: true
    command: python3 manage.py runserver 0.0.0.0:8000
    depends_on:
      - db
      - cache
    links:
      - db
  frontend:
    build: ./frontend
    volumes:
      - ./frontend:/app
       #One-way volume to use node_modules from inside image
      - /app/node_modules
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
      - CHOKIDAR_USEPOLLING=true
    depends_on:
      - backend
    tty: true
    command: npm start
  
  db:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - "./mysql:/var/lib/mysql"
      - "./.data/conf:/etc/mysql/conf.d"
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: temp
      MYSQL_USER: root
      MYSQL_PASSWORD: root

volumes:
    mysql: {}

【问题讨论】:

  • 你如何为你的 react 前端提供服务?你没有为此使用你的 Django 后端吗?

标签: django reactjs docker


【解决方案1】:

您可以将例如 API_URL(prod 为“mysite.com/api/”,dev 为“localhost:8000/api/”)作为环境变量进行响应,并使用它来制作 url

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-09
    • 2019-04-28
    • 1970-01-01
    • 2023-02-24
    • 2017-07-12
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    相关资源
    最近更新 更多