java 如何使用 docker-compose 链接两个容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35729083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to link two containers using docker-compose
提问by Omnipresent
At the moment I have two containers which I build from an image and then link the two:
目前我有两个容器,我从一个图像构建,然后将两者链接起来:
For example:
例如:
#mysql
docker build -t my/mysql docker-mysql
docker run -p 3306:3306 --name mysql -d my/mysql:latest
#webapp
docker build -t my/tomcat:7.0 tomcat/7.0
docker run -d --link mysql --name tomcat7 my/tomcat:7.0
Since I'm linking the webapp
container with mysql
container, the webapp
container gets a MYSQL_PORT_3306_TCP_ADDR
environment variable created. I use this environment variable to then connect to the mysql database in my jdbc
string.
由于我将webapp
容器与mysql
容器链接,因此webapp
容器会MYSQL_PORT_3306_TCP_ADDR
创建一个环境变量。我使用这个环境变量然后连接到我的jdbc
字符串中的 mysql 数据库。
All of this works fine but now I'd like to use docker-compose
so that everything can be built and ran from one command.
所有这些都工作正常,但现在我想使用它,docker-compose
以便可以从一个命令构建和运行所有内容。
However, while playing with docker-compose I'm noticing that it prefixes docker_
to the image name and furthermore deprecates the link
option.
但是,在使用 docker-compose 时,我注意到它docker_
作为图像名称的前缀,并且进一步弃用了该link
选项。
Question
问题
How would the above build/run commands translate to docker-compose yml file such that the webapp container can connect to the mysql container for jdbc.
上面的构建/运行命令如何转换为 docker-compose yml 文件,以便 webapp 容器可以连接到 jdbc 的 mysql 容器。
回答by Augusto
You need to add an alias
attribute in the compose yml. The following is taken from the docs the docsand is a very short example
您需要alias
在 compose yml 中添加一个属性。以下内容取自 docs the docs,是一个非常简短的示例
web:
links:
- db
- db:database
- redis
That compose snippet defines a web
container which has to be linked to the db and redis containers and it also adds the db
container with the alias `database.
该 compose 代码段定义了一个web
容器,该容器必须链接到 db 和 redis 容器,并且它还添加db
了别名为“database”的容器。
In you case, I think the yml for the tomcat container would look something like this
在你的情况下,我认为 tomcat 容器的 yml 看起来像这样
mysql:
image: my/mysql:latest
ports:
- "3306:3306"
tomcat7:
image: my/tomcat:7.0
links:
- mysql
Bear in mind that the tomcat container is not exposing any ports!
请记住,tomcat 容器不会暴露任何端口!
回答by OneCricketeer
Here is a rough translation to a docker-compose.
这是对 docker-compose 的粗略翻译。
Note that docker-compose tags it's own images, so say you made a mysql
image in a directory named test
, then the container name would turn into test_mysql_1
. The 1 is appended on the end because you can scale up multiple containers using docker-compose.
请注意,docker-compose 标记了它自己的图像,因此假设您mysql
在名为 的目录中制作了一个图像test
,那么容器名称将变为test_mysql_1
. 将 1 附加到末尾是因为您可以使用 docker-compose 扩展多个容器。
This file also assumes this file-structure
此文件也采用此文件结构
├── docker-mysql
│?? └── Dockerfile
├── tomcat
│?? └── 7.0
│ ?? └── Dockerfile
└── docker-compose.yml
version: '2'
services:
mysql:
build:
context: "./docker-mysql"
hostname: mysql
ports:
- "3306:3306"
webapp:
build:
context: "./tomcat/7.0"
hostname: tomcat7
links:
- mysql