Communication between docker-compose and other images

1k Views Asked by At

I have this architecture: - 1 docker component running nginx on the host's port 80 - app with 2 services: one node and one mongodb

the docker-compose file:

version: '2'

services:
  backend:
    build: ./back-end/
    container_name: "app-back-end"
    volumes:
      - ./back-end/:/usr/src/dance-app-back
      - /usr/src/app-back/node_modules
    ports:
      - "3000:3050"
    links:
      - mongodb

  mongodb:
    image: mongo:3.2.15
    ports:
      - "3100:27017"
    volumes:
      - ./data/mongodb:/data/db

The nginx config file

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location /back_1 {
        #proxy_pass http://172.17.0.2:5050/;
        proxy_pass http://0.0.0.0:5050/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

The nginx container seems not to be able to reach the port 3000 on the host. What I'm I doing wrong?

1

There are 1 best solutions below

5
On BEST ANSWER

If you have port 3000 on your host and would like to map it to your container port, than you need to do that in your docker-compose file

Change:

backend:
    build: ./back-end/
    container_name: "app-back-end"
    volumes:
      - ./back-end/:/usr/src/dance-app-back
      - /usr/src/app-back/node_modules
    ports:
      - "3000:3000"
    links:
      - mongodb

Then you can link nginx to your container that you want to proxy pass to.

Add to your compose file:

  nginx:
    restart: always
    image: nginx:1.13.1
    ports:
      - "80:80"
    depends_on:
      - backend
    links:
      - backend:backend

And then in your nginx file mention the name of your application container that you would like to proxy to with the port of that container that is open.

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location /back_1 {
        proxy_pass http://backend:3000/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Instead of the IP of the container inyour nginx, you can link the container name and the nginx config and docker will resolve those IPs for you.