代码之家  ›  专栏  ›  技术社区  ›  Kingamere

Docker链接容器-未显示环境变量

  •  0
  • Kingamere  · 技术社区  · 10 年前

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

    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容器。

    然后文档告诉我这样做以查看环境变量:

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

    然而,它并不像文档中所示那样打印出环境变量。

    我刚从我的Spring Boot应用程序中获取STDOUT。

    我做错了什么?

    您期望哪些环境变量?如果未设置,将显示任何非。如果你把你的文件放在这里会有帮助的。我想你想做的是从你的rest api访问mongo,对吧?然后,只需在rest api配置中将mongodb定义为mongo的主机名,它就可以工作了。BTW:在第二次调用中,您出现了一个错误,因为您没有指定mongodb,而是指定--link db:db。hrrgttnchml 6小时前

    编辑1 :@hrrgttnchml,我希望这些环境变量直接来自文档:

    DB_PORT
    DB_PORT_5432_TCP
    DB_PORT_5432_TCP_PROTO
    DB_PORT_5432_TCP_PORT
    DB_PORT_5432_TCP_ADDR
    

    我认为您不必使用-e、-env标志创建环境变量。 根据文档,当您链接两个容器时,Docker会自动创建它们:

    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,所以我可以在应用程序中指定。属性文件我的mongodb服务器的主机和端口,例如:

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

    所以你是在告诉我更换 192.168.99.100 with mongodb ?

    我还在windows上运行oracle box虚拟机,所以VM本身有一个ip地址,当连接到容器时,我必须使用机器的ip而不是本地主机,所以192.168.99.100就是从这里来的。对的

    编辑2: 我已经获得了与mongodb通信的rest api 没有 容器链接,只需使用VM的地址以及Dockerfile中暴露的端口。因此,对于mongodb,它将是192.168.99.100:27017,而对于其余api,它将为192.168.99.100:8080。

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

    现在很明显,解决方法是使用容器链接作为解决方案,因为主机和端口在发生更改时会在/etc/hosts文件中自动更新,这让我们回到了最初的问题,即如何使容器链接工作。

    编辑3: 指定 mongodb 因为rest api中的主机名确实有效,但仅当为链接传递数据库别名时,如: --link mongodb

    1 回复  |  直到 10 年前
        1
  •  1
  •   Andy Shinn    10 年前

    devops-rest-api 有一个 ENTRYPOINT 可能您的 docker run 将被传递到入口点。不要指定命令,而是尝试使用 --entrypoint env .