docker-compose.yml:

name: alias
 
services:
  shell:
    image: alpine/curl
    command: tail -f /dev/null
 
  blue:
    image: nginx
    volumes:
      - /tmp/alias/blue.html:/usr/share/nginx/html/index.html:ro
 
  green:
    image: nginx
    volumes:
      - /tmp/alias/green.html:/usr/share/nginx/html/index.html:ro

*blue.html`:

blue

*green.html`:

green
  • Startup: docker compose up -d

  • Test connectivity

    docker compose exec shell curl blue
    docker compose exec shell curl green
    
  • Create network

    docker network create bluegreen
    
  • Connect shell to network, so it can connect to other containers

    docker network connect bluegreen alias-shell-1
    
  • Set current host

    docker network connect bluegreen alias-blue-1 --alias current
    
  • Test current

    docker compose exec shell curl current
    # blue
    
  • Switch to green

    docker network connect bluegreen alias-green-1 --alias current
    docker network disconnect bluegreen alias-blue-1
    
    docker compose exec shell curl current
    # green