Connect two instances of dockers

I have a docked application with several services using docker-compose. I would like to connect this application to ElasticSearch / Logstash / Kibana (ELK) using another docker- building application, docker-elk . Both of them work on the same docker machine in development. In production, this is probably not the case.

How do I configure docker-compose.yml to link to the ELK stack?

+5
source share
4 answers

Jun 2016 Update

The answer below is deprecated since docker 1.10. See Another similar answer for a new solution. fooobar.com/questions/264130 / ...

Old answer

  • Create a network:

      $ docker network create --driver bridge my-net 
  • Link to this network as an environment variable ( ${NETWORK} ) in docker-compose.yml files. For instance:

     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" 

Please note that pg in http://pg:5432 allow the ip address of the pg service (container). No need to hard code IP addresses; An entry for pg is automatically added to the / etc / host container of myapp.

  1. Make a call to docker-compose, passing it the network you created:

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

I created a bridge network , above, which works with only one node (host). Good for dev. If you need two nodes to talk to each other, you need to create

+5
source

Take a look at a network with multiple hosts.

Networking is a Docker Engine feature that allows you to create virtual networks and connect containers to them so that you can create a network topology suitable for your application. Network containers can even span multiple hosts, so you don’t have to worry about which host your container is on. They easily communicate with each other, wherever they are, - thus, applications.

+2
source

If you specify a predictable project name for the first composition, you can use external_links to refer to external containers by name from another layout file.

In the next version of docker-compose (1.6), you can use user-defined networks and have both compositions on the same network.

+2
source

You can also create a docker network outside of your docker:

 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 the host, you just need to set the ports, and there they will see each other through the network: "my-shared-network".

+1
source

Source: https://habr.com/ru/post/1238825/


All Articles