Bird
Raised Fist0
Nginxdevops~5 mins

Container networking in Nginx - Commands & Configuration

Choose your learning style10 modes available
Introduction
Containers need to talk to each other and the outside world. Container networking connects containers so they can share data and services safely and efficiently.
When you want your web server container to be reachable from your computer's browser.
When you run a database container and want your app container to connect to it.
When you want to expose a container's service on a specific port on your machine.
When you want multiple containers to communicate inside a private network without exposing ports.
When you want to isolate container traffic for security or performance reasons.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    networks:
      - webnet
  app:
    image: nginx:1.23
    networks:
      - webnet
networks:
  webnet:
    driver: bridge

This file defines two containers, both running nginx. The web service exposes port 80 inside the container to port 8080 on the host, so you can access it via localhost:8080.

Both services are connected to the same custom network webnet using the bridge driver, allowing them to communicate privately.

Commands
This command starts the containers defined in the docker-compose.yml file in detached mode, so they run in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default_webnet" with driver "bridge" Creating default_web_1 ... done Creating default_app_1 ... done
→
-d - Run containers in the background (detached mode)
Lists running containers to verify that the web and app containers are up and running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123def456 nginx:1.23 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp default_web_1 def789ghi012 nginx:1.23 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds default_app_1
Sends a request to the web container through the exposed port to check if the nginx server responds.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
Shows details about the custom network to confirm both containers are connected and can communicate.
Terminal
docker network inspect default_webnet
Expected OutputExpected
[ { "Name": "default_webnet", "Id": "e1f2d3c4b5a6", "Created": "2024-06-01T12:00:00.000000000Z", "Scope": "local", "Driver": "bridge", "Containers": { "abc123def456": { "Name": "default_web_1", "IPv4Address": "172.18.0.2/16" }, "def789ghi012": { "Name": "default_app_1", "IPv4Address": "172.18.0.3/16" } } } ]
Key Concept

If you remember nothing else from container networking, remember: exposing ports connects containers to the outside, and networks connect containers to each other.

Common Mistakes
Not exposing container ports to the host.
Without exposed ports, you cannot access the container's services from your computer or other devices.
Use the ports section in docker-compose or the -p flag in docker run to map container ports to host ports.
Not connecting containers to the same user-defined network.
Containers on different networks cannot communicate by container name, making inter-container communication fail.
Define a custom network and attach all related containers to it.
Using the default bridge network and expecting container names to resolve.
The default bridge network does not support automatic DNS resolution by container name.
Create and use a user-defined bridge network for name resolution.
Summary
Create a docker-compose.yml file defining services and a custom network.
Start containers with docker-compose up -d to run them in the background.
Use docker ps to verify containers are running and ports are exposed.
Test access to container services via exposed ports using curl or a browser.
Inspect the custom network to confirm containers are connected and can communicate.

Practice

(1/5)
1. What is the main purpose of container networking in Docker?
easy
A. To allow containers to communicate with each other
B. To increase container storage space
C. To speed up container startup time
D. To change container CPU limits

Solution

  1. Step 1: Understand container networking basics

    Container networking connects containers so they can share data and services.
  2. Step 2: Identify the purpose

    The main goal is to enable communication between containers.
  3. Final Answer:

    To allow containers to communicate with each other -> Option A
  4. Quick Check:

    Container networking = communication [OK]
Hint: Networking means containers talk to each other [OK]
Common Mistakes:
  • Confusing networking with storage or CPU settings
  • Thinking networking speeds up container startup
  • Assuming networking changes resource limits
2. Which Docker command creates a new custom network for containers?
easy
A. docker create mynet network
B. docker network create mynet
C. docker network new mynet
D. docker create network mynet

Solution

  1. Step 1: Recall Docker network creation syntax

    The correct command is docker network create <name>.
  2. Step 2: Match the options

    Only docker network create mynet matches the correct syntax exactly.
  3. Final Answer:

    docker network create mynet -> Option B
  4. Quick Check:

    docker network create = create network [OK]
Hint: Remember: 'docker network create' makes networks [OK]
Common Mistakes:
  • Swapping order of 'create' and 'network'
  • Using 'new' instead of 'create'
  • Adding extra words after network name
3. What will be the result of running this command?
docker run --network mynet nginx
medium
A. The nginx container connects to the 'mynet' network
B. The nginx container creates a new network named 'mynet'
C. The command fails because '--network' is invalid
D. The container runs without any network connection

Solution

  1. Step 1: Understand the '--network' flag

    The flag connects the container to an existing network named 'mynet'.
  2. Step 2: Analyze the command effect

    The command runs nginx container attached to 'mynet' network.
  3. Final Answer:

    The nginx container connects to the 'mynet' network -> Option A
  4. Quick Check:

    --network attaches container to network [OK]
Hint: Use --network to join existing networks [OK]
Common Mistakes:
  • Thinking it creates a new network
  • Believing '--network' is invalid syntax
  • Assuming container runs without network
4. You ran docker run --network mynet nginx but got an error: "network mynet not found". What is the likely fix?
medium
A. Restart Docker service and try again
B. Remove the '--network' option from the command
C. Create the network first using docker network create mynet
D. Use docker network connect mynet nginx instead

Solution

  1. Step 1: Understand the error message

    The error means the network 'mynet' does not exist yet.
  2. Step 2: Fix by creating the network

    Use docker network create mynet to create it before running the container.
  3. Final Answer:

    Create the network first using docker network create mynet -> Option C
  4. Quick Check:

    Network must exist before use [OK]
Hint: Create network before attaching containers [OK]
Common Mistakes:
  • Skipping network creation
  • Trying to connect without network existing
  • Assuming restart fixes network errors
5. You want two nginx containers to communicate using a custom network. Which steps correctly set this up?
hard
A. Run containers normally, then use docker network connect mynet <container> without creating network
B. Run containers with --link mynet option to connect them
C. Create network with docker create network mynet, then run containers without network option
D. Create network with docker network create mynet, then run both containers with --network mynet

Solution

  1. Step 1: Create a custom network

    Use docker network create mynet to make a network for containers.
  2. Step 2: Run containers attached to the network

    Start both nginx containers with --network mynet to connect them.
  3. Final Answer:

    Create network with docker network create mynet, then run both containers with --network mynet -> Option D
  4. Quick Check:

    Create network + attach containers = communication [OK]
Hint: Create network first, then run containers on it [OK]
Common Mistakes:
  • Using wrong command to create network
  • Not attaching containers to the network
  • Using deprecated --link option
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