Enterprise Docker Networking on macOS: Advanced Development Environment Configuration and Cross-Platform Compatibility
Docker networking on macOS presents unique challenges for enterprise development teams due to virtualization layer complexities and platform-specific networking behaviors. Unlike Linux environments where Docker runs natively, macOS implementations require sophisticated configuration to achieve seamless localhost connectivity, cross-platform compatibility, and production-parity networking. This comprehensive guide demonstrates advanced networking patterns, troubleshooting methodologies, and enterprise development environment optimization strategies for macOS-based Docker deployments.
Executive Summary
Enterprise development environments require consistent networking behavior across heterogeneous platforms to ensure development-production parity and minimize deployment issues. Docker Desktop for macOS introduces networking complexity through its VM-based architecture, affecting localhost connectivity, port binding, and service discovery patterns. This implementation guide covers advanced networking configurations, cross-platform compatibility strategies, security considerations, and operational best practices for enterprise macOS development environments.
Understanding macOS Docker Networking Architecture
Virtualization Layer Impact
Docker Desktop on macOS operates through multiple virtualization layers:
macOS Host
├── Docker Desktop VM (Linux)
│ ├── Docker Engine
│ ├── Container Runtime
│ └── Network Stack
├── Hypervisor Framework
└── Network Translation Layer
Key Networking Differences
Linux Docker (Native):
- Direct kernel integration
- Native localhost access
- Shared network namespace
- Direct port binding
macOS Docker (Virtualized):
- VM-based networking
- Network address translation
- Special hostname resolution
- Port forwarding mechanisms
Advanced Networking Configuration
Host Network Access Patterns
Configure proper host network access for enterprise applications:
# docker-compose.yml for cross-platform development
version: '3.8'
services:
web-application:
build: .
ports:
- "8080:8080"
environment:
- DATABASE_HOST=${DATABASE_HOST:-host.docker.internal}
- REDIS_HOST=${REDIS_HOST:-host.docker.internal}
- API_HOST=${API_HOST:-host.docker.internal}
- ENVIRONMENT=${ENVIRONMENT:-development}
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- app-network
api-service:
build: ./api
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:pass@${DATABASE_HOST:-host.docker.internal}:5432/apidb
- REDIS_URL=redis://${REDIS_HOST:-host.docker.internal}:6379
- JWT_SECRET=${JWT_SECRET}
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
- database
- redis
networks:
- app-network
database:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=apidb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init-scripts:/docker-entrypoint-initdb.d:ro
networks:
- app-network
redis:
image: redis:7-alpine
ports:
- "6379:6379"
command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
volumes:
- redis_data:/data
networks:
- app-network
nginx-proxy:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./ssl:/etc/nginx/ssl:ro
depends_on:
- web-application
- api-service
networks:
- app-network
networks:
app-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
volumes:
postgres_data:
driver: local
redis_data:
driver: local
Cross-Platform Environment Configuration
Implement environment-specific configuration management:
#!/bin/bash
# setup-dev-environment.sh
# Detect operating system
OS="$(uname -s)"
ARCH="$(uname -m)"
case "${OS}" in
Darwin*)
PLATFORM="macos"
HOST_IP="host.docker.internal"
;;
Linux*)
PLATFORM="linux"
HOST_IP="172.17.0.1" # Default Docker bridge
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*)
PLATFORM="windows"
HOST_IP="host.docker.internal"
;;
*)
echo "Unsupported platform: ${OS}"
exit 1
;;
esac
echo "Configuring development environment for ${PLATFORM}..."
# Create platform-specific environment file
cat > .env.local << EOF
# Platform: ${PLATFORM}
# Architecture: ${ARCH}
PLATFORM=${PLATFORM}
HOST_IP=${HOST_IP}
# Database configuration
DATABASE_HOST=${HOST_IP}
DATABASE_PORT=5432
DATABASE_NAME=development_db
DATABASE_USER=dev_user
DATABASE_PASSWORD=dev_password
# Redis configuration
REDIS_HOST=${HOST_IP}
REDIS_PORT=6379
# API configuration
API_HOST=${HOST_IP}
API_PORT=3000
API_BASE_URL=http://${HOST_IP}:3000
# Application configuration
APP_ENV=development
DEBUG=true
LOG_LEVEL=debug
# Security configuration (development only)
JWT_SECRET=development_jwt_secret_do_not_use_in_production
CORS_ORIGINS=http://localhost:*,http://127.0.0.1:*,http://${HOST_IP}:*
EOF
# Create Docker Compose override for platform-specific settings
cat > docker-compose.override.yml << EOF
version: '3.8'
services:
web-application:
environment:
- PLATFORM=${PLATFORM}
- HOST_IP=${HOST_IP}
extra_hosts:
- "dockerhost:${HOST_IP}"
volumes:
- .:/app
- /app/node_modules # Prevent node_modules mounting on host
api-service:
environment:
- PLATFORM=${PLATFORM}
- HOST_IP=${HOST_IP}
extra_hosts:
- "dockerhost:${HOST_IP}"
volumes:
- ./api:/app
- /app/node_modules
EOF
# Configure Docker Desktop settings for macOS
if [ "${PLATFORM}" = "macos" ]; then
echo "Configuring Docker Desktop for macOS..."
# Create Docker Desktop configuration
mkdir -p ~/Library/Group\ Containers/group.com.docker/settings
cat > ~/Library/Group\ Containers/group.com.docker/settings/settings.json << EOF
{
"memoryMiB": 8192,
"cpus": 4,
"diskSizeMiB": 102400,
"filesharingDirectories": [
"/Users",
"/Volumes",
"/private",
"/tmp"
],
"proxyHttpMode": "system",
"displayedTutorial": true,
"kubernetesEnabled": true,
"useVirtualizationFramework": true,
"useVirtualizationFrameworkRosetta": true,
"hostNetworkingEnabled": false
}
EOF
echo "Please restart Docker Desktop to apply configuration changes."
fi
echo "Development environment configured for ${PLATFORM}"
echo "Use 'docker-compose up -d' to start the development stack"
Advanced Port Management
Implement sophisticated port management for complex applications:
# Port management configuration
version: '3.8'
x-common-variables: &common-variables
PLATFORM: ${PLATFORM:-macos}
HOST_IP: ${HOST_IP:-host.docker.internal}
services:
# Frontend Development Server
frontend-dev:
build:
context: ./frontend
dockerfile: Dockerfile.dev
ports:
- "${FRONTEND_PORT:-3000}:3000"
environment:
<<: *common-variables
- NODE_ENV=development
- REACT_APP_API_URL=http://${HOST_IP:-host.docker.internal}:${API_PORT:-8080}
- REACT_APP_WS_URL=ws://${HOST_IP:-host.docker.internal}:${WS_PORT:-8081}
volumes:
- ./frontend:/app
- /app/node_modules
- frontend_cache:/app/.next
networks:
- development
# Backend API Server
backend-api:
build:
context: ./backend
dockerfile: Dockerfile.dev
ports:
- "${API_PORT:-8080}:8080"
- "${DEBUG_PORT:-9229}:9229" # Node.js debugger
environment:
<<: *common-variables
- NODE_ENV=development
- DATABASE_URL=postgresql://user:pass@database:5432/appdb
- REDIS_URL=redis://redis:6379
- JWT_SECRET=${JWT_SECRET}
- DEBUG_PORT=9229
volumes:
- ./backend:/app
- /app/node_modules
depends_on:
- database
- redis
networks:
- development
# WebSocket Server
websocket-server:
build:
context: ./websocket
dockerfile: Dockerfile.dev
ports:
- "${WS_PORT:-8081}:8081"
environment:
<<: *common-variables
- NODE_ENV=development
- REDIS_URL=redis://redis:6379
volumes:
- ./websocket:/app
- /app/node_modules
depends_on:
- redis
networks:
- development
# Database
database:
image: postgres:15-alpine
ports:
- "${DB_PORT:-5432}:5432"
environment:
- POSTGRES_DB=appdb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- postgres_data:/var/lib/postgresql/data
- ./database/init:/docker-entrypoint-initdb.d:ro
networks:
- development
# Redis
redis:
image: redis:7-alpine
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
networks:
- development
# Development Tools
mailcatcher:
image: schickling/mailcatcher
ports:
- "${MAIL_WEB_PORT:-1080}:1080"
- "${MAIL_SMTP_PORT:-1025}:1025"
networks:
- development
adminer:
image: adminer:4-standalone
ports:
- "${ADMINER_PORT:-8080}:8080"
environment:
- ADMINER_DEFAULT_SERVER=database
depends_on:
- database
networks:
- development
networks:
development:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
volumes:
postgres_data:
redis_data:
frontend_cache:
Security Configuration for Development
Development Security Best Practices
Implement security controls appropriate for development environments:
# Dockerfile.dev with security considerations
FROM node:18-alpine AS development
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001 -G nodejs
# Install security updates
RUN apk update && \
apk upgrade && \
apk add --no-cache \
dumb-init \
tini && \
rm -rf /var/cache/apk/*
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies with audit
RUN npm ci --only=development && \
npm audit --audit-level=moderate
# Copy application code
COPY --chown=nodejs:nodejs . .
# Switch to non-root user
USER nodejs
# Expose port
EXPOSE 3000
# Use tini for proper signal handling
ENTRYPOINT ["tini", "--"]
CMD ["npm", "run", "dev"]
Network Security Configuration
Configure network security for development environments:
# docker-compose.security.yml
version: '3.8'
services:
# Security scanning
trivy-scanner:
image: aquasec/trivy:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- trivy_cache:/root/.cache/trivy
command: >
sh -c '
while true; do
trivy image --severity HIGH,CRITICAL --format table $(docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>")
sleep 3600
done
'
networks:
- security
# Network monitoring
netshoot:
image: nicolaka/netshoot
command: sleep infinity
networks:
- development
- security
cap_add:
- NET_ADMIN
- NET_RAW
privileged: true # Only for development debugging
# Local certificate authority
local-ca:
build:
context: ./docker/ca
dockerfile: Dockerfile
volumes:
- ca_certs:/certs
environment:
- CA_SUBJECT="/C=US/ST=CA/L=San Francisco/O=Development/OU=IT Department/CN=Development CA"
- CERT_VALIDITY_DAYS=365
networks:
- security
networks:
security:
driver: bridge
internal: true
volumes:
trivy_cache:
ca_certs:
Performance Optimization
File System Performance
Optimize file system performance for macOS development:
# docker-compose.performance.yml
version: '3.8'
x-volume-options: &volume-options
type: bind
bind:
propagation: cached
services:
web-app-optimized:
build: .
volumes:
# Source code with cached propagation
- type: bind
source: ./src
target: /app/src
bind:
propagation: cached
# Dependencies as named volume (faster)
- node_modules:/app/node_modules
# Build output with delegated propagation
- type: bind
source: ./dist
target: /app/dist
bind:
propagation: delegated
# Temporary files in memory
- type: tmpfs
target: /app/tmp
tmpfs:
size: 512m
# Cache directory
- build_cache:/app/.cache
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=false # Disable polling for file watching
- WATCHPACK_POLLING=false
# Performance optimizations
sysctls:
- net.core.somaxconn=65535
ulimits:
nofile:
soft: 65536
hard: 65536
volumes:
node_modules:
driver: local
build_cache:
driver: local
Resource Management
Configure optimal resource allocation:
{
"dockerDesktopSettings": {
"memoryMiB": 12288,
"cpus": 6,
"diskSizeMiB": 204800,
"swapMiB": 2048,
"useVirtualizationFramework": true,
"useVirtualizationFrameworkRosetta": true,
"useGrpcfuse": true,
"vpnKitMaxPortIdleTime": "300s",
"allowExperimentalFeatures": true,
"filesharingDirectories": [
"/Users",
"/Volumes",
"/private",
"/tmp"
],
"hostNetworkingEnabled": false,
"kubernetesEnabled": false,
"showSystemContainers": false,
"resourceSaver": {
"enabled": true,
"cpuThreshold": 25,
"memoryThreshold": 25
}
}
}
Troubleshooting and Diagnostics
Comprehensive Network Diagnostics
Implement advanced troubleshooting tools:
#!/bin/bash
# docker-network-diagnostics.sh
echo "=== Docker Network Diagnostics for macOS ==="
echo "Date: $(date)"
echo "Platform: $(uname -s) $(uname -m)"
echo ""
# Docker version and system info
echo "=== Docker Information ==="
docker version --format 'Client: {{.Client.Version}}, Server: {{.Server.Version}}'
docker system info --format 'CPUs: {{.NCPU}}, Memory: {{.MemTotal}}'
echo ""
# Network configuration
echo "=== Docker Networks ==="
docker network ls
echo ""
# Container networking information
echo "=== Container Networking ==="
for container in $(docker ps --format '{{.Names}}'); do
echo "Container: $container"
docker inspect "$container" --format '{{.NetworkSettings.IPAddress}} {{.NetworkSettings.Ports}}'
echo ""
done
# Host network connectivity tests
echo "=== Host Connectivity Tests ==="
echo "Testing host.docker.internal resolution:"
nslookup host.docker.internal 2>/dev/null || echo "Failed to resolve host.docker.internal"
echo ""
echo "Testing localhost connectivity from container:"
docker run --rm alpine:latest sh -c '
echo "Ping test to host.docker.internal:"
ping -c 3 host.docker.internal 2>/dev/null || echo "Ping failed"
echo "Port connectivity tests:"
nc -zv host.docker.internal 80 2>/dev/null && echo "Port 80: Open" || echo "Port 80: Closed"
nc -zv host.docker.internal 443 2>/dev/null && echo "Port 443: Open" || echo "Port 443: Closed"
nc -zv host.docker.internal 5432 2>/dev/null && echo "Port 5432: Open" || echo "Port 5432: Closed"
'
# macOS specific networking information
echo ""
echo "=== macOS Network Configuration ==="
echo "Network interfaces:"
ifconfig | grep -E "^[a-z]|inet "
echo ""
echo "DNS configuration:"
cat /etc/resolv.conf
echo ""
echo "Host file entries:"
grep -E "(localhost|docker)" /etc/hosts
# Docker Desktop VM information
echo ""
echo "=== Docker Desktop VM Information ==="
docker run --rm --privileged alpine:latest sh -c '
echo "VM network interfaces:"
ip addr show 2>/dev/null || ifconfig
echo ""
echo "VM routing table:"
ip route show 2>/dev/null || route -n
echo ""
echo "VM DNS configuration:"
cat /etc/resolv.conf
'
# Performance metrics
echo ""
echo "=== Performance Metrics ==="
echo "File system performance test:"
time docker run --rm -v "$(pwd)":/data alpine:latest sh -c 'dd if=/dev/zero of=/data/test_file bs=1M count=100 && rm /data/test_file' 2>&1 | grep -E "(real|user|sys)"
echo ""
echo "Network latency test:"
docker run --rm alpine:latest sh -c '
echo "Latency to host.docker.internal:"
ping -c 10 host.docker.internal 2>/dev/null | tail -1 || echo "Ping test failed"
'
Common Issue Resolution
Address frequent macOS Docker networking issues:
#!/bin/bash
# docker-network-fixes.sh
echo "Docker macOS Network Issue Resolution Script"
echo "==========================================="
# Function to check if Docker is running
check_docker() {
if ! docker info >/dev/null 2>&1; then
echo "Error: Docker is not running. Please start Docker Desktop."
exit 1
fi
}
# Function to fix host.docker.internal resolution
fix_host_resolution() {
echo "Fixing host.docker.internal resolution..."
# Add host.docker.internal to /etc/hosts if missing
if ! grep -q "host.docker.internal" /etc/hosts; then
echo "127.0.0.1 host.docker.internal" | sudo tee -a /etc/hosts
echo "Added host.docker.internal to /etc/hosts"
fi
# Test resolution
if nslookup host.docker.internal >/dev/null 2>&1; then
echo "✓ host.docker.internal resolution working"
else
echo "✗ host.docker.internal resolution still failing"
fi
}
# Function to fix Docker daemon socket permissions
fix_docker_socket() {
echo "Fixing Docker socket permissions..."
if [ -S /var/run/docker.sock ]; then
sudo chmod 666 /var/run/docker.sock
echo "✓ Docker socket permissions fixed"
else
echo "✗ Docker socket not found"
fi
}
# Function to reset Docker Desktop network
reset_docker_network() {
echo "Resetting Docker Desktop network..."
read -p "This will restart Docker Desktop. Continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
osascript -e 'quit app "Docker Desktop"'
sleep 5
# Clear Docker networks
docker system prune -f --volumes
# Restart Docker Desktop
open -a "Docker Desktop"
echo "Docker Desktop restarting..."
# Wait for Docker to be ready
echo "Waiting for Docker to be ready..."
while ! docker info >/dev/null 2>&1; do
sleep 2
done
echo "✓ Docker Desktop reset complete"
fi
}
# Function to optimize Docker Desktop settings
optimize_docker_settings() {
echo "Optimizing Docker Desktop settings..."
local settings_file="$HOME/Library/Group Containers/group.com.docker/settings/settings.json"
if [ -f "$settings_file" ]; then
# Backup current settings
cp "$settings_file" "$settings_file.backup"
# Apply optimized settings
cat > "$settings_file" << 'EOF'
{
"memoryMiB": 8192,
"cpus": 4,
"diskSizeMiB": 102400,
"useVirtualizationFramework": true,
"useVirtualizationFrameworkRosetta": true,
"useGrpcfuse": true,
"vpnKitMaxPortIdleTime": "300s",
"filesharingDirectories": [
"/Users",
"/Volumes",
"/private",
"/tmp"
],
"hostNetworkingEnabled": false,
"resourceSaver": {
"enabled": true
}
}
EOF
echo "✓ Docker Desktop settings optimized"
echo "Please restart Docker Desktop to apply changes"
else
echo "✗ Docker Desktop settings file not found"
fi
}
# Function to create test environment
create_test_environment() {
echo "Creating test environment..."
cat > docker-compose.test.yml << 'EOF'
version: '3.8'
services:
test-web:
image: nginx:alpine
ports:
- "8080:80"
networks:
- test-net
test-api:
image: httpd:alpine
ports:
- "8081:80"
networks:
- test-net
networks:
test-net:
driver: bridge
EOF
docker-compose -f docker-compose.test.yml up -d
echo "Test environment created. Testing connectivity..."
sleep 5
# Test connectivity
if curl -s http://localhost:8080 >/dev/null; then
echo "✓ Test web server accessible"
else
echo "✗ Test web server not accessible"
fi
if curl -s http://localhost:8081 >/dev/null; then
echo "✓ Test API server accessible"
else
echo "✗ Test API server not accessible"
fi
read -p "Clean up test environment? (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
docker-compose -f docker-compose.test.yml down
rm docker-compose.test.yml
echo "✓ Test environment cleaned up"
fi
}
# Main menu
check_docker
echo ""
echo "Select fix to apply:"
echo "1) Fix host.docker.internal resolution"
echo "2) Fix Docker socket permissions"
echo "3) Reset Docker Desktop network"
echo "4) Optimize Docker Desktop settings"
echo "5) Create test environment"
echo "6) Run all fixes"
echo "0) Exit"
read -p "Enter choice [0-6]: " choice
case $choice in
1) fix_host_resolution ;;
2) fix_docker_socket ;;
3) reset_docker_network ;;
4) optimize_docker_settings ;;
5) create_test_environment ;;
6)
fix_host_resolution
fix_docker_socket
optimize_docker_settings
echo "All fixes applied. Consider restarting Docker Desktop."
;;
0) echo "Exiting..." ;;
*) echo "Invalid choice" ;;
esac
Enterprise Integration Patterns
CI/CD Pipeline Integration
Configure CI/CD pipelines for cross-platform compatibility:
# .github/workflows/docker-build.yml
name: Docker Build and Test
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18, 20]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Configure platform-specific variables
shell: bash
run: |
case "${{ runner.os }}" in
Linux)
echo "DOCKER_HOST_IP=172.17.0.1" >> $GITHUB_ENV
echo "PLATFORM=linux" >> $GITHUB_ENV
;;
macOS)
echo "DOCKER_HOST_IP=host.docker.internal" >> $GITHUB_ENV
echo "PLATFORM=macos" >> $GITHUB_ENV
;;
Windows)
echo "DOCKER_HOST_IP=host.docker.internal" >> $GITHUB_ENV
echo "PLATFORM=windows" >> $GITHUB_ENV
;;
esac
- name: Create platform-specific environment
shell: bash
run: |
cat > .env.ci << EOF
PLATFORM=${{ env.PLATFORM }}
HOST_IP=${{ env.DOCKER_HOST_IP }}
NODE_VERSION=${{ matrix.node-version }}
DATABASE_HOST=${{ env.DOCKER_HOST_IP }}
REDIS_HOST=${{ env.DOCKER_HOST_IP }}
API_HOST=${{ env.DOCKER_HOST_IP }}
EOF
- name: Build development image
run: |
docker build \
--build-arg NODE_VERSION=${{ matrix.node-version }} \
--build-arg PLATFORM=${{ env.PLATFORM }} \
-t test-image:latest \
-f Dockerfile.dev .
- name: Run security scan
uses: aquasecurity/trivy-action@master
with:
image-ref: 'test-image:latest'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Start test environment
run: |
docker-compose -f docker-compose.yml -f docker-compose.ci.yml up -d
# Wait for services to be ready
timeout 120s bash -c 'until curl -f http://localhost:8080/health; do sleep 2; done'
- name: Run integration tests
run: |
docker-compose exec -T web-application npm test -- --coverage
docker-compose exec -T api-service npm run test:integration
- name: Run cross-platform connectivity tests
shell: bash
run: |
# Test host connectivity
docker run --rm --network host test-image:latest sh -c '
echo "Testing connectivity to host..."
curl -f http://${{ env.DOCKER_HOST_IP }}:8080/health
curl -f http://${{ env.DOCKER_HOST_IP }}:3000/api/health
'
- name: Cleanup
if: always()
run: |
docker-compose down -v
docker system prune -f
Multi-Environment Configuration Management
Implement sophisticated environment management:
#!/bin/bash
# environment-manager.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# Configuration
ENVIRONMENTS=("development" "staging" "testing" "production-local")
CONFIG_DIR="$PROJECT_ROOT/config"
DOCKER_DIR="$PROJECT_ROOT/docker"
# Detect platform
detect_platform() {
case "$(uname -s)" in
Darwin*) echo "macos" ;;
Linux*) echo "linux" ;;
CYGWIN*|MINGW*|MSYS*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
# Generate environment-specific Docker Compose files
generate_compose_files() {
local env="$1"
local platform="$2"
echo "Generating Docker Compose configuration for $env on $platform..."
# Base compose file
cp "$DOCKER_DIR/docker-compose.base.yml" "$PROJECT_ROOT/docker-compose.yml"
# Environment-specific overrides
if [ -f "$DOCKER_DIR/docker-compose.$env.yml" ]; then
cat "$DOCKER_DIR/docker-compose.$env.yml" >> "$PROJECT_ROOT/docker-compose.override.yml"
fi
# Platform-specific overrides
if [ -f "$DOCKER_DIR/docker-compose.$platform.yml" ]; then
cat "$DOCKER_DIR/docker-compose.$platform.yml" >> "$PROJECT_ROOT/docker-compose.override.yml"
fi
# Environment variables
cat > "$PROJECT_ROOT/.env" << EOF
# Generated environment configuration
# Environment: $env
# Platform: $platform
# Generated: $(date)
COMPOSE_PROJECT_NAME=app_${env}
COMPOSE_FILE=docker-compose.yml:docker-compose.override.yml
ENVIRONMENT=$env
PLATFORM=$platform
HOST_IP=$(get_host_ip "$platform")
# Application configuration
NODE_ENV=$env
DEBUG=$([ "$env" = "development" ] && echo "true" || echo "false")
LOG_LEVEL=$([ "$env" = "development" ] && echo "debug" || echo "info")
# Database configuration
DATABASE_HOST=$(get_host_ip "$platform")
DATABASE_PORT=5432
DATABASE_NAME=app_${env}
DATABASE_USER=app_user
DATABASE_PASSWORD=$(generate_password)
# Redis configuration
REDIS_HOST=$(get_host_ip "$platform")
REDIS_PORT=6379
# API configuration
API_HOST=$(get_host_ip "$platform")
API_PORT=3000
API_BASE_URL=http://$(get_host_ip "$platform"):3000
# Security configuration
JWT_SECRET=$(generate_jwt_secret)
ENCRYPTION_KEY=$(generate_encryption_key)
# Feature flags
FEATURE_ADVANCED_LOGGING=$([ "$env" = "development" ] && echo "true" || echo "false")
FEATURE_METRICS_COLLECTION=true
FEATURE_DEBUG_MODE=$([ "$env" = "development" ] && echo "true" || echo "false")
EOF
echo "✓ Configuration generated for $env on $platform"
}
# Get host IP based on platform
get_host_ip() {
case "$1" in
macos|windows) echo "host.docker.internal" ;;
linux) echo "172.17.0.1" ;;
*) echo "localhost" ;;
esac
}
# Generate secure passwords
generate_password() {
openssl rand -base64 32 | tr -d "=+/" | cut -c1-25
}
generate_jwt_secret() {
openssl rand -base64 64 | tr -d "=+/"
}
generate_encryption_key() {
openssl rand -hex 32
}
# Validate environment
validate_environment() {
local env="$1"
if [ ! -f "$PROJECT_ROOT/.env" ]; then
echo "Error: Environment file not found. Run setup first."
return 1
fi
if ! docker-compose config >/dev/null 2>&1; then
echo "Error: Docker Compose configuration is invalid."
return 1
fi
echo "✓ Environment validation passed"
return 0
}
# Main function
main() {
local command="${1:-help}"
local environment="${2:-development}"
local platform="$(detect_platform)"
case "$command" in
setup)
echo "Setting up environment: $environment"
echo "Platform detected: $platform"
generate_compose_files "$environment" "$platform"
;;
validate)
validate_environment "$environment"
;;
start)
if validate_environment "$environment"; then
echo "Starting $environment environment..."
docker-compose up -d
fi
;;
stop)
echo "Stopping environment..."
docker-compose down
;;
restart)
echo "Restarting environment..."
docker-compose restart
;;
logs)
docker-compose logs -f "${@:3}"
;;
clean)
echo "Cleaning up environment..."
docker-compose down -v --remove-orphans
docker system prune -f
;;
help|*)
echo "Usage: $0 {setup|validate|start|stop|restart|logs|clean} [environment]"
echo ""
echo "Commands:"
echo " setup - Generate environment configuration"
echo " validate - Validate current environment"
echo " start - Start the environment"
echo " stop - Stop the environment"
echo " restart - Restart the environment"
echo " logs - Show environment logs"
echo " clean - Clean up environment and resources"
echo ""
echo "Environments: ${ENVIRONMENTS[*]}"
echo "Current platform: $platform"
;;
esac
}
# Execute main function with all arguments
main "$@"
Conclusion
Docker networking on macOS requires sophisticated configuration to achieve enterprise-grade development environments that maintain cross-platform compatibility and production parity. This comprehensive implementation demonstrates advanced networking patterns, security considerations, and operational practices necessary for successful macOS-based Docker deployments.
Key benefits of this enterprise macOS Docker implementation include:
- Cross-Platform Compatibility: Consistent networking behavior across development platforms
- Production Parity: Development environments that closely mirror production infrastructure
- Performance Optimization: Optimized file system and network performance configurations
- Security Integration: Comprehensive security controls appropriate for development environments
- Operational Excellence: Advanced troubleshooting tools and automated environment management
- Enterprise Integration: Seamless CI/CD pipeline integration and multi-environment support
Regular performance monitoring, security assessments, and configuration optimization ensure the continued effectiveness of macOS Docker environments. Consider implementing additional tooling such as development environment automation, advanced debugging capabilities, and team collaboration features based on organizational requirements.
The patterns demonstrated here provide a solid foundation for implementing production-grade Docker development environments on macOS that scale from individual developers to large enterprise teams while maintaining security, performance, and operational efficiency across diverse computing platforms.