How to link docker containers?

2019-05-09 17:29发布

问题:

I have tried linking my docker containers but it seems to give error on access.

My structure is as following:

  1. Database docker(Mysql) - Container name is um-mysql
  2. Back-end docker(Tomcat) - Image name is cz-um-app
  3. Front-end docker(Nginx) - Image name is cz-um-frontend

Linking of Back-end with Database docker is done as following and it works perfectly:

$ docker run -p 8080:8080 --name backendservices --link um-mysql:um-mysql cz-um-app

The linking of Front-end with Back-end is done as following:

$ docker run -p 80:80 --name frontend --link backendservices:backendservices cz-um-frontend

But, linking of Front-end with Back-end is not working.

I have a login page, on submit, it accesses a url http://backendservices:8080/MyApp

In console, it shows error as:

net::ERR_NAME_NOT_RESOLVED

Not sure why linking of back-end container with database works fine and not the same case of front-end with back-end. Do I need to configure some settings in Nginx for this?

The hosts entry is as following and I am able to ping backendservices too:

回答1:

First you don't need to map 8080:8080 for backendservices: any EXPOSEd port in backendservices image is visible by any other container linked to it. No host port mapping needed.

Secondly, you can check in your front end if the backend has been register:

docker exec -it frontend bash
cat /etc/hosts

If it is not, check docker ps -a to see if backend is still runnong.



标签: docker