I have a fairly complex application where I have next js on the client and on the backend I have graphql and I have nginx as a reverse proxy.
I am using next JS incremental static site regeneration functionality on the index page so that’s why I want my server up and running before my client container start building because when I run npm run build it is going to fetch some data from the graphql server here is my docker compose file
version: "3"
services:
mynginx:
container_name: mynginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 80:80
graphql:
container_name: graphql_server
depends_on:
- mynginx
build:
context: ./server
dockerfile: Dockerfile
mynextjs:
container_name: nextjs_server
depends_on:
- graphql
build:
context: ./client
dockerfile: Dockerfile
3
Answers
depends_on
withhealthcheck
, to start container when another already workshttps://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck
something like this
In simple terms, what you need is to wait until
graphql
service is properly finished starting before you runmynextjs
service.I’m afraid that
depends_on
(orlinks
) might not work out of the box in this case. The reason is, although Compose does startgraphql
service before it startsmynextjs
service, it doesn’t wait untilgraphql
service is in READY state to start the dependant services.Basically, what you need is to find a way to tell Compose to wait until
graphql
service is in READY state.The solution is described in Control startup and shutdown order in Compose documentation
I hope this helps you. Cheers 🍻 !!!
If I understand your problem correctly, you need to delay beginning the build of the image for the frontend container, until the backend containers are running and ready.
The easiest way that comes to mind would be to use profiles to allow starting these independently.
Eg:
Then chain the starting something like:
Another option might be splitting into two separate compose files, with a shared docker network between them.
ref: https://docs.docker.com/compose/profiles/