I have 2 folders separated, one for backend and one for frontend services:
- backend/docker-compose.yml
- frontend/docker-compose.yml
The backend has a headless wordpress
installation on nginx
, with the scope to serve the frontend as an api service. The frontend runs on next.js
. Here are the 2 different docker-compose.yml
:
backend/docker-compose.yml
version: '3.9'
services:
nginx:
image: nginx:latest
container_name: my-app-nginx
ports:
- '80:80'
- '443:443'
- '8080:8080'
...
networks:
- internal-network
mysql:
...
networks:
- internal-network
wordpress:
...
networks:
- internal-network
networks:
internal-network:
external: true
frontend/docker-compose.yml
version: '3.9'
services:
nextjs:
build:
...
container_name: my-app-nextjs
restart: always
ports:
- 3000:3000
networks:
- internal-network
networks:
internal-network:
driver: bridge
name: internal-network
In the frontend I use the fetch
api in nextjs
as following:
fetch('http://my-app-nginx/wp-json/v1/enpoint', ...)
I tried also with ports 80
and 8080
, without success.
The sequence of commands I run are:
docker network create internal-network
- in
backend/
folder,docker-compose up -d
(all backend containers run fine, I can fetch data with Postman from WordPress api) - in
frontend/ folder
,docker-compose up -d
fails with the errorError: getaddrinfo EAI_AGAIN my-app-nginx
I am not a very expert user of docker
so I might miss something here, but I understand that there might be internal network issues over the containers. I read many answers regarding this topic but I couldn’t figure it out.
Any recommendations?
3
Answers
Just to add a proper answer:
docker-compose up -d
commandsextends
keyword as described in the docsHowever, I would suggest that you treat it as a single docker-compose project which can itself have multiple nested git repositories:
Real working example to backup using your applications that validates this approach:
I have found this thread here
Communication between multiple docker-compose projects
By looking at the most upvoted answers, I wonder if it is related to network prefix?
It seems like the internal-network would be prefixed with frontend_? On the other hand you can also try to locate the network by name in backend/docker-compose.yml:
The issue is external networks need the network name specified (because docker compose prefixes resources by default). Your backend docker compose network section should look like this:
You are creating the network in your frontend docker compose so you should omit the
docker network create ...
command (just need to init frontend first). Or instead treat them both as external and keep the command. In which use the named external network as shown above in your frontend docker compose as well.