docker.io - Docker连接应用程序和数据库容器 [英] docker.io - Docker linking between application & database containers

查看:116
本文介绍了docker.io - Docker连接应用程序和数据库容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的Scala-Akka应用程序与我的MySQL数据库在两个独立的Docker容器上。我发现Docker允许开发人员使用名为-link的标记将他们​​的应用程序链接到他们的数据库。在我用来创建我的图像的Dockerfiles中,我添加了EXPOSE 3306 8080。

I am trying to use my Scala-Akka application with my MySQL database on two separate Docker containers. I found out that Docker allows developers to link their application to their databases with the flag named --link. In my Dockerfiles in which I've used to create my images, I have add in EXPOSE 3306 8080 to it.

这是我如何运行容器: p>

And this is how I run the containers:

docker run -d -p 3306:3306 --name mysql centos6mysql
docker run -d -p 8080:8080 --name scalaapp --link mysql:db centos6scala

运行容器后,我使用了docker ps和I能够看到活动的容器。但是,似乎应用程序容器没有使用MySQL容器中的数据库。任何人都知道发生了什么问题?

After running the containers, I used docker ps and I am able to see the active containers. However, It seems like the application container is not using the database from the MySQL container. Anyone know what's wrong?

推荐答案

在Docker中链接允许在容器之间建立网络连接。 Docker将为URL,IP,端口和协议的链接容器定义环境变量。这些名称将基于您的容器的名称。例如:

Linking in Docker allows network connections to be made between containers. Docker will define environmental variables to your linked containers for the URL, IP, port, and protocol. The names of these will be based on the name of your container. For instance:

DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
DB_PORT_5432_TCP_PROTO=tcp
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_ADDR=172.17.0.5

您可以使用这些环境变量来设置您的Akka应用程序容器来连接到您的DB容器。但是,您必须手动配置应用程序容器。 Docker将不会自动为您建立连接。

You can use these environmental variables to set up your Akka app container to connect to your DB container. However, you must manually configure the app container to do so. Docker will not make the connection for you automatically.

所以,在应用程序的某个地方,您需要将这些值传递给您的启动脚本,这可能类似于:

So, somewhere in your app, you will need to pass these values to your startup script, something that might look like:

./restcore --Ddb.default.db="jdbc:mysql//${DB_PORT_3306_TCP_ADDR}:${DB_PORT_3306_TCP_PORT"

这篇关于docker.io - Docker连接应用程序和数据库容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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