SomethingSomething
SomethingSomething

Reputation: 12196

docker container internal server applications

I've learned that for exposing a server that runs in a docker container to the outer environment, when using docker run, by adding some parameters, it is possible to forward ports to the host machine.

My question is different though, I have some applications that should all run inside the docker container and should communicate between themselves using the network, while they are all running on the container.

Is it possible on a docker container to run these internal servers and communicate between them using localhost:PORT, referring to the "container's IP address" as localhost (or 127.0.0.1) ?

Upvotes: 1

Views: 351

Answers (2)

shizhz
shizhz

Reputation: 12501

If you plan to run all your applications inside one docker container, don't do it. One of the purpose to use Docker is to isolate applications from each other, putting all of them into one container will bring more troubles than benefits, one example is it'll be more difficult if you just want to upgrade one of the applications.

If you really need/have to put them into one container, and then use localhost:port is OK for them to communicate with each other, just like they're running on one machine.

If you decide to run them in separated containers and even further they maybe run on multiple machines, this problem goes to multi-container communication and it's a big topic. The options you have include:

  • Docker multi-host networking, basically you'll need some docker networking driver to connect dockers across multi-hosts together, and then containers can connect to each other by container name or hostname e.g.

    There're many resources talking about this, you can refer to the following links for more info:

  • Jump over docker multi-host networking, map your service port to a random host port, and then use a kind of service discovery service to facilitate the communication. Actually in our company, we're using Mesos to manage cluster, marathon to deploy our services, and then Bamboo as service discovery and LB, they're working together very well.

This is some sharing of my ideas, hope it could be helpful to you.

Upvotes: 1

fzgregor
fzgregor

Reputation: 1897

localhost inside of a container will resolve to the network stack of this container. So, if all your servers run in the same container, they use the same network stack and will be able to communication with each other via localhost.

docker run -it --rm alpine:3.5 sh
/ # apk add --no-cache py-pip
/ # pip install Flask
/ # cat > flask_server.py << EOS
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
EOS
/ # python flask_server.py &
/ #  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
/ # telnet localhost 5000
GET /

127.0.0.1 - - [04/Apr/2017 12:26:04] "GET /" 200 -
Hello World!Connection closed by foreign host

That said, if two containers don't share the network stack - which is typically the case - they will not be able to communicate with one another via localhost.

Upvotes: 2

Related Questions