如何链接Docker容器? [英] How to link docker containers?

查看:94
本文介绍了如何链接Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试链接docker容器,但似乎访问时出错。

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

我的结构如下:


  1. 数据库docker(Mysql)-容器名称为 um-mysql

  2. 返回- end docker(Tomcat)-图像名称为 cz-um-app

  3. 前端docker(Nginx)-图像名称为 cz-um-frontend

  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.

我有一个登录页面,在提交时,它访问一个URL http:// bac kendservices:8080 / MyApp

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

不确定为什么要链接后端具有数据库的容器可以正常工作,前端与后端的情况不同。 是否需要为此在Nginx中配置一些设置?

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?

hosts条目如下,我也可以ping backendservices:

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

推荐答案

首先,您不需要为后端服务映射8080:8080:任何 backendservices 图片中的 EXPOSE d 端口可被其他任何人看到与其链接的容器。

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

如果不是,请检查 docker ps -a 以查看后端是否仍在运行。

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

这篇关于如何链接Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆