Bird
Raised Fist0
Dockerdevops~5 mins

Container to container communication in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes, you run multiple containers that need to talk to each other. Container to container communication lets these containers share data or services easily on the same machine or network.
When you want a web app container to get data from a database container on the same host.
When you run a backend service in one container and a frontend in another, and they need to exchange information.
When you want to split your app into smaller parts that work together but run separately.
When you need to connect a logging container to other containers to collect their logs.
When you want to test how two containers interact before deploying them together.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  webapp:
    image: nginx:1.23
    ports:
      - "8080:80"
    networks:
      - app-network
  database:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
    networks:
      - app-network
networks:
  app-network:
    driver: bridge

This file defines two containers: webapp running Nginx and database running MySQL.

Both containers are connected to the same custom network called app-network. This network allows them to communicate by container name.

The ports section exposes the webapp on the host machine's port 8080.

Commands
This command creates a custom network named app-network so containers can communicate on it.
Terminal
docker network create app-network
Expected OutputExpected
app-network
Starts a MySQL database container named database connected to app-network with a root password set.
Terminal
docker run -d --name database --network app-network -e MYSQL_ROOT_PASSWORD=examplepass mysql:8.0
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
→
-d - Run container in background (detached mode)
→
--name - Assign a name to the container
→
--network - Connect container to a specific network
Starts an Nginx web server container named webapp connected to app-network and exposes port 8080 on the host.
Terminal
docker run -d --name webapp --network app-network -p 8080:80 nginx:1.23
Expected OutputExpected
f1e2d3c4b5a697887766554433221100ffeeddccbbaa99887766554433221100
→
-d - Run container in background (detached mode)
→
--name - Assign a name to the container
→
--network - Connect container to a specific network
→
-p - Map host port to container port
Runs a ping command inside the webapp container to check connectivity to the database container by its name.
Terminal
docker exec webapp ping -c 3 database
Expected OutputExpected
PING database (172.18.0.2): 56 data bytes 64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.123 ms 64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.110 ms 64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.105 ms --- database ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.105/0.112/0.123 ms
→
-c 3 - Send 3 ping packets then stop
Key Concept

Containers on the same user-defined network can communicate using their container names as hostnames.

Common Mistakes
Not connecting containers to the same custom network
Containers on different networks or the default bridge network cannot resolve each other's names, so they cannot communicate by name.
Create a user-defined network and connect all related containers to it.
Using container IP addresses directly for communication
Container IPs can change when containers restart, causing communication failures.
Use container names as hostnames within the same network for stable communication.
Not exposing or mapping ports when external access is needed
Without port mapping, services inside containers are not reachable from the host or outside.
Use the -p flag to map container ports to host ports when needed.
Summary
Create a user-defined Docker network to allow containers to communicate by name.
Run containers attached to this network so they can find each other easily.
Use container names as hostnames inside the network for stable communication.
Verify connectivity by running commands like ping inside one container targeting another.

Practice

(1/5)
1. What is the simplest way for two Docker containers to communicate on the same host?
easy
A. Use a shared volume for communication
B. Use the host's IP address directly inside containers
C. Run containers with --network=none and use port mapping
D. Connect both containers to the same user-defined Docker network

Solution

  1. Step 1: Understand Docker networking basics

    Docker containers on the same user-defined network can resolve each other by container name and communicate directly.
  2. Step 2: Compare other options

    Using host IP or port mapping is less direct and --network=none disables networking. Shared volumes are for file sharing, not network communication.
  3. Final Answer:

    Connect both containers to the same user-defined Docker network -> Option D
  4. Quick Check:

    Same network enables container communication [OK]
Hint: Use a user-defined network for easy container communication [OK]
Common Mistakes:
  • Using default bridge network without user-defined network
  • Trying to communicate via host IP without port mapping
  • Assuming shared volumes enable network communication
2. Which Docker command correctly creates a user-defined network named mynetwork?
easy
A. docker network create mynetwork
B. docker create network mynetwork
C. docker network new mynetwork
D. docker network add mynetwork

Solution

  1. Step 1: Recall Docker network creation syntax

    The correct command to create a network is docker network create <name>.
  2. Step 2: Check other options for syntax errors

    Options B, C, and D use incorrect verbs or commands not recognized by Docker.
  3. Final Answer:

    docker network create mynetwork -> Option A
  4. Quick Check:

    Correct command syntax = docker network create [OK]
Hint: Remember: 'docker network create' is the correct syntax [OK]
Common Mistakes:
  • Using 'docker create network' instead of 'docker network create'
  • Using 'docker network new' which is invalid
  • Trying 'docker network add' which does not exist
3. Given two containers web and db connected to the same user-defined network, what will be the output of docker exec web ping -c 1 db if db is running and reachable?
medium
A. ping: unknown host db
B. PING db (172.18.0.2): 56 data bytes\n64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.123 ms\n\n--- db ping statistics ---\n1 packets transmitted, 1 packets received, 0% packet loss
C. connect: Network is unreachable
D. Request timed out

Solution

  1. Step 1: Understand container DNS resolution on user-defined networks

    Containers on the same user-defined network can resolve each other by container name, so 'db' resolves to its IP.
  2. Step 2: Predict ping command output

    Since 'db' is running and reachable, ping will succeed with a response showing bytes received and 0% packet loss.
  3. Final Answer:

    Successful ping output with 1 packet transmitted and received -> Option B
  4. Quick Check:

    Same network + running container = ping success [OK]
Hint: Ping container name on same network to test connectivity [OK]
Common Mistakes:
  • Assuming default bridge network supports container name resolution
  • Expecting ping to fail without port mapping
  • Confusing network unreachable with DNS resolution failure
4. You created two containers on a user-defined network but web cannot reach db. Which command helps you debug the network connectivity issue?
medium
A. docker ps -a
B. docker container logs web
C. docker network inspect mynetwork
D. docker volume ls

Solution

  1. Step 1: Identify the tool to check network details

    docker network inspect shows connected containers and network settings, useful for debugging connectivity.
  2. Step 2: Evaluate other commands

    docker container logsdocker ps -a lists containers; docker volume ls lists volumes, none show network details.
  3. Final Answer:

    docker network inspect mynetwork -> Option C
  4. Quick Check:

    Inspect network to debug connectivity [OK]
Hint: Use 'docker network inspect' to check container connections [OK]
Common Mistakes:
  • Checking container logs instead of network info
  • Listing volumes which are unrelated
  • Assuming 'docker ps' shows network details
5. You want two containers, app and cache, to communicate securely without exposing ports to the host. Which approach achieves this?
hard
A. Connect both containers to the same user-defined network and communicate via container names
B. Expose ports on both containers and use localhost to connect
C. Use host networking mode for both containers
D. Share a volume and write/read data files for communication

Solution

  1. Step 1: Understand secure container communication

    Using a user-defined network allows containers to communicate internally without exposing ports to the host, keeping communication private.
  2. Step 2: Evaluate other options for security

    Exposing ports or using host networking exposes services outside the host or risks port conflicts. Sharing volumes is not network communication.
  3. Final Answer:

    Connect both containers to the same user-defined network and communicate via container names -> Option A
  4. Quick Check:

    User-defined network keeps communication internal and secure [OK]
Hint: Use user-defined network to avoid exposing ports externally [OK]
Common Mistakes:
  • Exposing ports unnecessarily to the host
  • Using host network mode which is less secure
  • Confusing volume sharing with network communication
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