#!/bin/bash # Docker网络检查脚本 set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color NETWORK_NAME="proxy" echo -e "${GREEN}检查Docker网络环境...${NC}" # 检查Docker是否安装 if ! command -v docker &> /dev/null; then echo -e "${RED}错误: Docker未安装${NC}" exit 1 fi # 检查proxy网络是否存在 if docker network ls | grep -q ${NETWORK_NAME}; then echo -e "${GREEN}网络 ${NETWORK_NAME} 已存在${NC}" # 显示网络详情 echo -e "${YELLOW}网络详情:${NC}" docker network inspect ${NETWORK_NAME} --format 'table {{.Name}}\t{{.Driver}}\t{{.Scope}}' # 显示连接的容器 echo -e "${YELLOW}连接到 ${NETWORK_NAME} 网络的容器:${NC}" docker network inspect ${NETWORK_NAME} --format '{{range .Containers}}{{.Name}} {{end}}' | tr ' ' '\n' | grep -v '^$' | while read container; do if [ ! -z "$container" ]; then echo -e "${GREEN} - ${container}${NC}" fi done else echo -e "${RED}网络 ${NETWORK_NAME} 不存在${NC}" echo -e "${YELLOW}请先创建网络: docker network create ${NETWORK_NAME}${NC}" exit 1 fi # 检查MySQL容器是否在proxy网络中 echo -e "${YELLOW}检查MySQL容器...${NC}" MYSQL_CONTAINERS=$(docker network inspect ${NETWORK_NAME} --format '{{range .Containers}}{{.Name}} {{end}}' | tr ' ' '\n' | grep -i mysql || true) if [ ! -z "$MYSQL_CONTAINERS" ]; then echo -e "${GREEN}找到MySQL容器:${NC}" echo "$MYSQL_CONTAINERS" | while read container; do if [ ! -z "$container" ]; then echo -e "${GREEN} - ${container}${NC}" fi done else echo -e "${RED}未找到MySQL容器${NC}" echo -e "${YELLOW}请确保MySQL容器已连接到 ${NETWORK_NAME} 网络${NC}" fi # 检查Redis容器是否在proxy网络中 echo -e "${YELLOW}检查Redis容器...${NC}" REDIS_CONTAINERS=$(docker network inspect ${NETWORK_NAME} --format '{{range .Containers}}{{.Name}} {{end}}' | tr ' ' '\n' | grep -i redis || true) if [ ! -z "$REDIS_CONTAINERS" ]; then echo -e "${GREEN}找到Redis容器:${NC}" echo "$REDIS_CONTAINERS" | while read container; do if [ ! -z "$container" ]; then echo -e "${GREEN} - ${container}${NC}" fi done else echo -e "${RED}未找到Redis容器${NC}" echo -e "${YELLOW}请确保Redis容器已连接到 ${NETWORK_NAME} 网络${NC}" fi # 检查Elasticsearch容器是否在proxy网络中 echo -e "${YELLOW}检查Elasticsearch容器...${NC}" ES_CONTAINERS=$(docker network inspect ${NETWORK_NAME} --format '{{range .Containers}}{{.Name}} {{end}}' | tr ' ' '\n' | grep -i elasticsearch || true) if [ ! -z "$ES_CONTAINERS" ]; then echo -e "${GREEN}找到Elasticsearch容器:${NC}" echo "$ES_CONTAINERS" | while read container; do if [ ! -z "$container" ]; then echo -e "${GREEN} - ${container}${NC}" fi done else echo -e "${RED}未找到Elasticsearch容器${NC}" echo -e "${YELLOW}请确保Elasticsearch容器已连接到 ${NETWORK_NAME} 网络${NC}" fi # 测试网络连通性 echo -e "${YELLOW}测试网络连通性...${NC}" if docker run --rm --network ${NETWORK_NAME} alpine ping -c 1 mysql &> /dev/null; then echo -e "${GREEN}可以ping通mysql容器${NC}" else echo -e "${RED}无法ping通mysql容器${NC}" fi if docker run --rm --network ${NETWORK_NAME} alpine ping -c 1 redis &> /dev/null; then echo -e "${GREEN}可以ping通redis容器${NC}" else echo -e "${RED}无法ping通redis容器${NC}" fi if docker run --rm --network ${NETWORK_NAME} alpine ping -c 1 elasticsearch &> /dev/null; then echo -e "${GREEN}可以ping通elasticsearch容器${NC}" else echo -e "${RED}无法ping通elasticsearch容器${NC}" fi echo -e "${GREEN}网络检查完成!${NC}"