[docker] 오늘의 삽질 : nginx : host not found in upstream ~ 에러
docker compose 이용하여 여러개의 도커 컨테이너를 조합한 서버를 만들고 있었다. 이후 docker compose up 명령을 통해 도커 컨테이너들을 실행하는 도중, nginx 와 관련된 콘솔 창에서 다음과 같은 에러의 발생을 알렸다.
대략 upstream 으로 지정한 client:3000 을 발견할 수 없다는 메시지로 보인다. 이 에러는 무엇일까?
해당 문제가 발생했을 때 사용된 코드는 다음과 같다.
nginx : default.conf
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
location / {
proxy_pass http://client;
}
location /_next {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
docker-compose.yml
version: '3.9'
services:
postgres: #service name
image: 'postgres:latest'
environment:
- POSTGRES_PASSWORD=postgres_password
redis:
image: 'redis:latest'
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8080:80'
depends_on:
- api
- client
api:
depends_on:
- postgres
- redis
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /home/node/app/node_modules
- ./server:/home/node/app
env_file:
- ./postgres.env
- ./redis.env
cilent:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /app/node_modules
- ./client:/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
env_file:
- ./redis.env
volumes:
- /home/node/app/node_modules
- ./worker:/home/node/app
depends_on:
- redis
docker-compose.yml 파일의 services에는 우리가 사용하고 싶은 서비스들을 지정할 수 있다. 이때 해당 서비스들의 이름은 사용자 마음대로 정할 수 있으며, 해당 서비스 및 서비스에 대한 호스트의 이름이 기본적으로 해당 이름으로 설정된다는 특징이 있다. 따라서 해당 이름을 바탕으로 서로 다른 서비스에서 접근할 수 있다.
그렇다면, 이번 에러는 왜 발생했을까? 결론적으로 말하자면, 동일 상황에서는 대부분 스펠링 실수로 인해 발생한다. 위에 작성된 docker-compose.yml 의 서비스 리스트 중 다음 단어를 보자.
아이고, 서비스 명을 client가 아니라, cilent라고 작성했었다. 이렇게 작성된 코드는 다음 코드와 호환되지 않는다.
nginx 에서는 client:3000 에 대한 upstream 을 지정했는데, 정작 client 서비스 혹은 호스트가 존재하지 않으므로 client:3000 이라는 주소에 접근할 방법이 없는 것이다. 따라서 나의 경우 cilent를 client로 고친 후 해결되었다.
대부분의 설정 파일은 프로그램 코드와는 달리 오타와 같은 에러 상황을 알려주지 않는다. 이번 에러 역시 유저 입장에서는 에러이지만, 설정 파일 입장에서는 이것이 유저의 의도 혹은 실수인지 여부를 알 수 없기 때문에 발생한다. 에러 발생을 방지하면서 설정 및 프로그래밍을 하는 것이 최선이겠지만, 이미 에러가 발생했다면 해당 에러 메시지를 기반으로 오류가 발생한 원인을 되짚어보는 자세도 필요한 것 같다.