简体   繁体   中英

Docker container not able to call another running docker container from localhost?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM