Przejdลบ do treล›ci

๐ŸŒ Docker Networking

Documentation covering Docker networks, ports, and container-to-container communication.


๐Ÿงฉ Network Types

  • bridge โ€” default network; containers can communicate by service name
  • host โ€” no network isolation; container shares host network
  • overlay โ€” multi-host networking for Swarm / Kubernetes
  • macvlan โ€” container receives its own MAC/IP on the physical network

๐Ÿ”ง Common Commands

1
2
3
4
docker network ls
docker network inspect traefik-net
docker network create mynetwork
docker network rm mynetwork

๐Ÿ› ๏ธ Compose Configuration Example

1
2
3
4
5
6
7
8
services:
  app:
    networks:
      - traefik-net

networks:
  traefik-net:
    external: true

Notes:

  • external: true โ†’ the network must exist beforehand
  • containers in the same network can communicate via DNS (service name)

๐Ÿ”— Container-to-Container Communication

  • Containers in the same network can reach each other using service names Example: http://postgres:5432
  • Containers in different networks cannot communicate unless added to a shared network
  • ports: is not required for internal communication

๐Ÿšจ Common Issues & Solutions

Problem Cause Solution
Container cannot reach another Different networks Add both to the same network
Traefik cannot see service Service not in traefik-net Add networks: - traefik-net
Port unreachable Port not exposed or firewall blocking Check ports: and firewall rules
Port conflict Another process uses the port sudo lsof -i :PORT
DNS not working Corrupted bridge network Recreate network
Container fails after adding network Network missing docker network create traefik-net

๐Ÿ“˜ Example: App + Database + Traefik

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
services:
  api:
    image: myapp
    networks:
      - backend
      - traefik-net

  db:
    image: postgres
    networks:
      - backend

networks:
  backend:
  traefik-net:
    external: true

Result:

  • api โ†” db communicate via backend
  • Traefik โ†” api communicate via traefik-net
  • db is isolated (no public ports)