0
0
Nginxdevops~5 mins

Container networking in Nginx - Commands & Configuration

Choose your learning style9 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.