How to communicate between Docker containers via “hostname”

后端 未结 5 1220
离开以前
离开以前 2020-11-27 10:02

I plan to split my monolthic server up into many small docker containers but haven\'t found a good solution for \"inter-container communication\" yet. This is my target scen

5条回答
  •  青春惊慌失措
    2020-11-27 10:05

    As far as I know, by using only Docker this is not possible. You need some DNS to map container ip:s to hostnames.

    If you want out of the box solution. One solution is to use for example Kontena. It comes with network overlay technology from Weave and this technology is used to create virtual private LAN networks for each service and every service can be reached by service_name.kontena.local-address.

    Here is simple example of Wordpress application's YAML file where Wordpress service connects to MySQL server with wordpress-mysql.kontena.local address:

    wordpress:                                                                         
      image: wordpress:4.1                                                             
      stateful: true                                                                   
      ports:                                                                           
        - 80:80                                                                      
      links:                                                                           
        - mysql:wordpress-mysql                                                        
      environment:                                                                     
        - WORDPRESS_DB_HOST=wordpress-mysql.kontena.local                              
        - WORDPRESS_DB_PASSWORD=secret                                                 
    mysql:                                                                             
      image: mariadb:5.5                                                               
      stateful: true                                                                   
      environment:                                                                     
        - MYSQL_ROOT_PASSWORD=secret
    

提交回复
热议问题