What are the networking types in Docker, and what is the default? #


Bridge:

  • Default networking for container communication on a single Docker host
docker network create --driver bridge my_bridge

Host:

  • Best for apps needing direct access to the host's network for maximum performance
docker network create --driver host my_host
  • Note: You might get below error if you alreay have one instance of host network running
    Error response from daemon: only one instance of "host" network is allowed
    

Overlay:

  • Critical for multi-host networking in Docker Swarm and distributed apps
    docker network create --driver overlay my_overlay
    # Note: Only work if you're using docker swarm

Macvlan:

  • Used when containers need to appear as physical devices on the network
    # Check your network interface name (ip a)
    # Use the correct interface (ens5, ens3, or eth1 instead of eth0)
    ip a
    
    # Use bridge Mode for Cloud Environments
    # Since AWS does not support VLAN subinterfaces
    docker network create --driver macvlan \
        --subnet=192.168.1.0/24 \
        --gateway=192.168.1.1 \
        -o parent=ens5 \
        my_macvlan

None:

  • Provides complete network isolation for containers
    docker network create --driver null my_none
    
  • Note: You might get below error if you alreay have one instance of null driver is running
    Error response from daemon: only one instance of "null" network is allowed
    

Give some network Management Commands #


List:

  • List all Docker networks
    docker network ls

Inspection:

  • Returns detailed information about a Docker network in JSON format
    docker network inspect <NETWORK_ID or NAME>

How can containers be isolated using a custom Bridge Network? #


  • Create a Custom Bridge Network:
    docker network create isolated-network
  • Run Containers in the Custom Network:
    docker run -d --name container1 \
    --network isolated-network nginx
    
    docker run -d --name container2 \
    --network isolated-network mysql
  • Communication Between Containers: Containers connected to the same custom bridge network (isolated-network in this case) can communicate with each other using their container names or IP addresses assigned by Docker

What is docker-compose.yml, and why is it used along with example? #


  • Docker Compose: It's a tool provided by Docker that allows you to define and manage multi-container Docker applications, whereas the Docker CLI typically requires managing each container individually with separate commands
  • docker-compose.yml: It uses a YAML file called docker-compose.yml to configure the services that make up your application
    • Enable you to orchestrate and run multiple Docker containers as a single application

Example: docker-compose.yml

version: '3.7'
services:
  currency-exchange:
    image: in28min/currency-exchange:0.0.1-RELEASE
    ports:
      - "8000:8000"
    restart: always
    networks:
      - currency-compose-network

  currency-conversion:
    image: in28min/currency-conversion:0.0.1-RELEASE
    ports:
      - "8100:8100"
    restart: always
    environment:
      CURRENCY_EXCHANGE_SERVICE_HOST: http://currency-exchange
    depends_on:
      - currency-exchange
    networks:
      - currency-compose-network

# Networks to be created to facilitate communication between containers
networks:
  currency-compose-network:

Run it:

# Build and Run the Microservices
docker-compose up --build

# Accessing the Microservices
# Currency Exchange API:
http://localhost:8000/currency-exchange/from/USD/to/INR

# Currency Conversion API
http://localhost:8100/currency-conversion/from/USD/to/INR/quantity/10

# Stopping and Cleaning Up
docker-compose down
  • depends_on: It helps define the startup order for your containers
    • It ensures that the currency-exchange service is started before the currency-conversion service
    • Note: While depends_on starts containers in order, it does not guarantee that the dependent service is fully ready
  • environment: Sets the necessary environment variable for service
    • This attribute sets the CURRENCY_EXCHANGE_SERVICE_HOST variable for the currency-conversion service
    • Note: Specify an env_file to load variables from an external file, which helps manage configurations for different environments

πŸ’‘ Explore the Microservices project with detailed guides


List out some docker Compose Commands #


Up:

  • Builds, (re)creates, starts, and attaches to containers for a service
    docker-compose up
    
    # For detach mode, use the -d (follow) option
    docker-compose up -d

Down:

  • Stops and removes containers, networks, volumes, and images created by up
    docker-compose down

Restart specific service:

  • Command stops and then starts only the specific service without affecting the other running services
    docker-compose restart <NAME_OF_SERVICE>

Scale services:

  • To scale or run multiple instances of the service
    docker-compose scale <NAME_OF_SERVICE>=Number
    
    # Example:
    docker-compose scale currency-conversion=3

Build:

  • Builds or rebuilds services defined in the docker-compose.yml
    docker-compose build

Start:

  • Starts existing containers defined in the docker-compose.yml
    docker-compose start

Logs:

  • View the logs of all services defined in the docker-compose.yml
    docker-compose logs
    
    # For continuous, real-time logs, you can use the -f (follow) option
    docker-compose logs -f

How do you share a docker-compose.yml configuration across multiple environments? #


  • Configuration Files: Use a common docker-compose.yml for shared settings
  • Override Files: Create environment-specific override files (e.g., docker-compose.override.yml(Dev), docker-compose.prod.yml)
  • Environment Variables: Parameterize settings using environment variables for dynamic configuration

Example:

1) File Structure:

β”œβ”€β”€ docker-compose.yml           # Base config
β”œβ”€β”€ docker-compose.override.yml  # Dev defaults
β”œβ”€β”€ docker-compose.prod.yml      # Production overrides
β”œβ”€β”€ .env.dev                     # env for dev
β”œβ”€β”€ .env.prod                    # env for prod
β”œβ”€β”€ nginx-prod.conf              # config file for prod
└── html/                        # Sample HTML files
    β”œβ”€β”€ dev/
        └── index.html
    └── prod/
        └── index.html

2) Base Configuration (docker-compose.yml):

version: '3.8'

services:
  web:
    image: ${NGINX_IMAGE:-nginx}
    deploy:
      replicas: ${REPLICAS:-1}
    environment:
      - APP_ENV=${APP_ENV}
    volumes:
      - ${HTML_VOLUME}:/usr/share/nginx/html
    ports:
      - "${NGINX_PORT}:80"

3) Development Override (docker-compose.override.yml):

version: '3.8'

services:
  web:
    volumes:
      - ./html/dev:/usr/share/nginx/html  # Local dev files
    environment:
      - DEBUG=true
    ports:
      - "8080:80"

4) Production Config (docker-compose.prod.yml):

version: '3.8'

services:
  web:
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
    volumes:
      - ./html/prod:/usr/share/nginx/html
    configs:
      - source: nginx-prod-config
        target: /etc/nginx/conf.d/default.conf

configs:
  nginx-prod-config:
    file: ./nginx-prod.conf

5) Environment Files for dev: .env.dev

# Development environment
NGINX_IMAGE=nginx:alpine
REPLICAS=1
APP_ENV=development
NGINX_PORT=8080
HTML_VOLUME=./html/dev

6) Environment Files for prod: .env.prod

# Production environment
NGINX_IMAGE=nginx:latest
REPLICAS=1
APP_ENV=production
NGINX_PORT=80
HTML_VOLUME=./html/prod

7) Sample HTML Files for Dev: ./html/dev/index.html

<!DOCTYPE html>
<html>
<body>
  <h1>Development Environment</h1>
  <h2 style="color: red;">APP_ENV: Dev </h2>
</body>
</html>

8) Sample HTML Files for Prod: ./html/prod/index.html

<!DOCTYPE html>
<html>
<body>
  <h1>Production Environment</h1>
  <h2 style="color: green;">APP_ENV: Prod</h2>
</body>
</html>

9) Nginx configuration file for prod: ./nginx-prod.conf

server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

10) Usage Commands:

docker-compose --env-file .env.dev up -d
# Visit http://localhost:8080

docker-compose -f docker-compose.yml \
-f docker-compose.prod.yml \
--env-file .env.prod up -d
# Visit http://localhost

What are the limitations of Docker Compose? #


  • Single Host Limitation: Primarily designed for single-host deployments; not ideal for multi-host orchestration
  • Scaling Constraints: Limited capabilities for managing large-scale, production-grade cluster
  • Networking Complexity: Basic networking features; lacks advanced features found in orchestration tools like Kubernetes
  • State Management: Not intended for managing persistent state or complex service discovery
  • Example: For production environments, consider using Docker Swarm or Kubernetes for advanced orchestration features