How to get Docker containers to talk to each other while running on my local host?

前端 未结 3 805
醉梦人生
醉梦人生 2020-12-08 02:53

I have a Webapp running completely locally on my MacBook.

The Webapp has a Front End (Angular/Javascript) and a Back End (Python/Django) which implements a RESTful A

3条回答
  •  无人及你
    2020-12-08 03:08

    I think the most elegant solution would be to create a software defined network, but for this simple example it may be a bit overkill. Nevertheless when you think about running things in production, maybe even on different servers, this is the way to go.

    Until then, you may opt to link the containers. E.g., when you used to start your frontend container like this:

    $ docker run -p 8080:8080 --name frontend my-frontend
    

    You now could do it like this:

    $ docker run -p 8080:8080 --name frontend --link backend:backend my-frontend
    

    The trick here is to also start the backend container and giving it a name using the --name flag. Then, you can refer to this name in the --link flag and access the backend from within the frontend container using its name (--link takes care of automatically adding the linked container to the /etc/hosts file).

    This way you do not have to rely on a specific IP address, be it the host's one or whatever.

    Hope this helps :-)

提交回复
热议问题