Bird
Raised Fist0
Dockerdevops~5 mins

Network isolation between services in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run multiple services on the same machine, they can talk to each other by default. Network isolation helps keep services separate so they don't interfere or access each other unless you want them to.
When you want to run a web app and a database on the same server but keep their network traffic separate.
When you have multiple microservices and want to control which services can communicate.
When you want to improve security by limiting network access between containers.
When testing new services without affecting existing ones on the same host.
When you want to avoid port conflicts by isolating service networks.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  webapp:
    image: nginx:1.23
    networks:
      - frontend
  database:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
    networks:
      - backend
networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

This file defines two services: webapp and database. Each service is attached to its own network: frontend and backend. These networks are isolated bridge networks, so containers on frontend cannot communicate with containers on backend unless explicitly connected.

Commands
This command starts the services defined in the docker-compose.yml file in detached mode, creating the isolated networks automatically.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "example_frontend" with driver "bridge" Creating network "example_backend" with driver "bridge" Creating example_database_1 ... done Creating example_webapp_1 ... done
→
-d - Run containers in detached mode (in the background)
Lists all Docker networks to verify that the isolated networks were created.
Terminal
docker network ls
Expected OutputExpected
NETWORK ID NAME DRIVER SCOPE abc123def456 bridge bridge local def789ghi012 example_frontend bridge local ghi345jkl678 example_backend bridge local
Shows details of the frontend network, including which containers are connected to it.
Terminal
docker network inspect example_frontend
Expected OutputExpected
[ { "Name": "example_frontend", "Id": "def789ghi012", "Containers": { "container_id_webapp": { "Name": "example_webapp_1", "IPv4Address": "172.18.0.2/16" } } } ]
Tests network isolation by trying to ping the database container from the webapp container. It should fail because they are on different networks.
Terminal
docker exec example_webapp_1 ping -c 3 example_database_1
Expected OutputExpected
ping: example_database_1: Name or service not known
→
-c 3 - Send 3 ping requests and then stop
Key Concept

If you remember nothing else from this pattern, remember: Docker networks isolate containers so they can only communicate if attached to the same network.

Common Mistakes
Attaching all services to the default bridge network only
All containers can communicate freely, so there is no isolation.
Create and assign custom bridge networks to separate services.
Trying to ping containers by container name across isolated networks
Docker DNS only resolves container names within the same network.
Ensure containers are on the same network or use IP addresses if necessary.
Not specifying networks in docker-compose.yml and expecting isolation
Without explicit networks, all services join the default network and can communicate.
Define custom networks and assign services to them explicitly.
Summary
Define custom bridge networks in docker-compose.yml to isolate services.
Start services with docker-compose up -d to create networks and containers.
Verify networks with docker network ls and inspect to see connected containers.
Test isolation by trying to communicate between containers on different networks.

Practice

(1/5)
1. What is the main purpose of network isolation between Docker services?
easy
A. To keep services separate and control their communication
B. To speed up container startup time
C. To reduce the size of Docker images
D. To automatically update containers

Solution

  1. Step 1: Understand network isolation concept

    Network isolation means keeping services separate so they don't communicate unless allowed.
  2. Step 2: Identify the main goal

    The main goal is to control communication and improve security between services.
  3. Final Answer:

    To keep services separate and control their communication -> Option A
  4. Quick Check:

    Network isolation = separate and control communication [OK]
Hint: Network isolation means controlling service communication [OK]
Common Mistakes:
  • Confusing network isolation with image size optimization
  • Thinking it speeds up container startup
  • Assuming it updates containers automatically
2. Which Docker command creates a custom network for isolating services?
easy
A. docker image build my-network
B. docker container run my-network
C. docker network create my-network
D. docker service start my-network

Solution

  1. Step 1: Identify command to create network

    The command to create a Docker network is docker network create followed by the network name.
  2. Step 2: Match correct syntax

    docker network create my-network uses the correct syntax: docker network create my-network.
  3. Final Answer:

    docker network create my-network -> Option C
  4. Quick Check:

    Creating network = docker network create [OK]
Hint: Use 'docker network create' to make custom networks [OK]
Common Mistakes:
  • Using 'docker container run' to create networks
  • Confusing image build with network creation
  • Trying to start a service to create a network
3. Given two containers attached to different custom networks, what happens if one tries to ping the other by container name?
medium
A. Ping succeeds because Docker allows cross-network communication by default
B. Ping fails because containers on different networks cannot resolve each other's names
C. Ping succeeds only if both containers share the same image
D. Ping fails unless both containers are on the default bridge network

Solution

  1. Step 1: Understand Docker network isolation

    Containers on different custom networks cannot communicate or resolve each other's names by default.
  2. Step 2: Analyze ping behavior

    Since they are on separate networks, ping by container name will fail due to no DNS resolution across networks.
  3. Final Answer:

    Ping fails because containers on different networks cannot resolve each other's names -> Option B
  4. Quick Check:

    Different networks = no name resolution [OK]
Hint: Containers on different networks can't ping by name [OK]
Common Mistakes:
  • Assuming default bridge allows all communication
  • Thinking image similarity affects network reachability
  • Believing Docker allows cross-network DNS by default
4. You created two services on the same custom network but they cannot communicate. What is the most likely mistake?
medium
A. The services have the same container name
B. The network was created with the wrong driver
C. The services are not attached to the same network
D. The containers are running on different Docker hosts

Solution

  1. Step 1: Check network attachment

    If services are on the same network, they should communicate unless other factors block it.
  2. Step 2: Consider Docker host location

    If containers run on different Docker hosts without overlay network, they cannot communicate even on the same network name.
  3. Final Answer:

    The containers are running on different Docker hosts -> Option D
  4. Quick Check:

    Different hosts without overlay = no communication [OK]
Hint: Check if containers run on same host or overlay network [OK]
Common Mistakes:
  • Assuming same network name means same network across hosts
  • Ignoring network driver type
  • Confusing container names with network connectivity
5. You want two services to communicate but keep them isolated from all other containers. Which is the best approach?
hard
A. Create a custom network and attach only these two services to it
B. Use the default bridge network for both services
C. Attach both services to multiple networks including default
D. Run both services on the host network

Solution

  1. Step 1: Understand isolation needs

    To isolate two services from others, they must be on a private network only they share.
  2. Step 2: Choose network setup

    Creating a custom network and attaching only these two services ensures isolation and communication.
  3. Step 3: Evaluate other options

    Default bridge includes all containers, multiple networks reduce isolation, host network removes isolation.
  4. Final Answer:

    Create a custom network and attach only these two services to it -> Option A
  5. Quick Check:

    Custom network with only two services = isolation + communication [OK]
Hint: Use custom network with only needed services attached [OK]
Common Mistakes:
  • Using default network which includes all containers
  • Attaching services to multiple networks losing isolation
  • Using host network which disables isolation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard