美文网首页
快速进入 docker sh 脚本

快速进入 docker sh 脚本

作者: 吉凶以情迁 | 来源:发表于2024-08-09 21:33 被阅读0次

    这是我经过反复调整优化后的脚本

    #!/bin/sh
    
    # Function to display containers with numbering
    display_containers() {
      containers=$(docker ps --format "{{.ID}} {{.Names}}")
      echo "Please select a container (enter the number):"
      echo "$containers" | awk '{print NR")",$0}'
    }
    
    # Display containers
    display_containers
    
    # Read user input
    read -p "Enter the number of the container: " input
    
    # Get container ID based on user input
    container_name=$(docker ps --format "{{.Names}}" | sed "${input}q;d")
    
    # Check if container name is empty (invalid input)
    if [ -z "$container_name" ]; then
      echo "Invalid number or container not found"
      exit 1
    fi
    
    # Print the selected container name
    echo "Selected container: $container_name"
    
    # Enter the specified container's terminal
    docker exec -it "$container_name" /bin/sh
    
    image.png

    支持填写全名或者序号

    相关文章

      网友评论

          本文标题:快速进入 docker sh 脚本

          本文链接:https://www.haomeiwen.com/subject/mlknhjtx.html