SkillSprout
Chapter 1 · Lesson 13
Beginner
1 min read

IP Addresses, Ports and Container DNS

Overview

Container networking combines four ideas: an IP address identifies an interface, a port identifies an application socket, localhost means the current network namespace, and DNS translates names into addresses.

Detailed Notes

IP address

An IP address identifies a network interface. A container commonly receives a private address from Docker's bridge network. That address is useful inside Docker networks but is not necessarily reachable or stable from outside the host.

Port

Ports separate applications on the same IP address. nginx often listens on port 80 in the container. An API can listen on 8080. No two processes on the same network namespace can bind the same port through the same protocol.

-p 8080:80 means:

  1. Docker listens on host port 8080.
  2. Traffic arriving there is sent to container port 80.
  3. The container process must still listen on port 80.

Publishing the host side is separate from container listening.

Localhost does not mean every container

If an API process is in container A and it opens localhost:5432, it looks only inside container A. If PostgreSQL is in container B, localhost is wrong; use the PostgreSQL container or service name.

On a user-defined bridge:

bash
docker network create app-network
docker run -d --name database --network app-network postgres:16
docker run --rm --network app-network postgres:16 pg_isready -h database -p 5432

Docker DNS resolves database to the container's current IP. Service names work better than copying docker inspect IP values because a replacement container may receive a new IP.

Common Interview Questions

Finished this lesson?

0 of 27 lessons done · 0% of the course