Docker link container – environment variables not being displayed
Hello I am trying to link my containers following this guide:
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:
DB_PORT DB_PORT_5432_TCP DB_PORT_5432_TCP_PROTO DB_PORT_5432_TCP_PORT DB_PORT_5432_TCP_ADDR
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: http://pastebin.com/3Ktbbr9n
MongoDB dockerfile: http://pastebin.com/iqyKm5UK
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 application.properties file the host and port of my mongodb server such as:
So are you telling me to replace
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 192.168.99.100 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 192.168.99.100:27017, whereas for the rest-api it would be 192.168.99.100:8080.
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:
One Solution collect form web for “Docker link container – environment variables not being displayed”
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