title: Docker-compose : understanding linking environment variables
body: <p>I'm now using <code>docker-compose</code> for all of my projects. Very convenient. Much more comfortable than manual linking through several <code>docker</code> commands.</p>
  
  <p>There is something that is not clear to me yet though: the logic behind the linking environment variables.</p>
  
  <p>Eg. with this <code>docker-compose.yml</code>:</p>
  
  <pre><code>mongodb:
    image: mongo
    command: "--smallfiles --logpath=/dev/null"
  web:
    build: .
    command: npm start
    volumes:
      - .:/myapp
    ports:
      - "3001:3000"
    links:
      - mongodb
    environment:
      PORT: 3000
      NODE_ENV: 'development'
  </code></pre>
  
  <p>In the node app, I need to retrieve the mongodb url. And if I <code>console.log(process.env)</code>, I get so many things that it feels very random (just kept the <code>docker-compose</code>-related ones):</p>
  
  <pre><code>MONGODB_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
  MYAPP_MONGODB_1_PORT_27017_TCP_PORT: '27017',
  MYAPP_MONGODB_1_PORT_27017_TCP_PROTO: 'tcp',
  MONGODB_ENV_MONGO_VERSION: '3.2.6',
  MONGODB_1_ENV_GOSU_VERSION: '1.7',
  'MYAPP_MONGODB_1_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
  MYAPP_MONGODB_1_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
  MYAPP_MONGODB_1_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
  MONGODB_1_PORT: 'tcp://172.17.0.2:27017',
  MYAPP_MONGODB_1_ENV_MONGO_VERSION: '3.2.6',
  MONGODB_1_ENV_MONGO_MAJOR: '3.2',
  MONGODB_ENV_GOSU_VERSION: '1.7',
  MONGODB_1_PORT_27017_TCP_ADDR: '172.17.0.2',
  MONGODB_1_NAME: '/myapp_web_1/mongodb_1',
  MONGODB_1_PORT_27017_TCP_PORT: '27017',
  MONGODB_1_PORT_27017_TCP_PROTO: 'tcp',
  'MONGODB_1_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
  MONGODB_PORT: 'tcp://172.17.0.2:27017',
  MONGODB_1_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
  MYAPP_MONGODB_1_ENV_GOSU_VERSION: '1.7',
  MONGODB_ENV_MONGO_MAJOR: '3.2',
  MONGODB_PORT_27017_TCP_ADDR: '172.17.0.2',
  MONGODB_NAME: '/myapp_web_1/mongodb',
  MONGODB_1_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
  MONGODB_PORT_27017_TCP_PORT: '27017',
  MONGODB_1_ENV_MONGO_VERSION: '3.2.6',
  MONGODB_PORT_27017_TCP_PROTO: 'tcp',
  MYAPP_MONGODB_1_PORT: 'tcp://172.17.0.2:27017',
  'MONGODB_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
  MYAPP_MONGODB_1_ENV_MONGO_MAJOR: '3.2',
  MONGODB_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
  MYAPP_MONGODB_1_PORT_27017_TCP_ADDR: '172.17.0.2',
  MYAPP_MONGODB_1_NAME: '/myapp_web_1/novatube_mongodb_1',
  </code></pre>
  
  <p>Don't know what to pick, and why so many entries? Is it better to use the general ones, or the <code>MYAPP</code>-prefixed one? Where does the <code>MYAPP</code> name comes from? Folder name?</p>
  
  <p>Could someone clarify this?</p>
  
  <p>Wouldn't it be easier to let the user define the ones he needs in the <code>docker-compose.yml</code> file with a custom mapping? Like:</p>
  
  <pre><code>  links:
      - mongodb:
        - MONGOIP: IP
        - MONGOPORT : PORT
  </code></pre>
  
  <p>What I'm saying <em>might</em> not have sense though. :-)</p>