The reverse proxy server for the local environment can help to run several docker projects locally via the 80 port.
Each time when you face issues when you need to run several docker projects and you don't want to stop and run it again and again, or you just need your projects to work on 80 port. Use this Local Reverse Proxy!
Article: How to run multiple Docker projects under port 80 using an off-the-shelf local Reverse Proxy.
Client Host (OS mapping) Reverse Proxy Target Server (Docker App)
+------+ request +-----------------+ +--------------+ +----------------+
│ │ 'example.local' │ │ │ │ proxy │ │
│ │────────────────►│ 'example.local' │──────►│ │────────►│ │
│ │ response │ mapped to │ │ 127.0.0.1:80 │ │ localhost:8080 │
│ │◄────────────────│ '127.0.0.1' │◄──────│ │◄────────│ │
│ │ │ │ │ │ │ │
+------+ +-----------------+ +--------------+ +----------------+
(Browser) /etc/hosts
-
- For macOS/Linux just follow instruction.
- For windows - install Go into Windows OS, and use
CMD
to build and run Go app.
-
Clone and go into cloned repository.
-
Let's compile Go app
go mod tidy go build
-
Run Local Reverse Proxy:
./localreverseproxy # or if you have permissions errors use with sudo sudo ./localreverseproxy
-
If everything ok you will see in a console:
Reverse proxy is started.
-
Now visit
http://localhost
, you should see in the browserServer proxy started successfully!
and in the console you should see
Requested `localhost` host with method `GET` and URI `/` Requested `localhost` host with method `GET` and URI `/favicon.ico`
[!NOTE] Each http request will be logged and you will see it in the console, so you can check how things going.
-
Configure ports for your docker projects:
- Create
docker-compose.override.yml
in your projects. - Add there apache or nginx service and change a port. Because by default most of projects uses 80 or 443 ports.
Yaml should look like this:
services: nginx: ports: !override # helps to reset ports. - "8080:80" # Now set up new external port `8080`
[!NOTE]
!override
works only on the version v2.24.5 and above of docker-compose.For another project you can set 8090 port.
- Recreate docker containers inside your project directory to containers use new port.
docker-compose up -d --force-recreate
- Do the same steps for another docker project but use another port, as example
8090
.
[!NOTE] Remember one port for one service/container/device!
[!NOTE] Your 2 and more projects have to run and work without any issues, if not check that external ports be unique.
- Create
-
Go to the directory of cloned
localreverseproxy
repository again. -
Add your docker projects domains into
services.json
file. Where:Key
is local domain address.Value
is URL:Port of docker service. Usehttp://localhost:<your_service_port>
Note
You can use simple regexp in Key
fields of services.json
file.
To have a mask, like: *.local
.
Note
If you change the services.json
file you don't need to compile app again. It is read in runtime.
Note
Don't forget add domains into your hosts
file.
- Configure routing via
json
. - Implement reverse-proxy functionality.
- Add documentation how to use it.
- Show error if port is busy.
- Add requests logging in console.
- Add service response logging in console.
- Add supporting https
- Add supporting url paths