Spring Cloud is based on Docker for packaging and deployment 4-Connection and communication between containers (single host environment)

Today, the deployment project found that the config client can be started normally only when the configuration center config server is fully started, but I can't figure out how to judge that a java application is completely started. The way I think is to put the management of the config server application in a separate file named docker-compose-base.yml, execute docker-compose -f docker-compose-base.yml up to build the image and start the config server container, then sleep in the sh script for a period of time, and then execute the docker compose configuration file containing other applications. However, it is said that the containers started according to different configuration files are network isolated. Today, let's explore this problem:

Problem description: How to set up the connection between containers (including two cases: 1: the container is defined in the same configuration file, 2: the container is defined in different configuration files).

Answer:
1. Related information on the StackOverFlow website:
To set up connections between different containers within the same docker-compose.yml file, use the links parameter as follows: Use  links when you want to link together containers within the same docker-compose.yml. All you need to do is set the link to the service name. Like this:
---
elasticsearch:
  image: elasticsearch:latest
  command: elasticsearch -Des.network.host=0.0.0.0
  ports:
    - "9200:9200"

logstash:
  image: logstash:latest
  command: logstash -f logstash.conf
  ports:
    - "5000:5000"
  links:
    - elasticsearch

To connect containers in different docker-compose.yml, you need to use the external_links parameter to set the name of the target container.

If you want to link a container inside of the docker-compose.yml to another container that was not included in the same docker-compose.yml or started in a different manner then you can use external_links and you would set the link to the container's name. Like this:

---
logstash:
  image: logstash:latest
  command: logstash -f logstash.conf
  ports:
    - "5000:5000"
  external_links:
    - my_elasticsearch_container

It is recommended to put the application in the same docker-compose.yml file

I would suggest the first way unless your use case for some reason requires that they cannot be in the same docker-compose.yml

2. Related information on the 51CTO website:

The case where the container is defined in the same docker-compose.yml file is the same as above, let's see the case in different yml files:

Method: Let the containers that need to be linked belong to the same external network

We still use the nginx image to simulate such a scenario: Suppose we need to link two nignx containers (test1 and test2) managed by Docker Compose, so that test2 can access the services provided in test1, here we can ping it as allow.

First, we define the contents of the docker-compose.yml file of the container test1 as:

version: "3"
services:
  test2:
  image: nginx
  container_name: test1
  networks:
    - default
    - app_net
networks:
  app_net:
  external: true

容器 test2 内容与 test1 基本一样,只是多了一个 external_links ,需要特别说明的是: 最近发布的Docker版本已经不需要使用external_links来链接容器,容器的DNS服务可以正确的作出判断 ,因此如果你你需要兼容较老版本的Docker的话,那么容器 test2 的 docker-compose.yml文件内容为
version: "3" 
services: 
  test2: 
  image: nginx 
  networks: 
    - default 
    - app_net 
  external_links: 
    - test1 
  container_name: test2 
networks: 
  app_net: 
  external: true 

否则的话, test2 的 docker-compose.yml 和 test1 的定义完全一致,不需要额外多指定一个 external_links 。

正如你看到的那样,这里两个容器的定义里都使用了同一个外部网络 app_net ,因此,我们需要在启动这两个容器之前通过以下命令再创建外部网络:

docker network create app_net 
之后,通过 docker-compose up -d 命令启动这两个容器,然后执行 docker exec -it test2 ping test1 ,可以看到两者是可以ping通的。

证明这两个容器是成功链接了,反过来在 test1 中ping test2 也是能够正常ping通的。

如果我们通过 docker run --rm --name test3 -d nginx 这种方式来先启动了一个容器( test3 )并且没有指定它所属的外部网络,而需要将其与 test1 或者 test2 链接的话,这个时候手动链接外部网络即可:

docker network connect app_net test3 

这样,三个容器都可以相互访问了。

重点来了

在以上两篇资料的基础上集合docker的特性我们得到几个有效信息:

(1)在docker的新版本中,已经不需要使用external_links来链接容器,容器的DNS服务可以正确的作出判断 。还有,links也不需要设置了。

(2)使用docker-compose up命令时,如果docker-compose.yml文件中没有配置网络,docker会生成一个默认的网络,而且同一个文件夹下的docker compose配置文件使用的是同一个网络名为  "文件夹名_default"。

所以在项目中如果需要定义两个docker compose的配置文件,可以把两个文件放在同一个文件夹下,那么就不需要自定义网络,也不需要写links或者external_links配置就可以实现所有容器间的互通了。

后记:

实现容器按顺序启动,需要用到depends_on

但是会有一个问题:depends_on只保证启动顺序,而我们的实际需求是:config client容器启动的commands必须等到config server完全启动,config client容器的command才可以执行。我们可以加如restart: always,这样的话config client容器会不断重启直到config server容器启动完成。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482373&siteId=291194637