As per https://docs.docker.com/v1.9/compose/env/
Using this file
version: '2'
services:
legacyBackend:
image: someimage:latest
volumes:
- /var/www/backend
ports:
- "8100:80"
- "8101:81"
- "3315:3306"
tty: true
app:
image: anotherimage:latest
links:
- legacyBackend
volumes:
- /var/www/app
ports:
- "8104:4200"
tty: true
I would expect $APP_PORT
to be a available env variable inside the legacyBackend service, and I would expect $LEGACYBACKEND_PORT
to be available inside the app service.
Which as you can see in the image above, is not the case.
You can however ping the app
service:
But you cant echo it to get a IP:
Listing the env variables:
Using inspect, the "Links" value is null, alltough they are clearly defined in docker-compose.yml and they work with the ping command:
I'm not sure how I can get a linked containers network address when the documented env variables are not available. A common use case is for REST based mobile apps, where you need to sed
config files with correct ip's in a local environment setup.
You're reading an old version of the docs: https://docs.docker.com/compose/link-env-deprecated/
Environment variables will only be populated if you鈥檙e using the legacy version 1 Compose file format.
Also https://docs.docker.com/compose/networking/. You don't need ips, use the hostname that the alises provide you.
Hm ok, the current docs link to that page, my bad there for not noticing.
I have a use case where hostnames are not usable, for example when (literally) building a mobile app inside the container, the hostname aliases are parsed clientside while the app is built using node, so "app" will literally be the string "app", in version 1 you could pass the ENV variable to the build which evaluates to a IP server to the client, so the string "app" would be an actual IP.
If I try to use the hostname in place of the ENV variable, the result is literally the alias name and not the IP it represents, since it's just parsed as a string in the build.
I suppose my question then is how do you find out the IP to linked containers in a bash shell using version 2?
Is this completely removed or is there a way to get it? It feels like I'm missing something obvious :)
I've been reading the networking docs, cli docs, compose file docs for the past 7 hours with no success as to a solution that one can leverage to pass linked containers ip's to any type of build script, using the hostnames just results in the actual strings being set as ip's.
using the hostnames just results in the actual strings being set as ips
I'm not understanding why this is a problem. Just about every library should support hostnames in place of IP addresses. What are you doing with these values that they aren't being resolved by DNS?
I'm sorry, that was poorly worded, here is an example, assuming the hostname is db
:
version 1, db
is a ENV var that is available in bash scripts, so it can be used as ${db}
for any server-side build script, so having a sed
command that sets things such as a backend api location
, since the ip is available as a ENV var, there is no problem using any build script.
version 2, db
is a hostname that is not available in /etc/hosts, nor is it an ENV var, and it breaks all bash based helper scripts.
If I were to pass db
to the build script for the mobile app, which runs in javascript, it would be evaluated literally as the STRING db
, so the backend api location would be LITERALLY the string db
instead of 192.0.0.1. There are no connections made and there is no parsing of these files on the server to make any connections, since the code is only built on the server but it's run on the client side, i.e the browser, like all modern javascripts projects.
The build script runs on nodejs (https://nodejs.org/en/) and parses javascript files where you need to use a solution such as sed
in .sh files to configure javascript files for the build process, or using nodejs process.env to get ENV vars.
In this case you can't use docker-compose to setup say a backend REST api and any javascript based project that requires a build, which is pretty much every single one, that wants to use said api in things like Ajax requests or to serve any resources.
In our use case, we have a Ember(javascript) app that uses a Symfony(PHP) based REST Api, the Ember app has a backend_location config parameter in it's config file and is built on the server, so that the client doesn't have to wait for a minute to load it in his browser.
If i set this backend_location to db
, which is the hostname, and then try to visit the app , it literally tries to connect to http://db.
So it all boils down to: is there any way to get a linked containers ip inside a container? I mean if I run ping db
it somehow knows what the ip is, even though there is no entry in /etc/hosts or /etc/resolv.conf, I don't know where to look for where this is set and it is not stated anywhere in the documentation nor is there any example of how to get linked container ip's, do I really need to make a ping call in a bash file and regexp the output or install a package to get ip's from hostnames?
Solved this using this technique, though it seems to be silly:
getent hosts serviceName | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
I'm guessing you wrote this script in separate file to solve this.
If so, how did you call this script from the docker-compose file?
Any updates on this? Echoing @MichaelHindley, In some aspects the ip get's 'expanded'/inferred while others it remains as a literal string.
@lborg019 one way is to just call it right in the docker-compose, e.g.:
command: >
bash -c "master=$$(getent hosts spark_master_w_worker | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
&& ./spark-2.1.0-bin-hadoop2.7/bin/spark-class org.apache.spark.deploy.worker.Worker spark://$$master:7077"
Take note of the escaped use of $ for vars.
EDIT
To the thumbs down, hopefully you downvoted because this approach sucks. Just found out this probably doesn't work outright on Mac.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically closed because it had not recent activity during the stale period.
Most helpful comment
You're reading an old version of the docs: https://docs.docker.com/compose/link-env-deprecated/
Also https://docs.docker.com/compose/networking/. You don't need ips, use the hostname that the alises provide you.