
docker를 사용하여 프런트엔드, 백엔드 및 DB를 설정하려고 합니다. 내 프런트엔드 컨테이너만 공개가 아닌 백엔드에 요청해야 하는 조건은 다음과 같습니다. conf 파일을 작성했지만 여전히 postman을 사용하여 백엔드에 액세스할 수 있습니다. 또한 프런트엔드 빌드 파일을 볼륨으로 이동하고 /usr/share/nginx/html
프런트엔드 컨테이너가 중지되고 파일이 nginx 컨테이너에서 직접 제공되는 위치에 마운트합니다.
기본 nginx 컨테이너가 프런트엔드 nginx 서비스로 경로를 지정하는 프런트엔드 컨테이너에서 다른 nginx 서비스를 실행하는 것이 좋습니까?
default.conf
upstream backend_servers {
server backend:8000;
}
server {
listen 80;
location / {
root /usr/share/nginx/html/frontend;
try_files $uri $uri/ /index.html;
}
# Reverse proxy requests to the uWSGI server
location /api {
rewrite ^/api(.*) $1 break;
uwsgi_pass backend_servers;
include /etc/nginx/uwsgi_params;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
내 Docker 작성 파일은 다음과 같습니다.
version: '3'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.prod
volumes:
- app-data:/app/dist
backend:
build:
context: .
dockerfile: Dockerfile.prod
environment:
- DEBUG=0
- ALLOWED_HOSTS=nginx,localhost,127.0.0.1
- SECRET_KEY=my-secure-key
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
db:
image: postgres:latest
restart: always
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- db-data:/var/lib/postgresql/data
nginx:
build:
context: ./nginx
dockerfile: Dockerfile.prod
ports:
- "80:80"
volumes:
- app-data:/usr/share/nginx/html/frontend
depends_on:
- backend
volumes:
db-data:
app-data:
답변1
프런트엔드, 백엔드 및 nginx를 자체 브리지 네트워크에 배치합니다.
networks:
backend:
driver: bridge
service:
frontend:
...
networks:
- backend