问题
I am using this lib:
https://github.com/jwilder/nginx-proxy
Here is my docker-compose file:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
whoami:
image: jwilder/whoami
environment:
- VIRTUAL_HOST=whoami.local
service1:
image: mynode:1.4
build: .
volumes:
- .:/app
restart: always
environment:
- VIRTUAL_HOST=service1.local
service2:
image: mynodeother:1.3
build: .
volumes:
- .:/app
restart: always
environment:
- VIRTUAL_HOST=service2.local
I added 2 new node services...
I can do like this: curl -H "Host: service2.local" localhost
and get response from service2....
Questions are what benefits I have from this? And how can I run service1 on 80 port?
here is Dockerfile
from service1:
FROM node:6.9.4
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm install nodemon -g
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
回答1:
It's easy, you just need to run all your services in 80 port and EXPOSE that port in your Dockerfile, that's it.
As long as you don't publish that port to your host (like you're doing it with nginx), there's no problem.
The advantage is that every service you have there can reach the other one by using the hostname, it means, the container's name, this is cool because you don't need to know the current ip address assigned to every container.
So if you go into one of those services with bash
or sh
you should be able to ping the other services by using the hostname:
Inside service1:
ping service2
The good thing about nginx-proxy
is that it's going to detect if you scale one of your services and it will update the nginx config automatically:
docker-compose scale service1=3
I will start 2 other instances of your service1 and no matter if you have 100, the rest of the services can reach them by using the hostname: service1.
So you can balance the load without worring about the ip address of every instance of the same service.
来源:https://stackoverflow.com/questions/43483753/how-to-change-nginx-proxy-settings