3

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"
marcv
  • 1,874
  • 4
  • 24
  • 45
  • Don't re-invent the wheel. Linked containers for the win. – user2105103 Jul 06 '16 at 19:47
  • My containers are already linked. Could you give more details about how this alone should solve my problem? – marcv Jul 06 '16 at 20:13
  • Fire up your nginx standalone, then try: docker run -it --rm --link :www.test.com alpine /bin/ash Once inside you can ping www.test.com just fine. And see how it is linked by inspecting /etc/hosts. – user2105103 Jul 06 '16 at 20:32
  • Don't use old wheels ;) Use Docker Networking, not links -https://docs.docker.com/compose/networking/ – johnharris85 Jul 07 '16 at 00:07

1 Answers1

3

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