Connect two instances of docker-compose

非 Y 不嫁゛ 提交于 2020-04-08 10:05:50

问题


I have a dockerized application with a few services running using docker-compose. I'd like to connect this application with ElasticSearch/Logstash/Kibana (ELK) using another docker-compose application, docker-elk. Both of them are running in the same docker machine in development. In production, that will probably not be the case.

How can I configure my application's docker-compose.yml to link to the ELK stack?


回答1:


Update Jun 2016

The answer below is outdated starting with docker 1.10. See this other similar answer for the new solution. https://stackoverflow.com/a/34476794/1556338

Old answer

  1. Create a network:

     $ docker network create --driver bridge my-net
    
  2. Reference that network as an environment variable (${NETWORK})in the docker-compose.yml files. Eg:

    pg:
      image: postgres:9.4.4
      container_name: pg
      net: ${NETWORK}
      ports:
        - "5432"
    myapp:
      image: quay.io/myco/myapp
      container_name: myapp
      environment:
        DATABASE_URL: "http://pg:5432"
      net: ${NETWORK}
      ports:
        - "3000:3000"
    

Note that pg in http://pg:5432 will resolve to the ip address of the pg service (container). No need to hardcode ip addresses; An entry for pg is automatically added to the /etc/host of the myapp container.

  1. Call docker-compose, passing it the network you created:

    $ NETWORK=my-net docker-compose up -d -f docker-compose.yml -f other-compose.yml
    

I've created a bridge network above which only works within one node (host). Good for dev. If you need to get two nodes to talk to each other, you need to create an overlay network. Same principle though. You pass the network name to the docker-compose up command.




回答2:


You could also create a network with docker outside your docker-compose :

docker network create my-shared-network 

And in your docker-compose.yml :

version: '2'
services:
  pg:
    image: postgres:9.4.4
    container_name: pg
    expose:
      - "5432"
networks:
  default:
    external:
      name: my-shared-network

And in your second docker-compose.yml :

version: '2'
  myapp:
    image: quay.io/myco/myapp
    container_name: myapp
    environment:
      DATABASE_URL: "http://pg:5432"
    net: ${NETWORK}
    expose:
      - "3000"
networks:
  default:
    external:
      name: my-shared-network

And both instances will be able to see each other, without open ports on host, you just need to expose ports, and there will see each other through the network : "my-shared-network".




回答3:


Take a look at multi-host docker networking

Networking is a feature of Docker Engine that allows you to create virtual networks and attach containers to them so you can create the network topology that is right for your application. The networked containers can even span multiple hosts, so you don’t have to worry about what host your container lands on. They seamlessly communicate with each other wherever they are – thus enabling true distributed applications.




回答4:


If you set a predictable project name for the first composition you can use external_links to reference external containers by name from a different compose file.

In the next docker-compose release (1.6) you will be able to use user defined networks, and have both compositions join the same network.



来源:https://stackoverflow.com/questions/34393779/connect-two-instances-of-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!