Alias service environment var in a Docker container
I use Docker Compose to spin up my containers. I have a RethinkDB service container that exposes (amongst others) the host port in the following env var:
However, my app must receive this host as an env var named
My question is: how can I alias the given env var to the desired one when starting the container (preferably in the most Dockerish way)? I tried:
env_file: .env environment: - RETHINKDB_HOST=$APP_RETHINKDB_1_PORT_28015_TCP_ADDR
but first, it doesn’t work and second, it doesn’t look as if it’s the best way to go.
2 Solutions collect form web for “Alias service environment var in a Docker container”
When one container is linked to another, it sets the environment variable, but also a host entry. For example,
ubuntu: links: rethinkdb:rethinkdb
will allow ubuntu to
ping rethinkdb and have it resolve the IP address. This would allow you to set
RETHINKDB_HOST=rethinkdb. This won’t work if you are relying on that variable for the port, however, but that’s the only thing I can think of besides adding a startup script or modifying your
If you want to modify your
CMD, which is currently set to
command: service rethink start, for example, just change it to prepend the variable assignment, e.g.
command: sh -c 'RETHINKDB_HOST=$APP_RETHINKDB_1_PORT_28015_TCP_ADDR && service rethink start'
The approach would be similar if you are using a startup script, you would just add that variable assignment as a line before the service starts
The environment variable name
APP_RETHINKDB_1_PORT_28015_TCP_ADDR you are trying to use already contains the port number. It is already kind of “hard coded”. I think you simply have to use this
environment: - RETHINKDB_HOST=28015