首页 > 解决方案 > Docker nuxt js 和 django 出现错误(Econnrefused)

问题描述

我目前正在使用 django 和 nuxt js 开发 docker。我可以从 asyncData 或 nuxtServerInit 中的https://jsonplaceholder.typicode.com/posts获取 json 数据,它可以正确获取并且没有错误。但是当我想从我的 django rest api 服务获取错误时(添加了 cors 标头)。我测试了使用 created() 钩子从 django rest api 获取帖子并且它正在工作。我不明白为什么它会出错。

页面/index.vue

export default {
  asyncData(context){
    return context.app.$axios.$get('http://127.0.0.1:8000') // this is throwing an error
      .then((res) => {
        let posts = [];
        posts = res;
        return { posts: res }
      })
  },
  created() {
    this.$axios.$get('http://127.0.0.1:8000') // this is not throwing any error
      .then(e => {console.log(e);})
  }
}

我的 docker 编译器

version: '3'

networks: 
  main:
    driver: bridge

services: 
  api:
    container_name: blog_api
    build: 
      context: ./backend
    ports: 
      - "8000:8000"
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"
    volumes: 
      - ./backend:/app
    networks: 
      - main

  web:
    container_name: blog_web
    build: 
      context: ./frontend
    ports: 
      - "8080:8080"
    volumes: 
      - ./frontend:/code
    networks: 
      - main

后端 docker 文件

FROM python:3.8-alpine

ENV PYTHONBUFFERED 1
ENV PYTHONWRITEBYTECODE 1

RUN mkdir /app
WORKDIR /app

COPY ./requirements.txt /app/
RUN pip install -r requirements.txt

EXPOSE 8000

ADD . /app

前端 dockerfile

FROM node:13-alpine

WORKDIR /code/

COPY . . 
EXPOSE 8080

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=8080


RUN npm install
CMD ["npm", "run", "dev"]

Nuxt-config.js

server: {
    port: 8080,
    host: '0.0.0.0'
  }

标签: node.jsdjangodockernuxt.js

解决方案


推荐阅读