0

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 error Error: 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?

middlelady
  • 573
  • 2
  • 13
  • 36
  • 1
    Is there any specific reason as to why you've used two separate `docker-compose.yml` files? Rather than creating a single one in the root directory? – dangarfield Nov 16 '22 at 09:21
  • I am planning to splitting the repo in 2. So I would like to isolate the frontend from the backend. Would you recommend something different in other to achieve that? – middlelady Nov 17 '22 at 08:17
  • 1
    A lot of opinions, but - https://stackoverflow.com/questions/49918636/git-repository-setup-for-a-docker-application-consisting-of-multiple-repositorie - I would generally suggest keeping things in this structure. Where my company has these types of apps (typically a microfrontend), we normally have a mono repo with 4 folders (frontend, backend, common (schema, validation), infra). As the two are inherently tightly coupled, you can treat them as one. HOWEVER, I am talking about SMALL DISCRETE services, not monolithic applications : ) – dangarfield Nov 17 '22 at 08:57
  • Yes, we have basically the same view at my company as well, but the ownership can change and every service / app should be decoupled and portable, without big issues. As I said I am not a big expert but it shouldn’t be a big deal for separate compose to communicate with each other. I will dig more – middlelady Nov 19 '22 at 09:08
  • did you allow cross origin access etc on backend? – abidinberkay Nov 22 '22 at 17:19

3 Answers3

1

Just to add a proper answer:

  • Generally you should NOT really want to be executing multiple docker-compose up -d commands
  • If you want to combine two separate docker-compose configs and run as one (slightly more preferable), you can use the extends keyword as described in the docs

However, 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:

dangarfield
  • 2,210
  • 1
  • 13
  • 18
  • Thanks for this, but I think your first 2 points are misleading, as they are not providing actual solutions but opinions, not fitting well this case. Also the examples provided are different (I also tried and didn't work). Docker is just made for isolation, and although I am not a great expert, I've read about many different setups with compose, and mine is not really uncommon. The only issue is probably my network which I am trying to figure. I will post an update when fixed. – middlelady Nov 20 '22 at 16:28
1

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:

networks:
  internal-network:
    external: 
      name: internal-network
Gamma
  • 23
  • 3
1

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:

networks:
  internal-network:
    name: internal-network
    external: true

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.

Andrew Gillis
  • 3,250
  • 2
  • 13
  • 15
  • Hey Andrew, thanks for this. I will try now and keep you posted, but it looks promising, I ignored the part to initiate on before the other. Let's see – middlelady Nov 23 '22 at 17:42
  • @middlelady No problem, I've been able to validate that it does properly expose the containers. I'm assuming "frontend" in your case is server-side or statically rendered nextjs? – Andrew Gillis Nov 23 '22 at 22:07
  • I am still not able to make it work indeed. So the frontend is SSR, and it's basically fetching from the backend the initial config (from API) to generate the build. That's where it fails. – middlelady Nov 25 '22 at 09:26