/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)打开此文件,开始编写脚本。
- #!/bin/bash
-
- # all definition
- NETWORK_NAME="net-1"
- VOLUME_MOUNT="-v /home/norten/Public/tools:/mnt"
- IMAGE_NAME="ubuntu"
-
- # View help command
- function help_container() {
- echo " "
- echo " "
- echo "create: ./docker_operations.sh create [num]"
- echo "./docker_operations.sh create 20 *means* create container-20 ,ip=192.168.0.80"
- echo " "
- echo " "
- echo "start: ./docker_operations.sh start [start_num] [end_num]"
- echo "./docker_operations.sh create 20 25 *means* start 20-25 containers"
- echo "./docker_operations.sh create 20 *means* start one container"
- echo " "
- echo " "
- echo "stop: ./docker_operations.sh stop [start_num] [end_num]"
- echo "./docker_operations.sh create 20 25 *means* stop 20-25 containers"
- echo "./docker_operations.sh create 20 *means* start one container"
- echo " "
- echo " "
- echo "exec: ./docker_operations.sh exec [num] "
- echo "./docker_operations.sh exec 20 *means* execute container-20"
- echo "After you execute container ,you should use
to exit your container" - echo " "
- echo " "
- echo "
" - echo "Used to see which containers are running"
- echo " "
- echo " "
- echo "
" - echo "Used to see all containers exist"
- echo " "
- echo " "
- echo "
" - echo "Used to check all information about a container"
- echo " "
- echo " "
-
- }
-
- # Dynamic container creation
- function create_container() {
- echo "create zero paremeter is: $0"
- echo "create first paremeter is: $1"
- echo "create second number is: $2"
-
- local num="$1"
- local CONTAINER_IP="192.168.0.$((num+60))"
- echo "IP IS $CONTAINER_IP"
- local CONTAINER_NAME="container-$num"
-
- # Check whether the IP address is already in use
- local existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))
- for ip in "${existing_ips[@]}"; do
- if [[ "$ip" == "$CONTAINER_IP" ]]; then
- echo "Error: IP Address $CONTAINER_IP is already in use by another container."
- exit 1
- fi
- done
-
- # Trying to create a container
- docker run -itd \
- --name "$CONTAINER_NAME" \
- --network="$NETWORK_NAME" \
- --ip="$CONTAINER_IP" \
- $VOLUME_MOUNT \
- $IMAGE_NAME \
- && echo "Container $CONTAINER_NAME created with IP $CONTAINER_IP." \
- || { echo "Failed to create container $CONTAINER_NAME."; exit 1; }
-
- }
-
-
- # Start specified or a range of containers
- function start_container() {
- echo "start zero paremeter is: $0"
- echo "start first paremeter is: $1"
- echo "start second number is: $2"
-
- local start_num="$1"
- local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
-
- for (( i=start_num; i<=end_num; i++ )); do
- local CONTAINER_NAME="container-$i"
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Starting container $CONTAINER_NAME..."
- docker start "$CONTAINER_NAME"
- echo "Container $CONTAINER_NAME started."
- else
- echo "Error: Container $CONTAINER_NAME does not exist."
- exit 1
- fi
- done
- }
-
-
- # Stop specified or a range of containers
- function stop_container() {
- local start_num="$1"
- local end_num="${2:-$start_num}" # 如果第二个参数未提供,则默认为第一个参数的值
-
- for (( i=start_num; i<=end_num; i++ )); do
- local CONTAINER_NAME="container-$i"
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Stopping container $CONTAINER_NAME..."
- docker stop "$CONTAINER_NAME"
- echo "Container $CONTAINER_NAME stopped."
- else
- echo "Warning: Container $CONTAINER_NAME does not exist."
- fi
- done
- }
-
- # Enter the shell of a specified container
- function exec_container() {
- local container_num="$1"
- local CONTAINER_NAME="container-$container_num"
-
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Entering container $CONTAINER_NAME..."
- docker exec -it "$CONTAINER_NAME" bash
- else
- echo "Error: Container $CONTAINER_NAME does not exist or is not running."
- exit 1
- fi
- }
-
-
- case "$1" in
- help)
- help_container
- ;;
-
- create)
- if [ "$#" -ne 2 ]; then
- echo "Error: You should provide a parameter after 'create'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
- echo "Error: The number must be an integer between 1 and 100."
- exit 1
- fi
-
- create_container "$2"
- ;;
-
- start)
- # Check the number of parameters to determine whether to start a single container or a container range
- if [ "$#" -lt 2 ]; then
- echo "Error: You should provide at least one number after 'start'."
- exit 1
- elif [ "$#" -eq 2 ]; then
- # If there are only two parameters, try starting a container
- start_container "$2"
- elif [ "$#" -eq 3 ]; then
- # If you have three parameters, try starting a series of containers
- if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]] || ! [[ "$3" =~ ^[1-9][0-9]?$|^100$ ]]; then
- echo "Error: Both numbers must be integers between 1 and 100."
- exit 1
- fi
- if [ "$2" -gt "$3" ]; then
- echo "Error: The first number must be less than or equal to the second."
- exit 1
- fi
- start_container "$2" "$3"
- else
- echo "Error: Too many arguments for 'start'."
- exit 1
- fi
- ;;
-
- stop)
- if [ "$#" -lt 2 ]; then
- echo "Error: You should provide at least one number after 'stop'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
- echo "Error: The number(s) must be integers between 1 and 100."
- exit 1
- fi
-
- if [ "$#" -eq 2 ]; then
- stop_container "$2"
- elif [ "$#" -eq 3 ]; then
- if ! [[ "$3" =~ ^[1-9][0-9]?$|^100$ ]]; then
- echo "Error: Both numbers must be integers between 1 and 100."
- exit 1
- fi
- if [ "$2" -gt "$3" ]; then
- echo "Error: The second number must be greater than or equal to the first."
- exit 1
- fi
- stop_container "$2" "$3"
- else
- echo "Error: Too many arguments for 'stop'."
- exit 1
- fi
- ;;
-
- exec)
- if [ "$#" -ne 2 ]; then
- echo "Error: You should provide exactly one number after 'exec'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]?$|^100$ ]]; then
- echo "Error: The number must be an integer between 1 and 100."
- exit 1
- fi
-
- exec_container "$2"
- ;;
-
- logs|status|remove)
- echo "Function '$1' has not been updated to handle numbered containers."
- exit 1
- ;;
- *)
- echo "Invalid command. Use './docker_operations.sh help' to get instructions."
- exit 1
- ;;
- esac
-
- exit 0
这个脚本定义了一些基本的函数,分别对应于创建并启动容器、查看日志、检查状态、停止、启动和删除容器的操作。你可以根据需要修改容器名、网络名、IP地址以及挂载的目录路径等。
保存文件后,给脚本执行权限:
chmod +x docker_operations.sh
现在你可以通过以下方式调用不同的操作:
- ./docker_operations.sh create # 创建并启动容器
- ./docker_operations.sh logs # 查看容器日志
- ./docker_operations.sh status # 查看容器状态
- ./docker_operations.sh stop # 停止容器
- ./docker_operations.sh start # 启动容器
- ./docker_operations.sh remove # 删除容器
"$1"
表示获取脚本运行时的第一个参数。
- if [ $# -lt 1 ]; then
- echo "Error: At least two parameters are required. "
- echo "Use './docker_operations.sh help' to get help"
- exit 1
- fi
if [ $# -lt 2 ]; then
: 这行代码检查了脚本启动时传入的参数数量($#
变量存储了参数的数量)。如果参数少于2个,则执行接下来的错误提示和退出命令。
exit 1
: 遇到错误时,脚本会退出,并返回错误代码1,通常表示出现了非正常终止。
- case "$1" in
- create)
- if [ -z "$2" ]; then
- echo "Error: Please provide a number when creating the container (for example:./docker_operations.sh create 31)."
- exit 1
- fi
- create_container "$2"
- ;;
if
语句用于条件性地执行命令。它后面跟着一个条件表达式,然后是一个代码块(通常缩进以提高可读性),该代码块仅在条件为真时执行。紧跟代码块之后的是一个fi
关键字,它是if
的反向拼写,用来标记if
语句的结束。
if [ -z "$2" ]
: 这一行是一个条件测试。-z
测试后面字符串是否为空(即长度是否为零)。"$2"
是脚本的第二个参数。所以,整个条件是在检查是否有提供第二个参数给脚本。如果没有提供(即长度为零),条件为真。
then
: 如果上面的条件为真(即没有提供第二个参数),则执行接下来的命令直到遇到fi
。
container_run_medium.sh
- rm -f *.log
- rm -f nohup.out
- rm -f cssd.dat
- nohup /mnt/simutools/pwbox_simu /mnt/simutools/pw_box.conf &
- /mnt/mediumSimu/MediumBoxBase /mnt/mediumSimu/hynn_flash_config_simu.conf
- #!/bin/bash
-
- # all definition
- NETWORK_NAME="net-1"
- VOLUME_MOUNT="-v /home/norten/Public/tools:/mnt"
- IMAGE_NAME="ubuntu"
-
- # View help command
- function help_container() {
- echo "/mnt/simutools# ./pwbox_simu pw_box.conf "
- echo "/mnt/mediumSimu# ./MediumBoxBase hynn_flash_config_simu.conf "
- echo " "
- echo " "
- echo "create: ./docker_operations.sh create [num]" #创建一个容器
- echo "./docker_operations.sh create 20 *means* create container-20 ,ip=192.168.0.80"
- echo " "
- echo " "
- echo "start: ./docker_operations.sh start [start_num] [end_num]" #启动容器
- echo "./docker_operations.sh start 20 25 *means* start 20-25 containers"
- echo "./docker_operations.sh start 20 *means* start one container"
- echo " "
- echo " "
- echo "exec: ./docker_operations.sh exec [num] " #进入容器
- echo "./docker_operations.sh exec 20 *means* execute container-20"
- echo "After you execute container ,you should use
to exit your container" - echo " "
- echo " "
- echo "stop: ./docker_operations.sh stop [start_num] [end_num]" #停止容器
- echo "./docker_operations.sh stop 20 25 *means* stop 20-25 containers"
- echo "./docker_operations.sh stop 20 *means* stop one container"
- echo " "
- echo " "
- echo "remove: ./docker_operations.sh remove [num] " #删除容器
- echo "./docker_operations.sh remove 20 *means* remove container-20"
- echo " "
- echo " "
- echo "info: ./docker_operations.sh info [container_ID] "
- echo " ./docker_operations.sh info a536fbad17b4 " #进入容器后输入命令左边那一串就是容器的ID
- echo " "
- echo " "
- echo "
" #查看所有的容器 - echo "Used to see which containers are running"
- echo " "
- echo " "
- echo "
" #查看正在运行的容器 - echo "Used to see all containers exist"
- echo " "
- echo " "
- echo "
" - echo "Used to check all information about a container"
- echo " "
- echo " "
-
- }
-
- # Dynamic container creation
- function create_container() {
- echo "create zero paremeter is: $0"
- echo "create first paremeter is: $1"
- echo "create second paremeter is: $2"
-
- local num="$1"
- local CONTAINER_IP="192.168.0.$((num+60))"
- echo "IP IS $CONTAINER_IP"
- local CONTAINER_NAME="container-$num"
-
- # Check whether the IP address is already in use
- local existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))
- for ip in "${existing_ips[@]}"; do
- if [[ "$ip" == "$CONTAINER_IP" ]]; then
- echo "Error: IP Address $CONTAINER_IP is already in use by another container."
- exit 1
- fi
- done
-
- # Trying to create a container
- docker run -itd \
- --name "$CONTAINER_NAME" \
- --network="$NETWORK_NAME" \
- --ip="$CONTAINER_IP" \
- $VOLUME_MOUNT \
- $IMAGE_NAME \
- && echo "Container $CONTAINER_NAME created with IP $CONTAINER_IP." \
- || { echo "Failed to create container $CONTAINER_NAME."; exit 1; }
-
- }
-
-
- # Start specified or a range of containers
- function start_container() {
- echo "start zero paremeter is: $0"
- echo "start first paremeter is: $1"
- echo "start second paremeter is: $2"
-
- local start_num="$1"
- local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
-
- for (( i=start_num; i<=end_num; i++ )); do
- local CONTAINER_NAME="container-$i"
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Starting container $CONTAINER_NAME..."
- docker start "$CONTAINER_NAME"
- echo "Container $CONTAINER_NAME started."
- else
- echo "Error: Container $CONTAINER_NAME does not exist."
- exit 1
- fi
- done
- }
-
-
- # Stop specified or a range of containers
- function stop_container() {
- local start_num="$1"
- local end_num="${2:-$start_num}" # If the second argument is not provided, it defaults to the value of the first argument
-
- for (( i=start_num; i<=end_num; i++ )); do
- local CONTAINER_NAME="container-$i"
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Stopping container $CONTAINER_NAME..."
- docker stop "$CONTAINER_NAME"
- echo "Container $CONTAINER_NAME stopped."
- else
- echo "Warning: Container $CONTAINER_NAME does not exist."
- fi
- done
- }
-
- # Enter the shell of a specified container
- function exec_container() {
- local container_num="$1"
- local CONTAINER_NAME="container-$container_num"
-
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Entering container $CONTAINER_NAME..."
- docker exec -it "$CONTAINER_NAME" bash
- else
- echo "Error: Container $CONTAINER_NAME does not exist or is not running."
- exit 1
- fi
- }
-
-
- # Remove a specified container
- function remove_container() {
- local container_num="$1"
- local CONTAINER_NAME="container-$container_num"
-
- if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
- echo "Removing container $CONTAINER_NAME..."
- docker rm -f "$CONTAINER_NAME"
- echo "Container $CONTAINER_NAME removed."
- else
- echo "Error: Container $CONTAINER_NAME does not exist."
- exit 1
- fi
- }
-
-
- # 添加一个新的函数来查询容器信息
- function info_container() {
- local search_term="$1"
- local containers_info=$(docker ps -a --format '{{.ID}} {{.Names}}' | grep "$search_term")
-
- if [ -z "$containers_info" ]; then
- echo "No container found matching '$search_term'."
- return 1
- fi
-
- echo "Matching containers:"
- echo "$containers_info" | while read -r container_id container_name; do
- echo "ID: $container_id, Name: $container_name"
- done
- }
-
-
- case "$1" in
- help)
- help_container
- ;;
-
- create)
- if [ "$#" -ne 2 ]; then
- echo "Error: You should provide a parameter after 'create'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
- echo "Error: The number must be an integer between 1 and 1000."
- exit 1
- fi
-
- create_container "$2"
- ;;
-
- start)
- # Check the number of parameters to determine whether to start a single container or a container range
- if [ "$#" -lt 2 ]; then
- echo "Error: You should provide at least one number after 'start'."
- exit 1
- elif [ "$#" -eq 2 ]; then
- # If there are only two parameters, try starting a container
- start_container "$2"
- elif [ "$#" -eq 3 ]; then
- # If you have three parameters, try starting a series of containers
- 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
- echo "Error: Both numbers must be integers between 1 and 1000."
- exit 1
- fi
- if [ "$2" -gt "$3" ]; then
- echo "Error: The first number must be less than or equal to the second."
- exit 1
- fi
- start_container "$2" "$3"
- else
- echo "Error: Too many arguments for 'start'."
- exit 1
- fi
- ;;
-
- stop)
- if [ "$#" -lt 2 ]; then
- echo "Error: You should provide at least one number after 'stop'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
- echo "Error: The number(s) must be integers between 1 and 1000."
- exit 1
- fi
-
- if [ "$#" -eq 2 ]; then
- stop_container "$2"
- elif [ "$#" -eq 3 ]; then
- if ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
- echo "Error: Both numbers must be integers between 1 and 1000."
- exit 1
- fi
- if [ "$2" -gt "$3" ]; then
- echo "Error: The second number must be greater than or equal to the first."
- exit 1
- fi
- stop_container "$2" "$3"
- else
- echo "Error: Too many arguments for 'stop'."
- exit 1
- fi
- ;;
-
- exec)
- if [ "$#" -ne 2 ]; then
- echo "Error: You should provide exactly one number after 'exec'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
- echo "Error: The number must be an integer between 1 and 1000."
- exit 1
- fi
-
- exec_container "$2"
- ;;
-
- remove)
- if [ "$#" -ne 2 ]; then
- echo "Error: You should provide exactly one number after 'remove'."
- exit 1
- fi
-
- if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
- echo "Error: The number must be an integer between 1 and 1000."
- exit 1
- fi
-
- remove_container "$2"
- ;;
- info)
- if [ "$#" -ne 2 ]; then
- echo "Usage: $0 info
" - exit 1
- fi
- info_container "$2"
- ;;
-
- logs|status)
- echo "Function '$1' has not been updated to handle numbered containers."
- exit 1
- ;;
- *)
- echo "Invalid command. Use './docker_operations.sh help' to get instructions."
- exit 1
- ;;
- esac
-
- exit 0