vue-fastapi 容器化部署经验总结
1、代码目录
project/
├── backend/
│ ├── app/
│ │ └── main.py
│ ├── requirements.txt
│ └── Dockerfile
├── frontend/
│ ├── src/
│ │ └── App.vue
│ ├── Dockerfile
│ └── package.json
└── docker-compose.yml
2、问题解决
2.1 docker-compose 版本不对,改成3.3即可
[root@localhost fastapi_web]# docker-compose up --build
ERROR: Version in "./docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the services key, or omit the version key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
2.2 docker服务没有启动
[root@localhost fastapi_web]# docker-compose up --build
ERROR: Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
2.3 docker-compose up --build 编译失败
增加main.js 已经解决
2.4 前端界面发消息到后端报错
CORS(Cross-Origin Resource Sharing):一种机制,允许服务器明确声明哪些外部域名可以访问其资源。如果服务器没有正确配置 CORS,浏览器会阻止跨域请求。
后端fastapi需要做修改
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/. (Reason: CORS request did not succeed). Status code: (null).
2.5 后端web访问一直报错
原因是需要在dockerfile中暴露后端的端口
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# 暴露端口
EXPOSE 8001
# 定义环境变量
ENV NAME World
COPY app/ app/
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]
[root@localhost fastapi_web]# docker-compose up backend
Creating network "fastapiweb_my_network" with the default driver
Creating fastapiweb_backend_1 ... done
Attaching to fastapiweb_backend_1
backend_1 | INFO: Started server process [1]
backend_1 | INFO: Waiting for application startup.
backend_1 | INFO: Application startup complete.
backend_1 | INFO: Uvicorn running on http://0.0.0.1:8001 (Press CTRL+C to quit)
运行时正常的,但是在浏览器登录 http://0.0.0.1:8001 会报错:
[root@localhost fastapi_web]# curl -f http://localhost:8001
curl: (56) Recv failure: Connection reset by peer

浙公网安备 33010602011771号