Docker link container – environment variables not being displayed

Hello I am trying to link my containers following this guide:

  • Nginx in Docker container gets `connection reset` error, but works fine without a container
  • Docker swarm container info about parent service
  • Running tests in a container on Travis
  • “Error response from daemon: Cannot start container … no such file or directory” on Oracle Linux running hello-world
  • Docker: When trying to run docker run hello-world getting - Forbidden
  • Building an Android project from the command line with Docker
  • I run docker run -d --name mongodb devops-mongodb

    Then I run docker run -d -P --name rest-api --link mongodb devops-rest-api

    To link my mongodb container to the rest-api container.

    Then the documentation told me to do this to view the environment variables:

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

    However, it does not print out the environment variables like shown in the docs.

    I just get the STDOUT from my Spring Boot application.

    What am I doing wrong?

    Which environment variables are you expecting? If you don’t set any non will be shown. It would help if you post your dockerfile here. I think what you want to do is to access mongo from your rest api, right? Then just define mongodb as the hostname of mongo in your rest-api configuration and it should work. BTW: in the second call you have an error cause you are not specifying mongodb but –link db:db. – hrrgttnchml 6 hours ago

    EDIT 1: @hrrgttnchml, I am expecting these environment variables, straight from the doc:


    I don’t think you have to create environment variables with the -e, -env flags.
    According to the docs, Docker creates them automatically when you link two containers:

    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

    I may be wrong, however.

    Here is my rest-api dockerfile:

    MongoDB dockerfile:

    Both applications can be started when the containers are ran. Just don’t know how to link them.

    Yes, I want the rest-api to communicate with the mongodb database.

    Since I’m running spring-boot for the rest-api, I can specify in my file the host and port of my mongodb server such as:

    So are you telling me to replace with mongodb?

    I’m also running the oracle box virtual machine on windows so the VM itself has an ip address, and when connecting to the container I have to use the machine’s IP and NOT localhost, so that is where is from. Correct?

    EDIT 2: I have gotten the rest-api to communicate with the mongodb without container linking by just using the address of the VM along with the exposed port in the Dockerfile. So for mongodb it would be, whereas for the rest-api it would be

    However, this is impractical because the ip address is hardcoded into the rest-api class itself. So someone else will not be able to run the images in his container on his machine be cause the ip addresses won’t match.

    Now obviously the work around is to use container linking as the solution since the host and port are automatically updated in the /etc/hosts file when they change, leading us back to the original question of how to get container linking to work.

    EDIT 3: Specifying mongodb as the hostname in the rest-api does work but only when the alias for the database is passed in for the link such as: --link mongodb

  • Docker volume located in /tmp on OSX empty
  • Can't connect to MySQL container on AWS ECS
  • How can I pass the argument to docker-compose.yml using shell file
  • Exclude Auto Import of Docker Plugin in Gradle
  • Alpine Linux Docker Container with jre-8 does not have Nashorn Engine
  • How to connect to SSHD inside a Docker container from Windows?
  • One Solution collect form web for “Docker link container – environment variables not being displayed”

    Does devops-rest-api have a ENTRYPOINT perhaps? Any command line parameters on your docker run will get passed to the entrypoint. Instead of specifying a command, try overriding with --entrypoint env.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.