Docker container link not setting environment variables

In two terminal sessions I have the following docker containers running:

docker run -i --name docker1 centos:centos6 /bin/bash

  • How do I register a new NuGet package source with dotnet CLI on Ubuntu 14.04?
  • using docker for quick switching iojs version
  • How to move docker containers between different hosts
  • When to use auto mapped Docker data volume
  • How to detach from docker container running within another container
  • Use environment variables in docker
  • docker run -i --name docker2 centos:centos6 /bin/bash

    docker ps yields:

    8c67bb6975f7 centos:centos6 /bin/bash 7 minutes ago docker1

    8c67bb6975f7 centos:centos6 /bin/bash 7 minutes ago docker2

    (Any better way to just run an empty docker container for testing purposes btw?)

    When I run docker run -i --name link --link docker1:docker1 --link docker2:docker2 centos:centos6 /bin/bash and invoke env I get the following result:

    DOCKER1_NAME=/docker1
    HOSTNAME=b8403b72030d
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    PWD=/
    SHLVL=1
    HOME=/
    DOCKER2_NAME=/docker2
    

    So as you can see I only get the NAME of the containers, and not the port and IP which is what I really want. I feel like there’s something I’ve misunderstood, but I can’t tell what it is by looking at the docs.

  • running java applications in docker with jboss or tomcat server
  • Docker: unable to prepare context: unable to evaluate symlinks in Dockerfile path: GetFileAttributesEx
  • How to prevent docker images on docker hub being overwritten
  • Should we initialize Docker daemon every time when we restart the blockchain server?
  • Nginx in Docker container won't run PHP
  • Setting arguments in docker-compose file
  • One Solution collect form web for “Docker container link not setting environment variables”

    Luckily a good friend of mine (@tobiasgwaaler) has a better grasp of Docker than I do, and he graciously gave me the answer.

    In order for a docker container to share its port and ip it needs to EXPOSE the port number. Thus, by simply adding this to my Dockerfile:

    EXPOSE <port-number>

    And huzzah, the port and IP are now available in the env

    DOCKER1_PORT=tcp://172.17.0.116:<port-number>
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.