diff between networks and network_mode

tags: learning linux diff-between

content

networks:

  • it’s used to connect containers to custom networks
  • the self-defined network, containers in the same network can communicate to each other via service’s name
  • it creates isolated network segments
  • a container can be connected to multiple networks
services:
	web:
		image: nginx
		networks:
		- frontend
		- backend
		# nginx is connected to two networks

network_mode:

  • attach a container to the network stack of another container or service
  • available values
    • bridge (default): gets its own network namespace
    • none: disable network, container is isolated, no networking at all
    • host: shares host’s network completely, uses host’s ip address
    • container:<name>: uses the network of another container
    • service:<name>: uses the network of another service

differences:

  • networks is for connecting containers
  • network_mode changes the fundamental networking behavior
    • containers are not just connected, they are literally using the same network stack
    • as if they’re the same container

up

down

reference