Part 1: Exploring Docker Compose and Networking Connections: A Browser/Container Perspective

Joseph Whiteaker
2 min readOct 1, 2023

--

In this series' opening segment, we dissect Docker Compose and the dynamics of networking connections. Utilizing a basic example accessible on GitHub, we illustrate the networking connections within browsers and containers in a Docker Compose framework.

Setup and Exploration

Our scenario involves setting up two simplistic apps: an Express backend (docker-app) and a Next.js frontend (ui-docker-app). Running the apps is as simple as executing docker-compose up -d.

version: "3"

services:
api:
container_name: nx-express-api
build:
context: .
dockerfile: ./apps/docker-app/Dockerfile
ports:
- "3333:3333"
restart: unless-stopped
networks:
- api-network

web:
container_name: nx-react-ui
build:
context: .
dockerfile: ./apps/ui-docker-app/Dockerfile
ports:
- "5550:3000"
restart: unless-stopped
networks:
- api-network

networks:
api-network:
driver: bridge

Diving into Connection Mechanisms

Post setup, users can input a URL in the UI to attempt a connection to the backend, viewing the ensuing response. This exposes two distinctive connection methodologies to the API:

  1. Through a Next.js Endpoint: This route involves a request to a Next.js endpoint which, in turn, reaches out to the Express API endpoint.
  2. Direct Browser Interaction: This method directly engages the API from the browser.

Networking Nuances and Insights

Exploring these approaches reveals subtle networking nuances within Docker. A UI container residing in the network cannot simply point to the service name as if it’s host in an API request on the frontend.

Instead, a workaround is necessary, involving the use of the localhost API URL and the corresponding port mapped to the API in the Docker Compose file, to enable proper functionality in the browser.

Closing Part 1

This segment illuminates the interplay between Docker Compose and networking connections, emphasizing the importance of understanding networking nuances and inter-container communications within Docker environments.

--

--