ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤
ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤
Ëæ×ÅÈÝÆ÷ÊÖÒյĿìËÙÉú³¤£¬DockerÒѳÉΪ×îÊܽӴýµÄÈÝÆ÷»¯Æ½Ì¨Ö®Ò»¡£¶øÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤ÊÇʹÓÃDockerʱ±Ø²»¿ÉÉÙµÄÒ»ÏîÊÖÒÕ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Ê¹ÓÃDockerÍøÂçģʽ¾ÙÐиôÀë
DockerÌṩÁ˶àÖÖÍøÂçģʽ£¬°üÀ¨ÇŽÓģʽ£¨bridge£©¡¢Ö÷»úģʽ£¨host£©¡¢ÈÝÆ÷ģʽ£¨container£©ºÍÎÞÍøÂçģʽ£¨none£©µÈ¡£²î±ðµÄÍøÂçģʽÌṩ²î±ðµÄÍøÂç¸ôÀë»úÖÆ£¬¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵÄÍøÂçģʽ¡£
ÇŽÓģʽ£¨bridge£©
ÇŽÓģʽÊÇDockerµÄĬÈÏÍøÂçģʽ£¬Ò²ÊÇ×î³£ÓõÄÍøÂçģʽ֮һ¡£ÔÚÇŽÓģʽÏ£¬Docker»áΪÿ¸öÈÝÆ÷·ÖÅÉÁËÒ»¸ö×ÔÁ¦µÄIPµØµã£¬²¢ÇÒÈÝÆ÷Ö®¼ä¿ÉÒÔͨ¹ýIPµØµã¾ÙÐÐͨѶ¡£
ʹÓÃÇŽÓģʽ¿ÉÒÔ½«ÈÝÆ÷°²ÅÅÔÚÒ»¸ö¸ôÀëµÄÍøÂçÇéÐÎÖУ¬Í¬Ê±Ò²¿ÉÒÔʹÓÃÍøÂçÉèÖÃÀ´ÏÞÖÆÈÝÆ÷Ö®¼äµÄͨѶ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÇŽÓģʽµÄDocker ComposeʾÀý£º
version: '3' services: app1: image: app1:latest networks: - mynetwork app2: image: app2:latest networks: - mynetwork networks: mynetwork:
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÁ½¸öÈÝÆ÷£¬app1ºÍapp2£¬ËüÃǶ¼ÅþÁ¬µ½ÁËÒ»¸öÃûΪmynetworkµÄÍøÂç¡£ÕâÑù£¬app1ºÍapp2¾Í¿ÉÒÔͨ¹ýÍøÂç¾ÙÐÐͨѶÁË¡£
Ö÷»úģʽ£¨host£©
Ö÷»úģʽÊÇDockerµÄÒ»ÖÖÌØÊâÍøÂçģʽ£¬ÔÚÖ÷»úģʽÏ£¬ÈÝÆ÷ÓëËÞÖ÷»ú¹²Ïíͳһ¸öÍøÂçÃüÃû¿Õ¼ä¡£ÕâÒâζ×ÅÈÝÆ÷¿ÉÒÔÖ±½ÓʹÓÃËÞÖ÷»úµÄÍøÂç×°±¸ºÍÍøÂçÉèÖã¬ÈÝÆ÷ÖеÄÓ¦ÓóÌÐòºÍËÞÖ÷»úÖеÄÓ¦ÓóÌÐò¿ÉÒÔʹÓÃÏàͬµÄIPµØµã¡£
ʹÓÃÖ÷»úģʽ¿ÉÒÔÌṩ¸üºÃµÄÍøÂçÐÔÄÜ£¬ÓÉÓÚÈÝÆ÷µÄÍøÂçÁ÷Á¿²»ÐèÒª¾ÓÉÍøÂçµØµãת»»£¨NAT£©µÈ´¦Àí¡£¿ÉÊÇ£¬Ö÷»úģʽµÄÈõµãÊÇÈÝÆ÷ÓëËÞÖ÷»úÖ®¼äûÓÐÍøÂç¸ôÀ룬ÈÝÆ÷ÖеÄÓ¦ÓóÌÐò¿ÉÒÔÖ±½Ó»á¼ûËÞÖ÷»úÉϵÄЧÀͺÍ×ÊÔ´¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÖ÷»úģʽµÄDocker ComposeʾÀý£º
version: '3' services: app: image: app:latest network_mode: "host"
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÈÝÆ÷app£¬²¢Ê¹ÓÃnetwork_mode½«ÆäÉèÖÃΪÖ÷»úģʽ¡£ÕâÑù£¬ÈÝÆ÷app¾Í¿ÉÒÔÓëËÞÖ÷»ú¹²Ïíͳһ¸öÍøÂçÃüÃû¿Õ¼äÁË¡£
¶þ¡¢Ê¹ÓÃDockerÍøÂçÉèÖþÙÐÐÇå¾²±£»¤
³ýÁËÑ¡ÔñºÏÊʵÄÍøÂçģʽ¾ÙÐÐÍøÂç¸ôÀ룬»¹¿ÉÒÔʹÓÃDockerµÄÍøÂçÉèÖþÙÐÐÇå¾²±£»¤¡£
ÄÚÖÃÍøÂç·À»ðǽ
DockerÄÚÖÃÁËÍøÂç·À»ðǽ¹¦Ð§£¬¿ÉÒÔͨ¹ýÉèÖÃÍøÂç¹æÔòÀ´ÏÞÖÆÈÝÆ÷Ö®¼äµÄͨѶ¡£¿ÉÒÔʹÓÃDockerµÄÏÂÁîÐй¤¾ß»òÕß±àдDocker ComposeÎļþÀ´ÉèÖÃÍøÂç¹æÔò¡£ÒÔÏÂÊÇÒ»¸öʹÓÃDockerÏÂÁîÐй¤¾ßÉèÖÃÍøÂç¹æÔòµÄʾÀý£º
# ½¨ÉèÒ»¸öеÄÍøÂç docker network create mynetwork # Ìí¼ÓÍøÂç¹æÔò£¬Õ¥È¡ÈÝÆ÷Ö®¼äµÄͨѶ docker network inspect mynetwork --format='{{range .Containers}}{{.Name}} {{end}}' | xargs -n1 -I{} docker network disconnect -f mynetwork {}
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪmynetworkµÄÍøÂ磬²¢Ê¹ÓÃdocker network inspectÏÂÁî»ñÈ¡Á˸ÃÍøÂçÏÂËùÓÐÈÝÆ÷µÄÃû³Æ£¬È»ºóʹÓÃdocker network disconnectÏÂÁîեȡÈÝÆ÷Ö®¼äµÄͨѶ¡£
ʹÓÃÍøÂçÓÖÃû
DockerÔÊÐíΪÈÝÆ÷ÉèÖÃÍøÂçÓÖÃû£¬¿ÉÒÔÓÃÀ´Òþ²ØÈÝÆ÷µÄÕæʵÃû³Æ£¬Ìá¸ßÈÝÆ÷µÄÇå¾²ÐÔ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃDocker ComposeÉèÖÃÍøÂçÓÖÃûµÄʾÀý£º
version: '3' services: app: image: app:latest networks: mynetwork: aliases: - webapp networks: mynetwork:
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇΪÈÝÆ÷appÉèÖÃÁËÒ»¸öÌåÃûwebapp£¬ÕâÑùÍⲿµÄÈÝÆ÷»òÕßÍøÂçÖ»ÄÜͨ¹ýÓÖÃûwebappÀ´»á¼ûÈÝÆ÷app£¬¶øÎÞ·¨Ö±½ÓʹÓÃÕæʵµÄÈÝÆ÷Ãû³Æ¡£
ʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤¿ÉÒÔÌá¸ßÈÝÆ÷µÄÇå¾²ÐÔºÍÎȹÌÐÔ£¬ïÔÌÈÝÆ÷Ö®¼äµÄ×ÌÈÅ¡£Í¨¹ýÑ¡ÔñÊʵ±µÄÍøÂçģʽºÍÉèÖÃÍøÂç¹æÔò£¬¿ÉÒÔʵÏÖÈÝÆ÷Ö®¼äµÄÍøÂç¸ôÀëºÍÇå¾²±£»¤¡£Í¬Ê±£¬Ê¹ÓÃÍøÂçÓÖÃû¿ÉÒÔÌá¸ßÈÝÆ÷µÄÇå¾²ÐÔ£¬±ÜÃâÈÝÆ÷µÄÕæʵÃû³Æ±»Ì»Â¶¡£
Ï£Íû±¾ÎĵÄÏÈÈݺÍʾÀýÄܹ»×ÊÖú¶ÁÕ߸üºÃµØʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄÍøÂç¸ôÀëºÍÇå¾²±£»¤µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡