node.js - 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'
}
解决方案
推荐阅读
- c# - 是否可以将 MahApps 的 TextBoxHelper 应用于 ScrollViewer?
- android - 如何在用户单击 Android 上的按钮时读取 NFC 标签
- laravel - 无法弄清楚连接
- asciimath - AsciiMath 的局限性
- java - 递归函数因“此方法必须返回 int 类型的结果”而失败
- python - GCP云函数python-GCS复制文件-重复文件
- elasticsearch - Elasticsearch 6.2.4 不提供数据进行查询
- python - 由于 SELECT ValueError,继承 Airflow OracleHook 以设置 cx_Oracle outputtypehandler
- angular - 管道没有返回正确的值
- python - 自动化无聊的东西 - 图片网站下载器