在github代码空间上使用docker文件会导致502 bad gateway

w1jd8yoj  于 2023-04-20  发布在  Docker
关注(0)|答案(1)|浏览(217)

我有一个fastapi python脚本,当我使用以下命令时,它可以在代码空间上工作:

uvicorn main:fast_API_app --reload

出现以下代码,我的API工作正常:

INFO:     Will watch for changes in these directories: ['/workspaces/WebAPI']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [3229] using WatchFiles
INFO:     Started server process [3241]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

导致
Running it in github codespaces works fine
但是,当我将其转换为docker容器时,运行它会导致502 Bad Gateway
终端:

docker container run <username>/<container name>:v0.0.1
INFO:     Started server process [1]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

我在github代码空间中选择端口8000是公共端口还是私有端口没有区别。
下面是我的Dockerfile,用于构建镜像。

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["uvicorn", "main:fast_API_app", "--host", "0.0.0.0", "--port","8000"]

这将导致以下错误:
image of the error
它不显示错误代码。
我已经尝试过了(但可能也做错了):

  • 我试着暴露不同的端口
  • 我试着用独角兽代替独角兽
  • 在www.example.com上搜索Stackoverflow.comdocker代码空间和bad gateway
  • 将端口前向切换为public / private
  • 将端口协议更改为https
  • 多次重建容器
ruarlubt

ruarlubt1#

谢谢你@Hedde
一些重命名并将其更改为:

CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "--access-logfile", "-", "--error-logfile", "-", "main:app"]

成功了

相关问题