From ec3e21b8828ad68cac6d0f4a4fe83a32ffda2c79 Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Sun, 29 Dec 2024 16:53:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B1host=E7=BD=91=E7=BB=9C=E6=94=B9?= =?UTF-8?q?=E6=88=90main=E7=BD=91=E7=BB=9C=EF=BC=8C=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=9A=E4=B9=89service=E8=84=9A=E6=9C=AC=E5=92=8Cnginx?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 43 ++++++++++++++++++++++++++----------------- nginx => nginx.sh | 0 service | 17 ----------------- service.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 34 deletions(-) rename nginx => nginx.sh (100%) delete mode 100755 service create mode 100755 service.sh diff --git a/docker-compose.yml b/docker-compose.yml index d8ed072..d7a617c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,19 +1,28 @@ -name: nginx +networks: + main: # 定义一个外部网络 + external: true -services: - nginx: - image: quay.io/wandoubaba517/nginx:1.27 - container_name: nginx - restart: always - volumes: - - ./conf/nginx.conf:/etc/nginx/nginx.conf - - ./conf/conf.d:/etc/nginx/conf.d - - ./conf/certs:/etc/nginx/certs - - ./conf/fastcgi_params:/etc/nginx/fastcgi_params - - ./conf/mime.types:/etc/nginx/mime.types - - ./conf/scgi_params:/etc/nginx/scgi_params - - ./conf/uwsgi_params:/etc/nginx/uwsgi_params - - ./html:/usr/share/nginx/html - working_dir: /usr/share/nginx/html +services: + nginx: + image: quay.io/wandoubaba517/nginx:1.27 + container_name: nginx + restart: always + volumes: + - ./conf/nginx.conf:/etc/nginx/nginx.conf + - ./conf/conf.d:/etc/nginx/conf.d + - ./conf/certs:/etc/nginx/certs + - ./conf/fastcgi_params:/etc/nginx/fastcgi_params + - ./conf/mime.types:/etc/nginx/mime.types + - ./conf/scgi_params:/etc/nginx/scgi_params + - ./conf/uwsgi_params:/etc/nginx/uwsgi_params + - ./html:/usr/share/nginx/html + working_dir: /usr/share/nginx/html stdin_open: true - network_mode: host + ports: + - 80:80 + - 443:443 + networks: + main: + ipv4_address: 192.168.250.250 # 指定容器的 IP 地址 + aliases: + - nginx # 设置别名 \ No newline at end of file diff --git a/nginx b/nginx.sh similarity index 100% rename from nginx rename to nginx.sh diff --git a/service b/service deleted file mode 100755 index c60d819..0000000 --- a/service +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -container_name="nginx" - -if [ "$1" = "start" ]; then - docker compose up -d -elif [ "$1" = "stop" ]; then - docker compose down -elif [ "$1" = "restart" ]; then - docker restart $container_name -elif [ "$1" = "reload" ]; then - docker exec -i $container_name service nginx reload -elif [ "$1" = "status" ]; then - docker exec -i $container_name service nginx status -else - echo "Usage: $0 [start|stop|restart|reload|status]" -fi diff --git a/service.sh b/service.sh new file mode 100755 index 0000000..5f9badc --- /dev/null +++ b/service.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +container_name="nginx" +network_name="main" +subnet="192.168.250.0/24" + +# 检查网络是否存在 +function network_check { + if ! docker network ls | grep -w "$network_name" > /dev/null 2>&1; then + echo "Network $network_name does not exist. Creating it..." + docker network create --subnet="$subnet" "$network_name" + fi +} + +# 启动服务 +function start { + network_check # 检查并创建网络(如有必要) + docker compose up -d # 启动服务 +} + +# 处理脚本参数 +case "$1" in + start) + start # 调用 start 函数 + ;; + stop) + docker compose down # 停止服务 + ;; + restart) + docker compose down # 停止服务 + start # 启动服务 + ;; + reload) + docker exec -i "$container_name" service nginx reload # 重载 Nginx 配置 + ;; + status) + docker exec -i "$container_name" service nginx status # 查看 Nginx 状态 + ;; + *) + echo "Usage: $0 [start|stop|restart|reload|status]" # 打印用法信息 + ;; +esac \ No newline at end of file