Моя структура приложения проста:
- клиент/
статический/
upload.html
Dockerfile
[*]сервер/
- Dockerfile:
- models.py
- requirements.txt
< /ul> - docker-compose.yaml:
Код: Выделить всё
SQLALCHEMY_DATABASE_URL = \
"postgresql://postgres:somepass@db/server_coords"
# SQLALCHEMY_DATABASE_URL = \
# "postgresql://postgres:somepass@localhost/server_coords"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
app = FastAPI()
# app.mount("/static", StaticFiles(directory="client/static"), name="static")
@app.get("/")
async def get_upload_page() -> FileResponse:
return FileResponse("client/static/upload.html")
Код: Выделить всё
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY static/upload.html index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Код: Выделить всё
FROM python:3.11
WORKDIR server
COPY requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Код: Выделить всё
version: '3'
volumes:
pg_data:
services:
server:
build:
context: ./server
ports:
- "8000:8000"
networks:
- test_network
db:
image: postgres:13.10
env_file: .env
volumes:
- pg_data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_DB: server_coords
POSTGRES_USER: postgres
POSTGRES_PASSWORD: somepass
networks:
- test_network
db_init:
image: postgres:13.10
env_file: .env
command: [ "bash", "-c", "sleep 5 && PGPASSWORD=somepass psql -h db -U postgres -w -c 'CREATE DATABASE server_coords;'" ]
depends_on:
- db
backend:
build: ./server/
env_file: .env
depends_on:
- db
ports:
- "8001:8000"
networks:
- test_network
frontend:
env_file: .env
build: ./client/
networks:
test_network:
driver: bridge
Код: Выделить всё
✔ Network route_calculation_fast_api_default Created 0.1s
✔ Network route_calculation_fast_api_test_network Created 0.1s
✔ Container route_calculation_fast_api-db-1 Created 0.9s
✔ Container route_calculation_fast_api-frontend-1 Created 0.9s
✔ Container route_calculation_fast_api-server-1 Created 0.9s
✔ Container route_calculation_fast_api-db_init-1 Created 0.4s
✔ Container route_calculation_fast_api-backend-1 Created
Attaching to backend-1, db-1, db_init-1, frontend-1, server-1
frontend-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
frontend-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
frontend-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
frontend-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
frontend-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
frontend-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
frontend-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
frontend-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
frontend-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: using the "epoll" event method
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: nginx/1.25.4
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10)
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: OS: Linux 5.15.146.1-microsoft-standard-WSL2
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker processes
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 30
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 31
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 32
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 33
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 34
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 35
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 36
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 37
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 38
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 39
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 40
frontend-1 | 2024/04/11 20:59:41 [notice] 1#1: start worker process 41
Код: Выделить всё
...
backend-1 | File "/server/main.py", line 50, in
backend-1 | app.mount("/static", StaticFiles(directory="client/static"), name="static")
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/usr/local/lib/python3.11/site-packages/starlette/staticfiles.py", line 59, in __init__
backend-1 | raise RuntimeError(f"Directory '{directory}' does not exist")
backend-1 | RuntimeError: Directory 'client/static' does not exist
server-1 exited with code 1
Код: Выделить всё
version: '3.8'
services:
client:
build:
context: ./client
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- server
server:
build:
context: ./server
dockerfile: Dockerfile
ports:
- "8000:8000"
depends_on:
- database
command: sh -c "sleep 10 && uvicorn main:app --host 0.0.0.0 --port 8000"
database:
image: postgres
environment:
POSTGRES_DB: server_coords
POSTGRES_USER: postgres
POSTGRES_PASSWORD: somepass
ports:
- "5432:5432"
Подробнее здесь: https://stackoverflow.com/questions/783 ... -localhost