0

I have two running docker containers. One docker container is calling the other docker container but when it is trying to call application is breaking. When I am giving my hostname of my machine inside the application.Application is working.

This is a really a dependency if i deploy these two containers i again have to find the hostname of that machine and then put inside my application is any other way so that which can remove this dependency.

This url is consumed by my docker container which is failing http://localhost:8080/userData

Same when i update with my host name then it is working. http://nl55443lldsfa:8080/userData

But this is really a dependency i cannot change inside my application everytime.Is any work around is there for the same.

user2604705
  • 99
  • 1
  • 9
  • 1
    Look into `docker-compose` as it sets up much of the basic networking for you automatically https://docs.docker.com/compose/networking/ – Jack Gore Aug 07 '18 at 19:39
  • Possible duplicate of [What does localhost means inside a Docker container?](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container) – David Maze Aug 07 '18 at 21:11
  • (Plug: my answer to that question answers both "what does localhost mean", since you mentioned it in the title, and also "how do I connect between containers", since that seems to be your actual issue.) – David Maze Aug 07 '18 at 21:12

2 Answers2

0

Put the two containers inside the same network when running them. Only then you can use hostnames for inter container communication.

Edit: And of course name you containers so you don’t get a random container name each time.

Edit 2: The commands are:

$ docker network create -d bridge my-bridge-network

$ docker run -d \
         --name webserver \
         --network=my-bridge-network \
         nginx:latest

$ docker run -d \
         --name dbserver \
         --network=my-bridge-network \
         mysql:5.7

Containers started both with a specified hostname and a common network can use hostnames internally to communicate with each other.

Günther Eberl
  • 674
  • 1
  • 10
  • 19
0

You should use docker-compose to run both containers and link them using the link property on your yaml file.

This might be a good example:

web:
    image: nginx:latest
    ports:
        - "8080:8080"
    links:
        - php
php:
    image: php

Then the ip of each container will be associated to its service name on the /etc/hosts file of both containers and you will be able to access them from inside the containers just by using that hostname.

Also be sure to be mapping the ports correctly, using http://localhost:8080 shouldn't fail if you map the ports correctly and the service is running.

Raül Ojeda
  • 69
  • 1
  • 6