Docker Network

created:

updated:

tags: docker

Similar to Docker Volume, I was confused about how Docker Network works. I’m still learning and this time, I got to understand about it a little bit more on docker-compose.yml format.

What is Docker Network?

Networks are the layer that allows services to communicate with each other. The top-level networks element lets you configure named networks that can be reused across multiple services. To use a network across multiple services, you must explicitly grant each service access by using the networks attribute.

Basic Example

In the following example, at runtime, networks front-tier and back-tier are created and the frontend service is connected to front-tier and back-tier networks.

services:
  frontend:
    image: awesome/webapp
    networks:
      - front-tier
      - back-tier

networks:
  front-tier:
  back-tier:

Advanced Example

services:
  proxy:
    build: ./proxy
    networks:
      - frontend
  app:
    build: ./app
    networks:
      - frontend
      - backend
  db:
    image: postgres
    networks:
      - backend

networks:
  frontend:
    # Use a custom driver
    driver: custom-driver-1
  backend:
    # Use a custom driver which takes special options
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

The advancd example shows a Compose file which defines two custom networks. The proxy service is isolated from db service, because they do not share a network in common. Only app can talk to both.

Reference