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: APP_RETHINKDB_1_PORT_28015_TCP_ADDR.

However, my app must receive this host as an env var named RETHINKDB_HOST.

  • Docker: deploying a new version of an app that uses external resources
  • Using docker-compose 1.8 bundle with Swarm 1.12
  • Setting Dynamic Env variable in Docker
  • Docker Swarm in Multi VLAN/Subnet Application Stack
  • How to use the latest Dart unstable in a Dockerfile?
  • How to run a cron job inside a docker container
  • 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.

  • Unable to build docker image, “must be a root user”
  • docker.py: why pass the volumes parameter twice?
  • Is it safe to export tarball of running docker container?
  • Oracle Instant Client - with Docker and PHP
  • Using GPU from a docker container?
  • Unable to start a Docker container through Remote Docker Java API
  • 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 CMD.

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.