marcv
marcv

Reputation: 1986

Make one Docker Compose service know the domain name of another

I'm using Docker Compose to create two containers. One runs an Nginx web server which serves the mydomain.com website, and the second needs to send HTTP requests to the first one (using the mydomain.com domain name).

I don't want to have to check the Nginx container's ip each time I run it and then use docker run --add-host on the second container. My goal is to run docker-compose up and that everything be ready.

I know it's not possible, but what I'm looking for is something in the line of:

# docker-compose.yml
nginx_container:
    ...

second_container:
    extra_hosts:
         # This is invalid. extra_hosts only accepts ips.
        - "mydomain.com:nginx_container"

Upvotes: 3

Views: 12247

Answers (1)

You can get a similar result using a configuration like this:

version: "3"

services:    
  api:
    image: node:8.9.3
    container_name: foo_api
    domainname: api.foo.test
    command: 'npm run dev'
    links:
      - "mongo:mongo.foo.test"
      - "redis:redis.foo.test"
    volumes:
      - .:/app
      - /app/node_modules
    ports:
      - "${PORT}:3000"
      - "9229:9229"
    depends_on:
      - redis
      - mongo
    networks:
      - backend

  redis:
    image: redis:3
    container_name: foo_redis
    domainname: redis.foo.test
    ports:
      - "6379:6379"
    networks:
      - backend

  mongo:
    image: mongo:3.6.2
    container_name: foo_mongo
    domainname: mongo.foo.test
    ports:
      - "${MONGO_PORT}:27017"
    environment:
      - MONGO_PORT=${MONGO_PORT}
    networks:
      - backend

networks:
  backend:
    driver: bridge

Upvotes: 4

Related Questions