Day 21 Blog: Navigating Docker Compose Networking with a Flask + Redis App

Usman JapUsman Jap
4 min read

The Mission: Supercharge Networking for Flask + Redis

Today’s goal? Enhance my Flask + Redis app with advanced Docker Compose networking—specifically, overlay networks and custom DNS—while practicing commands like docker network create.

Hour 1: Hands-On with the Flask + Redis App

The Plan Goes Awry (But in a Good Way)

My goal was to swap the app’s bridge network for an overlay network called flask-overlay-net. I navigated to my app directory:

cd ~/tmp/flask-redis-app

And created the network:

docker network create -d overlay --attachable flask-overlay-net

Next, I updated docker-compose.yml to use this network:

networks:
  app-net:
    driver: overlay
    name: flask-overlay-net
    attachable: true

I added custom DNS to the web and test services for external resolution:

services:
  web:
    # ... existing config ...
    dns:
      - 8.8.8.8
      - 1.1.1.1
  test:
    # ... existing config ...
    dns:
      - 8.8.8.8
      - 1.1.1.1

To verify connectivity, I tweaked app/app.py to log DNS resolution attempts:

import socket
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
    logger.info(f"DNS resolution for redis-service: {socket.gethostbyname('redis-service')}")
except socket.gaierror as e:
    logger.error(f"DNS resolution failed: {e}")

The Pivot: Swarm Mode to the Rescue

So, I hit a speed bump when I tried to spin up my Flask + Redis app with the overlay network:

docker compose --profile dev up -d --build --wait

Boom! An error:

failed to create network flask-overlay-net: Error response from daemon: This node is not a swarm manager.

Turns out, overlay networks need Docker Swarm mode, and my single-node Ubuntu setup wasn’t in on the Swarm party. No worries—I decided to embrace the challenge and activate Swarm mode to keep my overlay network dream alive!

I fired up Swarm with a single command:

docker swarm init

Like magic, my node became a Swarm manager! I verified it:

docker info --format '{{.Swarm.LocalNodeState}}'

Output: active. Sweet! I recreated the overlay network:

docker network create -d overlay --attachable flask-overlay-net

Then, I retried the Compose command:

docker compose --profile dev up -d --build --wait

Success! docker network inspect flask-overlay-net showed my containers (web, redis, nginx-dev, etc.) chilling on the overlay network. I tested the app:

curl http://localhost:8080

Output: “App from .env: Visited X times.” DNS logs confirmed resolution:

docker compose logs web

Output: “DNS resolution for redis-service: <IP>.” I pinged Google for good measure:

docker compose exec web ping -c 2 google.com

Pings flew through my custom DNS (8.8.8.8, 1.1.1.1). To flex my debugging skills, I disconnected Redis:

docker network disconnect flask-overlay-net flask-redis-app-redis-1
docker compose --profile dev up -d
curl http://localhost:8080

As expected, a 500 error: “Error connecting to Redis: Connection refused.” I checked logs (docker compose logs web), reconnected Redis, and restored the app:

docker network connect flask-overlay-net flask-redis-app-redis-1
docker compose --profile dev up -d --wait
curl http://localhost:8080

Back in action! I committed the fix:

git commit -m "Test and fix network disconnection with overlay network" -a
git push origin main

Hour 2: Commands, Grok, and Blog Prep

Mastering Networking Commands

Next, I practiced networking commands for 15 minutes. I inspected my network:

docker network inspect flask-bridge-net

Tested connectivity:

docker compose --profile dev up -d
docker compose exec web ping -c 2 redis-service

Ping successful! I listed networks:

docker network ls

And asked Grok: “Explain Docker Compose networking commands in 100 words.” Grok’s answer (check my earlier query) was spot-on, highlighting docker network create, inspect, and exec ping.

For a mini-project, I added a custom alias to web in docker-compose.yml:

services:
  web:
    # ... existing config ...
    networks:
      app-net:
        aliases:
          - flask-service
          - flask-web-alias

Tested it:

docker compose --profile dev up -d --build
docker compose exec web ping -c 2 flask-web-alias

Worked like a charm! Committed:

git commit -m "Add custom network alias" -a
git push origin main

Grok Deepens the Fun

For 20 minutes, I leaned on Grok to explore networking. I asked:

  • “Explain best practices for Docker Compose overlay networks in 150 words, with a 2025 example.” Grok suggested using Swarm, --attachable, and Prometheus monitoring (see my earlier query).

  • “How do overlay networks differ from bridge networks in 100 words?” Grok clarified: bridge for single-host, overlay for multi-host with Swarm (check my query).

  • “Step-by-step, how does Docker Compose use overlay networks?” Grok outlined Swarm init, stack deployment, and DNS (see my query).

Using DeepSearch, I found a 2025 X post on #Docker networking, summarizing it in day_21_notes.txt: “Overlay networks shine in Swarm for multi-node apps, but bridge networks rule single-host setups like mine.”


Takeaways and What’s Next

Day 21 was a rollercoaster! I learned overlay networks need Swarm, pivoted to a bridge network, and mastered service discovery, DNS, and debugging. Grok’s insights and X’s community vibe kept me pumped. My resume now boasts “Docker Compose Advanced Networking with Bridge Networks.”

Thanks for joining my Day 21 adventure! Drop a comment on Hashnode or X—what’s your favorite Docker networking trick? Let’s keep containerizing!

0
Subscribe to my newsletter

Read articles from Usman Jap directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Usman Jap
Usman Jap