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

  • Can't connect with BOLT to a docker Neo4j
  • Ansible docker_container gives “Error getting network id”
  • Docker exec rm not working
  • Dockerizing an existing Project
  • How To Create And Start Docker Container with specific port, detached mode using docker-java
  • Is it possible to create docker image from .img file containing OS
  • 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.

  • How to fetch Ips of a service in docker swarm cluster ?
  • Docker Containers not running on boot2docker boot
  • Run Jmeter from Jenkins with Docker
  • Redis in Docker container with custom redis.conf file not attaching
  • Docker push with artifactory
  • Docker mysql container cannot use a data volume defined on startup
  • 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.