Possible to set variable in Docker Compose?

Hello all =)

If I have the below docker-compose-yml file, is it then possible to get the IP addresses of the mongodb and redis contains and have them in the XXX and YYY place holders?

tex:
  image: sharelatex/sharelatex
  ports:
    - "80:80"
  links:
    - mongodb:db
    - redis:red
  environment:
    - SHARELATEX_MONGO_URL=mongodb://XXX/sharelatex
    - SHARELATEX_REDIS_HOST=YYY

mongodb:
  image: tutum/mongodb
  expose:
    - "27017"
    - "28017"
  environment:
    - AUTH=no

redis:
  image: redis
  expose:
    - "6379"

Hi,

Yes it is possible using the syntax ${VAR_NAME}

E.g:
web: build: . ports: - "${EXTERNAL_PORT}:5000"

For more information, check out the Compose File Reference.

HTH,
Alexandre

When you use Docker container links, Docker provides the linked names as DNS names. (If you use the newer private-network system, the container names themselves are DNS names.) So it should work to

  links:
    - mongodb:db
    - redis:red
  environment:
    - SHARELATEX_MONGO_URL=mongodb://db/sharelatex
    - SHARELATEX_REDIS_HOST=red

In my experience you don’t usually need to know containers’ IP addresses (and like all IP addresses they’re prone to changing), but more complicated setups can require some more involved configuration.

1 Like