I have a setup like this in my production compose.yml file.
services:
proxy:
container_name: app-proxy
image: nginx:1.25.5
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- frontend
networks:
- app-net
frontend:
container_name: frontend
image: my-org/nextjs-frontend:prod
build:
context: ./frontend
dockerfile: .docker/prod/Dockerfile
command: ["node", "server.js"]
depends_on:
- backend
networks:
- app-net
backend:
container_name: app-backend
image: my-org/wagtail-backend:prod
build:
context: ./backend
dockerfile: Dockerfile
environment:
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DEBUG=False
- CSRF_TRUSTED_ORIGINS=${CSRF_TRUSTED_ORIGINS}
volumes:
- backend-static:/staticfiles
- backend-media:/mediafiles
command: >
sh -c "
set -xe;
python manage.py migrate --noinput;
gunicorn --bind 0.0.0.0:8000 backend.wsgi:application
"
depends_on:
- db
networks:
- app-net
db:
container_name: app-db
image: postgres:16.3
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-net
volumes:
backend-static:
backend-media:
db-data:
networks:
app-net:
driver: bridge
What I did was start the db and backend services with docker compose up backend -d
, ensuring they were up and running. I then proceeded to try building the image for the frontend (which is a Next.js app) with docker build frontend
, because the frontend needs to fetch data from the backend (up and running blissfully) for pre-rendering pages during its build process with yarn (specified in its Dockerfile).The error I got from this build was a network error stating that it can’t resolve the backend service. Here’s the error message:
32.83 at node:internal/deps/undici/undici:12618:11
32.83 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
32.83 cause: Error: getaddrinfo EAI_AGAIN backend
32.83 at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26)
32.83 at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:128:17) {
32.83 errno: -3001,
32.83 code: 'EAI_AGAIN',
32.83 syscall: 'getaddrinfo',
32.83 hostname: 'backend'
32.83 }
Can anyone tell me why I get this error? Is the backend service exposed to this build process?
The fetch for the backend service is correctly defined in my nextjs codebase because I use the same setup for dev (except without SSG using yarn build
).
2