Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Docker Networking Explained: Bridge, Host and Custom Networks

Use a custom bridge so containers talk by name, publish ports for humans, and isolate unrelated stacks.

Friends, default bridge is fine for one box demos; custom networks give DNS names. Chala mitrano — name your network.

Quick answer

Hold this vertical picture:

  1. List default networks.
  2. Create a custom bridge.
  3. Attach api and db by name.
  4. Publish ports with -p.
  5. Isolate stacks.

Language: English · मराठी · हिंदी

Default bridgeLinks by IP mostly Host networkShares host stack Custom bridgeDNS by container name Publish portsHost:container map Isolate stacksOne network per app
Docker networks

Why networks matter

Containers that cannot find each other by name create random IP chaos in Compose stacks.

Why: Stable DNS names and isolation. What: bridge, host, custom bridge, publish ports. How: Create labnet and wire api+db.

Example 1: List networks

Type this:

command -v docker >/dev/null && docker network ls || printf "%s\n" "NETWORK ID NAME DRIVER\n... bridge bridge\n... host host\n... none null"

You will see:

NETWORK ID NAME DRIVER
... bridge bridge
... host host
... none null

What it means: Every Docker engine starts with bridge, host, and none.

Example 2: Create a custom bridge

Type this:

command -v docker >/dev/null && docker network create labnet || printf "%s\n" "labnet"

You will see:

labnet

What it means: User-defined bridges provide automatic DNS between containers.

Example 3: Inspect the network

Type this:

command -v docker >/dev/null && docker network inspect labnet --format "{{.Name}} {{.Driver}}" || printf "%s\n" "labnet bridge"

You will see:

labnet bridge

What it means: Driver bridge is the usual choice for multi-container apps on one host.

Concept 1: Custom bridge DNS

User-defined bridges resolve container names.

Why: api talks to db by hostname. What: --network NAME on run; Compose networks: How: Prefer this for multi-service apps.

Example 4: Attach by name (sketch)

Type this:

printf "%s\n" "docker run -d --name db --network labnet redis:7" "docker run -d --name api --network labnet myapi:1"

You will see:

docker run -d --name db --network labnet redis:7
docker run -d --name api --network labnet myapi:1

What it means: api can resolve host db on labnet.

Example 7: Compose networks

Type this:

printf "%s\n" "services:" "  api:" "    networks: [appnet]" "  db:" "    networks: [appnet]" "networks:" "  appnet:"

You will see:

services:
  api:
    networks: [appnet]
  db:
    networks: [appnet]
networks:
  appnet:

What it means: Compose creates the user-defined network for you.

Example 8: DNS test sketch

Type this:

printf "%s\n" "docker exec api ping -c1 db"

You will see:

docker exec api ping -c1 db

What it means: Name db works on custom networks; often fails on legacy default bridge without links.

Concept 2: Publish and host mode

Humans reach apps through -p; host mode shares the stack.

Why: Expose carefully. What: -p host:container; --network host rare How: Check conflicts with ss.

Example 5: Publish a port

Type this:

printf "%s\n" "docker run -d -p 8080:80 --name web nginx:alpine"

You will see:

docker run -d -p 8080:80 --name web nginx:alpine

What it means: -p host:container maps traffic into the container.

Example 6: Host network note

Type this:

printf "%s\n" "docker run --network host ... shares the host network namespace"

You will see:

docker run --network host ... shares the host network namespace

What it means: Useful for special cases; skips isolation — rare in shared EC2 labs.

Example 11: Port conflict tip

Type this:

printf "%s\n" "Bind for 0.0.0.0:8080 failed = something else uses 8080" "ss -lntp | grep 8080"

You will see:

Bind for 0.0.0.0:8080 failed = something else uses 8080
ss -lntp | grep 8080

What it means: Check the host before changing the app.

Concept 3: Isolate and move

One network per app stack when possible.

Why: Limit blast radius. What: connect/disconnect; separate names How: Use the network card.

Example 9: Isolate two stacks

Type this:

printf "%s\n" "app1net for shop" "app2net for blog" "Do not share unless needed"

You will see:

app1net for shop
app2net for blog
Do not share unless needed

What it means: Isolation reduces blast radius — Accenture security reviews like this.

Example 10: Disconnect / connect

Type this:

printf "%s\n" "docker network connect labnet api" "docker network disconnect labnet api"

You will see:

docker network connect labnet api
docker network disconnect labnet api

What it means: Move running containers between networks when needed.

Example 12: Network card

Type this:

printf "%s\n" "1 custom bridge 2 same network names 3 -p for humans 4 isolate stacks"

You will see:

1 custom bridge 2 same network names 3 -p for humans 4 isolate stacks

What it means: Memorize for Docker networking interviews.

Network card

  1. Create a user-defined bridge
  2. Put api and db on it
  3. Publish only what users need
  4. Isolate unrelated stacks

Suppose: Shop Compose

appnet for api+db+redis.

Suppose: Port already taken

ss -lntp; pick another host port.

Ravindra Bagale's Tip

Compose on Flipkart labs puts api and db on one user-defined bridge so api talks to db by hostname.

Check yourself (quiz)

Q1. Why custom bridge?

Automatic DNS between container names

Q2. What is -p?

Publish hostPort:containerPort

Q3. Host network?

Shares host network namespace — less isolation

Q4. Compose tip?

Declare networks: and attach services

Q5. Port in use?

ss -lntp and choose another host port

Q6. Isolate stacks?

Separate user-defined networks per app

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

Custom bridge → DNS names → publish → isolate. Did you understand? Let us try it once more.

Frequently asked questions

Why custom bridge?

Automatic DNS between names.

What does -p do?

Maps hostPort to containerPort.

When host network?

Rare special cases; less isolation.

Compose networks?

Declare networks and attach services.

Port in use?

Check ss -lntp; change host port.

Isolate how?

One user-defined network per stack.