Docker链接容器 – 环境variables不被显示

您好,我正在尝试链接我的容器遵循本指南:

https://docs.docker.com/userguide/dockerlinks/

我运行docker run -d --name mongodb devops-mongodb

然后我运行docker run -d -P --name rest-api --link mongodb devops-rest-api

将我的mongodb容器链接到rest-api容器。

然后文档告诉我这样做来查看环境variables:

 docker run --rm --name rest-api2 --link db:db devops-rest-api env 

但是,它不打印出文档中显示的环境variables。

我只是从我的Spring Boot应用程序中获得STDOUT。

我究竟做错了什么?

你期望哪个环境variables? 如果你没有设置任何非会显示。 这将有助于你在这里发布你的dockerfile。 我想你想要做的就是从你的restAPI访问mongo,对吧? 然后,只需在您的rest-apiconfiguration中将mongodb定义为mongo的主机名就可以了。 顺便说一句:在第二个调用中,你有一个错误,因为你没有指定mongodb,但是–link db:db。 – hrrgttnchml 6小时前

编辑1 :@ hrrgttnchml,我期待这些环境variables,直接从文档:

 DB_PORT DB_PORT_5432_TCP DB_PORT_5432_TCP_PROTO DB_PORT_5432_TCP_PORT DB_PORT_5432_TCP_ADDR 

我不认为你必须用-e,-env标志创build环境variables。 根据文档,Docker在链接两个容器时会自动创build它们:

 Docker creates several environment variables when you link containers. Docker automatically creates environment variables in the target container based on the --link parameters. It will also expose all environment variables originating from Docker from the source container. These include variables from: the ENV commands in the source container's Dockerfile the -e, --env and --env-file options on the docker run command when the source container is started 

不过,我可能是错的。

这里是我的rest-api dockerfile: http ://pastebin.com/3Ktbbr9n

MongoDB dockerfile: http ://pastebin.com/iqyKm5UK

两个应用程序都可以在容器运行时启动。 只是不知道如何链接他们。

是的,我想要rest-api与mongodb数据库进行通信。

由于我为rest-api运行spring-boot,我可以在我的application.properties文件中指定我的mongodb服务器的主机和端口,例如:

 spring.data.mongodb.host=192.168.99.100 spring.data.mongodb.port=27017 

那么你告诉我with mongodbreplace192.168.99.100

我也在Windows上运行oracle虚拟机,所以虚拟机本身有一个IP地址,当连接到容器时,我必须使用机器的IP而不是本地主机,所以这就是192.168.99.100所在的地方。 正确?

编辑2:我已经得到了rest-api与没有容器链接通过使用虚拟机的地址与Dockerfile中暴露的端口连接MongoDB。 所以对于mongodb它将是192.168.99.100:27017,而其余的api将是192.168.99.100:8080。

然而 ,这是不切实际的,因为ip地址被硬编码到rest-api类本身。 因此,别人将无法在他的机器上运行他的容器中的图像是因为IP地址不匹配。

现在很明显,解决方法是使用容器链接作为解决scheme,因为主机和端口在更改时会自动更新到/ etc / hosts文件中,这导致我们回到如何使容器链接工作的原始问题。

编辑3:指定mongodb作为rest-api中的主机名工作,但只有当数据库的别名传递给链接,如:– --link mongodb

也许devops-rest-api有一个ENTRYPOINTdocker run任何命令行参数都会传递给入口点。 不要指定一个命令,可以用--entrypoint env覆盖。