0

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" ]
Vladimir
  • 1,751
  • 6
  • 30
  • 52

1 Answers1

0

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.

calbertts
  • 1,513
  • 2
  • 15
  • 34
  • If I log into container like this: `docker run -it mynode:1.0 /bin/bash` and try to ping other container from some reason I get `ping: unknown host` do you know what is a problem? – Vladimir Apr 19 '17 at 00:26
  • Try with the service's name and the VIRTUAL_HOST. if they are in the same network it should work. Also make sure you have the latest docker version where the embedded dns server is implemented. – calbertts Apr 19 '17 at 00:33
  • It works when I call service with http req from antoher service but now when do it manually like above... Also how can I solve routing, for example if I have 2 services with diffirent routing is it possible to bind that routes on the same host automaticly? – Vladimir Apr 19 '17 at 07:19
  • I didn't understand your last question, I think that's another question, open a new one!.. this one seems to be answered. ;) – calbertts Apr 19 '17 at 10:34
  • Yes I explained detailed here: http://stackoverflow.com/questions/43492974/multiple-docker-services-to-listen-on-same-host-and-port – Vladimir Apr 19 '17 at 10:49