2.2 KiB
2.2 KiB
e.g. 3: Create an Ningx Reverse Proxy With Docker
How To Run Multiple Docker Containers Under One URL
Backend only
How to test it:
- Comment all other containers
- Run
docker compose build
&docker compose up
- Test by
curl localhost:5000
, should return{"version":"1.0.0"}
Frontend only
How to test it:
- Comment all other containers
- Run
docker compose build
&docker compose up
- Test by
curl localhost
, should return a html file (as/eg3/frontend/index.html
)
Backend + Frontend
In this section, we test frontend container can cross-communicate with backend container.
Steps:
- Comment out proxy container
- Remove exposed of both containers, as we they don't need expose their ports to outside environment. We just want to test their cross-communication in the same docker network
- port 80 for frontend;
- port 5000 for backend;
- Run
docker compose build
&docker compose up
- Create a network:
docker network create mynetwork
- Add containers into network
docker network connect mynetwork backend
docker network connect mynetwork frontend
- Inspect
mynetwork
has containersdocker network inspect mynetwork
- Test network:
- Enter frontend container:
docker exec -it frontend /bin/sh
- Make a request to backend:
curl http://backend:5000
- Get response
{"version": "1.0.0"}
- Enter frontend container:
Proxy using nginx
In this section, we test we can reach both frontend and backend from proxy container or computer itself
Steps:
- Uncomment all container in
docker-compose.yaml
- Build container and initiate them via
docker compose up
- These container are not in
mynetwork
- Enter proxy container via
docker exec -it proxy /bin/sh
- From the proxy container, try to reach other containers
- Frontend
curl http://frontend
curl http://localhost
to visit port 80
- Backend
curl http://backend:5000
curl http://localhost/api
- Frontend
- Exit the container; In normal terminal, we can still reach frontend and backend
curl http://localhost
to visit frontendcurl http://localhost/api
to visit backend