I’ve created two Laravel applications, each in their own docker container. They have one common network (subsequently, one application must access another over the network through an alias).
The problem is: in the browser, when the page is refreshed, with the same IP address, one or second application opens.
Docker version 24.0.6, build ed223bc
Can anyone explain this strange behavior?
Why does the output cycle to the second application when I refresh the page?
See below Docker settings for both apps:
And we see strange behaivor in browser when page is update. After each update, the output switches to the second application. The IP address does not change.
In the Docker Desctop nginx logs of the first app, I don’t see any requests when I update secod browser. And vice versa.
For example one of docker-compose.yml (second is same, diff only at container names and IP address)
version: '3.8'
services:
nginx:
container_name: app1_nginx
image: nginx:latest
volumes:
- ./:/var/www
- ./_docker/nginx/conf.d:/etc/nginx/conf.d
ports:
- "127.0.0.101:80:80"
depends_on:
- app
networks:
laravel:
app:
container_name: app1_app
build:
context: .
dockerfile: _docker/app/Dockerfile
volumes:
- ./:/var/www
networks:
laravel:
networks:
laravel:
name: "laravel-shared"
2
Answers
I found a solution. The problem was that the network settings were not correct. I can't say exactly how docker works and why the site changed cyclically, but it seems the reason was that the same network was used.
I added another shared network and now everything works correctly. In addition, I added aliases to the config and now each application can access another using that alias. Also aliases added in hosts file for access in browser:
New config example (second is same, diff only at container names and IP address):
Based on the description and the provided docker-compose.yml for app1, it seems that you are trying to bind two different applications to two different IP addresses but on the same host port (80). The line "127.0.0.101:80:80" indicates this. If the second application also has a similar configuration but with a different IP, that might be the cause of the problem.
My suggestion:
Instead of using different IP addresses, consider binding each application to a different port on 127.0.0.1. For instance, bind app1 to 127.0.0.1:8080 and app2 to 127.0.0.1:8081. This is a more standard way to differentiate applications on the same host.