• Docker容器脚本编写(Macvlan)


    /etc/docker/daemon.json

      GNU nano 4.8                                                daemon.json                                                           
    {
     
            "registry-mirrors": [
     
                    "https://cn-guangzhou.mirror.aliyuncs.com"
     
            ],
     
            "dns": [
     
                    "127.0.0.53", "8.8.8.8", "114.114.114.114"
     
            ],
     
            "dns-search":["mshome.net"],
            "dns-option":["trust-ad"]
     

    要创建一个包含Docker容器操作的shell脚本(例如创建、查看状态、停止和启动容器等),你可以按照以下步骤来编写:

    首先,创建一个新的文本文件,并将其命名为docker_operations.sh。然后,使用文本编辑器(如vim或nano)打开此文件,开始编写脚本。

    全部代码如下:

    1. #!/bin/bash
    2. # all definition
    3. NETWORK_NAME="net-1"
    4. VOLUME_MOUNT="-v /home/norten/Public/tools:/mnt"
    5. IMAGE_NAME="ubuntu"
    6. # View help command
    7. function help_container() {
    8. echo " "
    9. echo " "
    10. echo "create: ./docker_operations.sh create [num]"
    11. echo "./docker_operations.sh create 20 *means* create container-20 ,ip=192.168.0.80"
    12. echo " "
    13. echo " "
    14. echo "start: ./docker_operations.sh start [start_num] [end_num]"
    15. echo "./docker_operations.sh create 20 25 *means* start 20-25 containers"
    16. echo "./docker_operations.sh create 20 *means* start one container"
    17. echo " "
    18. echo " "
    19. echo "stop: ./docker_operations.sh stop [start_num] [end_num]"
    20. echo "./docker_operations.sh create 20 25 *means* stop 20-25 containers"
    21. echo "./docker_operations.sh create 20 *means* start one container"
    22. echo " "
    23. echo " "
    24. echo "exec: ./docker_operations.sh exec [num] "
    25. echo "./docker_operations.sh exec 20 *means* execute container-20"
    26. echo "After you execute container ,you should use to exit your container"
    27. echo " "
    28. echo " "
    29. echo ""
    30. echo "Used to see which containers are running"
    31. echo " "
    32. echo " "
    33. echo ""
    34. echo "Used to see all containers exist"
    35. echo " "
    36. echo " "
    37. echo ""
    38. echo "Used to check all information about a container"
    39. echo " "
    40. echo " "
    41. }
    42. # Dynamic container creation
    43. function create_container() {
    44. echo "create zero paremeter is: $0"
    45. echo "create first paremeter is: $1"
    46. echo "create second number is: $2"
    47. local num="$1"
    48. local CONTAINER_IP="192.168.0.$((num+60))"
    49. echo "IP IS $CONTAINER_IP"
    50. local CONTAINER_NAME="container-$num"
    51. # Check whether the IP address is already in use
    52. local existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))
    53. for ip in "${existing_ips[@]}"; do
    54. if [[ "$ip" == "$CONTAINER_IP" ]]; then
    55. echo "Error: IP Address $CONTAINER_IP is already in use by another container."
    56. exit 1
    57. fi
    58. done
    59. # Trying to create a container
    60. docker run -itd \
    61. --name "$CONTAINER_NAME" \
    62. --network="$NETWORK_NAME" \
    63. --ip="$CONTAINER_IP" \
    64. $VOLUME_MOUNT \
    65. $IMAGE_NAME \
    66. && echo "Container $CONTAINER_NAME created with IP $CONTAINER_IP." \
    67. || { echo "Failed to create container $CONTAINER_NAME."; exit 1; }
    68. }
    69. # Start specified or a range of containers
    70. function start_container() {
    71. echo "start zero paremeter is: $0"
    72. echo "start first paremeter is: $1"
    73. echo "start second number is: $2"
    74. local start_num="$1"
    75. local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
    76. for (( i=start_num; i<=end_num; i++ )); do
    77. local CONTAINER_NAME="container-$i"
    78. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    79. echo "Starting container $CONTAINER_NAME..."
    80. docker start "$CONTAINER_NAME"
    81. echo "Container $CONTAINER_NAME started."
    82. else
    83. echo "Error: Container $CONTAINER_NAME does not exist."
    84. exit 1
    85. fi
    86. done
    87. }
    88. # Stop specified or a range of containers
    89. function stop_container() {
    90. local start_num="$1"
    91. local end_num="${2:-$start_num}" # 如果第二个参数未提供,则默认为第一个参数的值
    92. for (( i=start_num; i<=end_num; i++ )); do
    93. local CONTAINER_NAME="container-$i"
    94. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    95. echo "Stopping container $CONTAINER_NAME..."
    96. docker stop "$CONTAINER_NAME"
    97. echo "Container $CONTAINER_NAME stopped."
    98. else
    99. echo "Warning: Container $CONTAINER_NAME does not exist."
    100. fi
    101. done
    102. }
    103. # Enter the shell of a specified container
    104. function exec_container() {
    105. local container_num="$1"
    106. local CONTAINER_NAME="container-$container_num"
    107. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    108. echo "Entering container $CONTAINER_NAME..."
    109. docker exec -it "$CONTAINER_NAME" bash
    110. else
    111. echo "Error: Container $CONTAINER_NAME does not exist or is not running."
    112. exit 1
    113. fi
    114. }
    115. case "$1" in
    116. help)
    117. help_container
    118. ;;
    119. create)
    120. if [ "$#" -ne 2 ]; then
    121. echo "Error: You should provide a parameter after 'create'."
    122. exit 1
    123. fi
    124. if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
    125. echo "Error: The number must be an integer between 1 and 100."
    126. exit 1
    127. fi
    128. create_container "$2"
    129. ;;
    130. start)
    131. # Check the number of parameters to determine whether to start a single container or a container range
    132. if [ "$#" -lt 2 ]; then
    133. echo "Error: You should provide at least one number after 'start'."
    134. exit 1
    135. elif [ "$#" -eq 2 ]; then
    136. # If there are only two parameters, try starting a container
    137. start_container "$2"
    138. elif [ "$#" -eq 3 ]; then
    139. # If you have three parameters, try starting a series of containers
    140. if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]] || ! [[ "$3" =~ ^[1-9][0-9]?$|^100$ ]]; then
    141. echo "Error: Both numbers must be integers between 1 and 100."
    142. exit 1
    143. fi
    144. if [ "$2" -gt "$3" ]; then
    145. echo "Error: The first number must be less than or equal to the second."
    146. exit 1
    147. fi
    148. start_container "$2" "$3"
    149. else
    150. echo "Error: Too many arguments for 'start'."
    151. exit 1
    152. fi
    153. ;;
    154. stop)
    155. if [ "$#" -lt 2 ]; then
    156. echo "Error: You should provide at least one number after 'stop'."
    157. exit 1
    158. fi
    159. if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
    160. echo "Error: The number(s) must be integers between 1 and 100."
    161. exit 1
    162. fi
    163. if [ "$#" -eq 2 ]; then
    164. stop_container "$2"
    165. elif [ "$#" -eq 3 ]; then
    166. if ! [[ "$3" =~ ^[1-9][0-9]?$|^100$ ]]; then
    167. echo "Error: Both numbers must be integers between 1 and 100."
    168. exit 1
    169. fi
    170. if [ "$2" -gt "$3" ]; then
    171. echo "Error: The second number must be greater than or equal to the first."
    172. exit 1
    173. fi
    174. stop_container "$2" "$3"
    175. else
    176. echo "Error: Too many arguments for 'stop'."
    177. exit 1
    178. fi
    179. ;;
    180. exec)
    181. if [ "$#" -ne 2 ]; then
    182. echo "Error: You should provide exactly one number after 'exec'."
    183. exit 1
    184. fi
    185. if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
    186. echo "Error: The number must be an integer between 1 and 100."
    187. exit 1
    188. fi
    189. exec_container "$2"
    190. ;;
    191. logs|status|remove)
    192. echo "Function '$1' has not been updated to handle numbered containers."
    193. exit 1
    194. ;;
    195. *)
    196. echo "Invalid command. Use './docker_operations.sh help' to get instructions."
    197. exit 1
    198. ;;
    199. esac
    200. exit 0

    这个脚本定义了一些基本的函数,分别对应于创建并启动容器、查看日志、检查状态、停止、启动和删除容器的操作。你可以根据需要修改容器名、网络名、IP地址以及挂载的目录路径等。

    保存文件后,给脚本执行权限:

    chmod +x docker_operations.sh

    现在你可以通过以下方式调用不同的操作:

    1. ./docker_operations.sh create # 创建并启动容器
    2. ./docker_operations.sh logs # 查看容器日志
    3. ./docker_operations.sh status # 查看容器状态
    4. ./docker_operations.sh stop # 停止容器
    5. ./docker_operations.sh start # 启动容器
    6. ./docker_operations.sh remove # 删除容器

     "$1"表示获取脚本运行时的第一个参数。

    1. if [ $# -lt 1 ]; then
    2. echo "Error: At least two parameters are required. "
    3. echo "Use './docker_operations.sh help' to get help"
    4. exit 1
    5. fi
    if [ $# -lt 2 ]; then: 这行代码检查了脚本启动时传入的参数数量( $#变量存储了参数的数量)。如果参数少于2个,则执行接下来的错误提示和退出命令。
    • exit 1: 遇到错误时,脚本会退出,并返回错误代码1,通常表示出现了非正常终止。

    1. case "$1" in
    2. create)
    3. if [ -z "$2" ]; then
    4. echo "Error: Please provide a number when creating the container (for example:./docker_operations.sh create 31)."
    5. exit 1
    6. fi
    7. create_container "$2"
    8. ;;
    • if 语句用于条件性地执行命令。它后面跟着一个条件表达式,然后是一个代码块(通常缩进以提高可读性),该代码块仅在条件为真时执行。紧跟代码块之后的是一个 fi 关键字,它是 if 的反向拼写,用来标记 if 语句的结束。

    • if [ -z "$2" ]: 这一行是一个条件测试。-z 测试后面字符串是否为空(即长度是否为零)。"$2" 是脚本的第二个参数。所以,整个条件是在检查是否有提供第二个参数给脚本。如果没有提供(即长度为零),条件为真。

    • then: 如果上面的条件为真(即没有提供第二个参数),则执行接下来的命令直到遇到 fi

    container_run_medium.sh

    1. rm -f *.log
    2. rm -f nohup.out
    3. rm -f cssd.dat
    4. nohup /mnt/simutools/pwbox_simu /mnt/simutools/pw_box.conf &
    5. /mnt/mediumSimu/MediumBoxBase /mnt/mediumSimu/hynn_flash_config_simu.conf

    全套

    1. #!/bin/bash
    2. # all definition
    3. NETWORK_NAME="net-1"
    4. VOLUME_MOUNT="-v /home/norten/Public/tools:/mnt"
    5. IMAGE_NAME="ubuntu"
    6. # View help command
    7. function help_container() {
    8. echo "/mnt/simutools# ./pwbox_simu pw_box.conf "
    9. echo "/mnt/mediumSimu# ./MediumBoxBase hynn_flash_config_simu.conf "
    10. echo " "
    11. echo " "
    12. echo "create: ./docker_operations.sh create [num]" #创建一个容器
    13. echo "./docker_operations.sh create 20 *means* create container-20 ,ip=192.168.0.80"
    14. echo " "
    15. echo " "
    16. echo "start: ./docker_operations.sh start [start_num] [end_num]" #启动容器
    17. echo "./docker_operations.sh start 20 25 *means* start 20-25 containers"
    18. echo "./docker_operations.sh start 20 *means* start one container"
    19. echo " "
    20. echo " "
    21. echo "exec: ./docker_operations.sh exec [num] " #进入容器
    22. echo "./docker_operations.sh exec 20 *means* execute container-20"
    23. echo "After you execute container ,you should use to exit your container"
    24. echo " "
    25. echo " "
    26. echo "stop: ./docker_operations.sh stop [start_num] [end_num]" #停止容器
    27. echo "./docker_operations.sh stop 20 25 *means* stop 20-25 containers"
    28. echo "./docker_operations.sh stop 20 *means* stop one container"
    29. echo " "
    30. echo " "
    31. echo "remove: ./docker_operations.sh remove [num] " #删除容器
    32. echo "./docker_operations.sh remove 20 *means* remove container-20"
    33. echo " "
    34. echo " "
    35. echo "info: ./docker_operations.sh info [container_ID] "
    36. echo " ./docker_operations.sh info a536fbad17b4 " #进入容器后输入命令左边那一串就是容器的ID
    37. echo " "
    38. echo " "
    39. echo "" #查看所有的容器
    40. echo "Used to see which containers are running"
    41. echo " "
    42. echo " "
    43. echo "" #查看正在运行的容器
    44. echo "Used to see all containers exist"
    45. echo " "
    46. echo " "
    47. echo ""
    48. echo "Used to check all information about a container"
    49. echo " "
    50. echo " "
    51. }
    52. # Dynamic container creation
    53. function create_container() {
    54. echo "create zero paremeter is: $0"
    55. echo "create first paremeter is: $1"
    56. echo "create second paremeter is: $2"
    57. local num="$1"
    58. local CONTAINER_IP="192.168.0.$((num+60))"
    59. echo "IP IS $CONTAINER_IP"
    60. local CONTAINER_NAME="container-$num"
    61. # Check whether the IP address is already in use
    62. local existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))
    63. for ip in "${existing_ips[@]}"; do
    64. if [[ "$ip" == "$CONTAINER_IP" ]]; then
    65. echo "Error: IP Address $CONTAINER_IP is already in use by another container."
    66. exit 1
    67. fi
    68. done
    69. # Trying to create a container
    70. docker run -itd \
    71. --name "$CONTAINER_NAME" \
    72. --network="$NETWORK_NAME" \
    73. --ip="$CONTAINER_IP" \
    74. $VOLUME_MOUNT \
    75. $IMAGE_NAME \
    76. && echo "Container $CONTAINER_NAME created with IP $CONTAINER_IP." \
    77. || { echo "Failed to create container $CONTAINER_NAME."; exit 1; }
    78. }
    79. # Start specified or a range of containers
    80. function start_container() {
    81. echo "start zero paremeter is: $0"
    82. echo "start first paremeter is: $1"
    83. echo "start second paremeter is: $2"
    84. local start_num="$1"
    85. local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
    86. for (( i=start_num; i<=end_num; i++ )); do
    87. local CONTAINER_NAME="container-$i"
    88. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    89. echo "Starting container $CONTAINER_NAME..."
    90. docker start "$CONTAINER_NAME"
    91. echo "Container $CONTAINER_NAME started."
    92. else
    93. echo "Error: Container $CONTAINER_NAME does not exist."
    94. exit 1
    95. fi
    96. done
    97. }
    98. # Stop specified or a range of containers
    99. function stop_container() {
    100. local start_num="$1"
    101. local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
    102. for (( i=start_num; i<=end_num; i++ )); do
    103. local CONTAINER_NAME="container-$i"
    104. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    105. echo "Stopping container $CONTAINER_NAME..."
    106. docker stop "$CONTAINER_NAME"
    107. echo "Container $CONTAINER_NAME stopped."
    108. else
    109. echo "Warning: Container $CONTAINER_NAME does not exist."
    110. fi
    111. done
    112. }
    113. # Enter the shell of a specified container
    114. function exec_container() {
    115. local container_num="$1"
    116. local CONTAINER_NAME="container-$container_num"
    117. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    118. echo "Entering container $CONTAINER_NAME..."
    119. docker exec -it "$CONTAINER_NAME" bash
    120. else
    121. echo "Error: Container $CONTAINER_NAME does not exist or is not running."
    122. exit 1
    123. fi
    124. }
    125. # Remove a specified container
    126. function remove_container() {
    127. local container_num="$1"
    128. local CONTAINER_NAME="container-$container_num"
    129. if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
    130. echo "Removing container $CONTAINER_NAME..."
    131. docker rm -f "$CONTAINER_NAME"
    132. echo "Container $CONTAINER_NAME removed."
    133. else
    134. echo "Error: Container $CONTAINER_NAME does not exist."
    135. exit 1
    136. fi
    137. }
    138. # 添加一个新的函数来查询容器信息
    139. function info_container() {
    140. local search_term="$1"
    141. local containers_info=$(docker ps -a --format '{{.ID}} {{.Names}}' | grep "$search_term")
    142. if [ -z "$containers_info" ]; then
    143. echo "No container found matching '$search_term'."
    144. return 1
    145. fi
    146. echo "Matching containers:"
    147. echo "$containers_info" | while read -r container_id container_name; do
    148. echo "ID: $container_id, Name: $container_name"
    149. done
    150. }
    151. case "$1" in
    152. help)
    153. help_container
    154. ;;
    155. create)
    156. if [ "$#" -ne 2 ]; then
    157. echo "Error: You should provide a parameter after 'create'."
    158. exit 1
    159. fi
    160. if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    161. echo "Error: The number must be an integer between 1 and 1000."
    162. exit 1
    163. fi
    164. create_container "$2"
    165. ;;
    166. start)
    167. # Check the number of parameters to determine whether to start a single container or a container range
    168. if [ "$#" -lt 2 ]; then
    169. echo "Error: You should provide at least one number after 'start'."
    170. exit 1
    171. elif [ "$#" -eq 2 ]; then
    172. # If there are only two parameters, try starting a container
    173. start_container "$2"
    174. elif [ "$#" -eq 3 ]; then
    175. # If you have three parameters, try starting a series of containers
    176. if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]] || ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    177. echo "Error: Both numbers must be integers between 1 and 1000."
    178. exit 1
    179. fi
    180. if [ "$2" -gt "$3" ]; then
    181. echo "Error: The first number must be less than or equal to the second."
    182. exit 1
    183. fi
    184. start_container "$2" "$3"
    185. else
    186. echo "Error: Too many arguments for 'start'."
    187. exit 1
    188. fi
    189. ;;
    190. stop)
    191. if [ "$#" -lt 2 ]; then
    192. echo "Error: You should provide at least one number after 'stop'."
    193. exit 1
    194. fi
    195. if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    196. echo "Error: The number(s) must be integers between 1 and 1000."
    197. exit 1
    198. fi
    199. if [ "$#" -eq 2 ]; then
    200. stop_container "$2"
    201. elif [ "$#" -eq 3 ]; then
    202. if ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    203. echo "Error: Both numbers must be integers between 1 and 1000."
    204. exit 1
    205. fi
    206. if [ "$2" -gt "$3" ]; then
    207. echo "Error: The second number must be greater than or equal to the first."
    208. exit 1
    209. fi
    210. stop_container "$2" "$3"
    211. else
    212. echo "Error: Too many arguments for 'stop'."
    213. exit 1
    214. fi
    215. ;;
    216. exec)
    217. if [ "$#" -ne 2 ]; then
    218. echo "Error: You should provide exactly one number after 'exec'."
    219. exit 1
    220. fi
    221. if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    222. echo "Error: The number must be an integer between 1 and 1000."
    223. exit 1
    224. fi
    225. exec_container "$2"
    226. ;;
    227. remove)
    228. if [ "$#" -ne 2 ]; then
    229. echo "Error: You should provide exactly one number after 'remove'."
    230. exit 1
    231. fi
    232. if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
    233. echo "Error: The number must be an integer between 1 and 1000."
    234. exit 1
    235. fi
    236. remove_container "$2"
    237. ;;
    238. info)
    239. if [ "$#" -ne 2 ]; then
    240. echo "Usage: $0 info "
    241. exit 1
    242. fi
    243. info_container "$2"
    244. ;;
    245. logs|status)
    246. echo "Function '$1' has not been updated to handle numbered containers."
    247. exit 1
    248. ;;
    249. *)
    250. echo "Invalid command. Use './docker_operations.sh help' to get instructions."
    251. exit 1
    252. ;;
    253. esac
    254. exit 0

  • 相关阅读:
    16.希尔排序
    一、什么是 HarmonyOS ?
    TIA博途打开程序时提示许可无法彻底完成,发生了内部错误的解决办法
    gcc 和 g++的区别
    BIO、NIO、IO多路复用(select/poll/epoll)、信号驱动IO、异步IO
    asp.net特色商品购物网站系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio
    强大CSS3可视化代码生成器
    DeepStream系列之6.1版本安装及测试
    零基础入门JavaWeb——正则表达式
    取证之查看本机保存的WiFi密码
  • 原文地址:https://blog.csdn.net/m0_52980547/article/details/139860489