liveBook · Manning

Virtual network

As explained in Learn Docker in a Month of Lunches, Second Edition

A Docker network is a virtual network managed by Docker that enables communication between connected containers.

A container can have an IP address without being reachable from your physical network. Docker assigns that address on a virtual network it manages. Containers connected to the same Docker network can communicate, while outside computers cannot connect directly to a container’s virtual IP address. section 2.4 section 4.2

To make a containerized website reachable from your computer, publish a port. For example, `docker container run --detach --publish 8088:80 diamol/ch02-hello-diamol-web:2e` sends traffic arriving at the computer on port 8088 to port 80 in the container. Visit `http://localhost:8088` to check the result. Publishing a port exposes a route into the container; it does not put the container’s virtual IP address on your physical network. section 2.4

For an application with several containers:

  • Create a network with `docker network create nat`. If `nat` already exists, the resulting error can be ignored. section 4.2
  • Connect a container when starting it: `docker container run --name iotd -d -p 800:80 --network nat image-of-the-day`. Containers on that network can reach one another by container name; the published port separately makes this API reachable at `http://localhost:800/image`. section 4.2
  • For a repeatable setup, declare services and networks in a Docker Compose file rather than retyping each container’s run options. section 7.1
Listing 7.1 A Docker Compose file to run the to-do app
services:

  todo-web:
    image: diamol/ch06-todo-list:2e
    ports:
      - "8020:80"
    networks:
      - app-net

networks:
  app-net:
    name: nat
    external: true

The Compose example connects `todo-web` to `app-net`, which refers to an existing Docker network named `nat`; its `ports` entry handles host access separately. section 7.1

Separate application environments can use isolated Docker networks and unique published ports, allowing development and test versions to run on one server. A Docker virtual network does not join multiple physical machines; spreading containers across servers requires another platform, such as Kubernetes or a cloud service. section 10.5 section 1.3

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Works through network creation, container attachment, port publishing, and Compose configuration; stops short of cross-server networking.
Examples
Uses a published website, a named network for a NASA-image API, and a Compose-configured to-do service.
What it assumes
Starts with a running container and explains virtual IP addresses and published ports without assuming networking internals.
What you can do after
Operate/configure it with Docker commands or Compose to connect containers and publish only the host access an application needs.
How it connects
Links container IP addresses, port publishing, container names, Docker Compose, and environment isolation.

Also explained in

Compare how 4 books explain virtual network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.