I am recently decided to migrate my dev environment from native mac to docker for mac, and I would like to have multiple projects expose the same port 80, so that I can simply type http://app1.dev/ and http://app2.dev/ in the browser without remembering dozens of port numbers.
I don't have to do anything on native environment to achieve this. But since now nginx runs separately in each container they are conflicted on port exposing. I also know that I can use an external link to an external container but I don't want to tear apart my docker-compose.yml file, I just want everything in one piece.
docker-compose.yml in ~/demo1/
version: '3'
services:
web:
image: nginx:alpine
ports:
- "80:80"
# ...
docker-compose.yml in ~/demo2/
version: '3'
services:
web:
image: nginx:alpine
ports:
- "80:80"
# ...
When I issue command docker-compose up -d
in demo2 I got:
Creating network "demo2_default" with the default driver
Creating demo2_web_1 ... error
ERROR: for demo2_web_1 Cannot start service web: driver failed
programming external connectivity on endpoint demo2_web_1
(cbfebd1550e944ae468a1118eb07574029a6109409dd34799bfdaf72cdeb3d35):
Bind for 0.0.0.0:80 failed: port is already allocated
ERROR: for web Cannot start service web: driver failed programming
external connectivity on endpoint demo2_web_1
(cbfebd1550e944ae468a1118eb07574029a6109409dd34799bfdaf72cdeb3d35):
Bind for 0.0.0.0:80 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
Is there any way to make them share them same port or maybe remap ports from host to container without using extra commands to create external containers? Or is there a way to create external containers within the docker-compose.yml file?