Docker无法连接到本地主机

x33g5p2x  于 2022-11-03  发布在  Docker
关注(0)|答案(1)|浏览(236)

我遇到了一个问题,无法从我的浏览器localhost:8000访问我的docker容器。没有错误消息。以下是浏览器的说明:This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE
这是我的docker-compose文件:

version: "3.7"

services:
  postgres:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=postgres

  fastapi:
    build: ./backend
    ports:
      - "8000:8000"
    volumes:
      - ./backend/:/usr/src/backend/
    depends_on: 
      - postgres

volumes:  
  postgres_data:

这是我的dockerfile:


# pull official base image

FROM python:3.8.3-slim-buster

# set work directory

WORKDIR /usr/src/backend

# set environment variables

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# copy project

COPY . .

# install dependencies

RUN pip install --upgrade pip
COPY ./requirements.txt .

# Installing dependencies, remove those that are not needed after the installation

RUN pip install -r requirements.txt

CMD uvicorn main:app --reload

下面是我的CLI:

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

INFO: Started reloader process [6] using statreload

INFO: Started server process [8]

INFO: Waiting for application startup.

INFO: Application startup complete.
moiiocjp

moiiocjp1#

如果有人在使用FastAPI时遇到此问题,
尝试在启动脚本中添加--host 0.0.0.0
示例:uvicorn main:app --reload --host 0.0.0.0 --port 8000

相关问题