Docker Community Forums

Share and learn in the Docker community.

Linking angular 2 application container with Nodejs container


(Adityavinayachar) #1

I am building a project in MEAN stack. I have 4 different images: single angular-2 , 2 nodejs images (Say node-1 and node-2) and a single mongoDB image. I have linked the containers of the mentioned images with docker-compose.yml file and run the command docker-compose up. Now what happens is a network of those containers gets created. As per observation container of node-1 image can communicate with container of node-2 image and container of mongoDB image as well. But the container of angular-2 image is NOT ABLE to communicate with node container WITHIN THE NETWORK. But the container of the angular-2 image successfully communicates with the node containers through the URL exposed by the node container to host i.e. if API URL’s mentioned in angular code is mentioned as http:localhost:3000/myapi then it works. I am sharing a sample docker-compose.yaml file for better understanding.

#docker-compose.yaml
version: "2"
services:
node-1:
image: node-1-image
ports:
- "4000:4000"
links:
- mongoDB
networks:
- webnet
node-2:
image: node-2-image
ports:
- "3000:3000"
links:
- mongoDB
- node-1
networks:
- webnet
client:
image: angular-2
ports:
- 80:80
networks:
- webnet
links:
- node-1
- node-2
mongoDB:
image: mongo
ports:
- "27017:27017"
volumes:
- "/data/db:/data/db"
networks:
- webnet
networks:
webnet:


(Sreenivas Makam) #2

General advise is td avoid links as that will get deprecated very soon. If you use Docker 1.12+ swarm mode, service discovery and dns is built in as long as containers are in same network. Pls try that option.


(Karthikeyan2018) #3

I am also facing a similar issue.
I have the following docker(Server Version: 17.12.1-ce , API version: 1.35 ) swarm setup:-
Virtual Machine 1 - Angular container 1
Virtual Machine 2 - Angular container 2
Virtual Machine 3 - node.js container 1
Virtual Machine 4 - node.js container 2
All the containers are connected to user defined overlay network.
I am able to ping node.js container 1 from inside of angular container 1
and vice versa.

Problem:-
My angular application has to make api calls to node and the url containing ip address details are mentioned in environment.ts file(custom angular configuration file). i see that ip address /host name/ node service name is not getting recognized due to which communication between angular & node containers is not happening.
i see request timeout error showing up in logs. Can some one help me in knowing what could be going wrong ?