Reputation: 848
Hello I am currently planning out some server architecture with Docker. (I'm fairly new to Docker)
My goals are:
How each container works:
Right now I'm using docker compose to run both frontend and backend.
docker-compose.yml
version: '3'
services:
frontend:
build: ./frontend
ports:
- 8080:80
networks:
- app-network
backend:
build: ./backend
ports:
- 3000:3000
working_dir: /usr/src/server
command: node server.bundle.js
networks:
- app-network
networks:
app-network:
driver: bridge
This works fine. I can visit localhost:8080 for webpage and localhost:3000 for backend.
Note: Front end container uses NGINX docker image, and backend uses NODE:10 docker image.
What I want to accomplish
On my server I will have another instance of NGINX. I would like to somehow containerize each docker-compose setup under one port like localhost:5000.
This way when I get a request from www.example-website1.com it redirects to localhost:5000, which then loads localhost:8080 from that docker-compose setup.
Again when I get a request from www.example-website2.com it redirects to localhost:5001, which then loads localhost:8080 from another docker-compose setup.
Is this possible? Or is there another way I should be approaching this problem?
Upvotes: 2
Views: 4001
Reputation: 158647
There's nothing special about port 8080 in your example and you can just directly pick the host port you want to use for your separate Compose-based applications. You have to pick different published ports for each Compose setup or the containers won't be able to start at all.
If your setup is that the host nginx contacts the "frontend" container, and then that forwards some requests on to the "backend" container, then you just need to pick the port number you want for the frontend, and you don't need to expose the backend.
version: '3'
services:
frontend:
build: ./frontend
ports:
# Pick a different port "5000" for each setup
- 5000:80
backend:
build: ./backend
# Use default command:, working_dir: from image
# Could publish ports: if necessary
depends_on:
- frontend
# Use "default" Docker network
If you do need to directly contact the backend from the outside, you could expose it via the host nginx proxy in the same way and publish ports:
from it, again picking distinct ports for each container stack.
Between the two containers, if backend
needs to call frontend
, it can always use the Docker Compose service name and the port inside the container: http://frontend:80/
. You don't need expose:
or ports:
for this to work.
Upvotes: 2
Reputation: 88
You can't use the same port in two different containers, you always have to expose a different port.
To configure ngnix you must install it on your host, then go to the folder:
/etc/ngnix/sites-enabled
Then create a .conf file with the specifications that ngnix will use. Example:
myapi.conf
server {
server_name mywebsite.com;
access_log /etc/nginx/mywebsite.log;
location / {
proxy_pass http://127.0.0.1:9003;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
In this example, all traffic that arrives through the site mywebsite.com will be directed to the container that has port 9003 exposed. I hope I helped you
Upvotes: 2