Skip to content

Instantly share code, notes, and snippets.

@anatawa12
Last active November 11, 2025 19:04
Show Gist options
  • Select an option

  • Save anatawa12/271001a500730fdcaf99f474a8452cc7 to your computer and use it in GitHub Desktop.

Select an option

Save anatawa12/271001a500730fdcaf99f474a8452cc7 to your computer and use it in GitHub Desktop.
simple command to manage https://github.com/nginx-proxy/nginx-proxy docker instance
#!/bin/sh
# nginx-proxy-manager: Simple script to manage nginx-proxy container and its networks.
# Requires Docker to be installed and running.
#
# Usage: nginx-proxy-manager <command> [options] [arguments]
# Commands:
# start Start or restart the nginx-proxy container.
# stop Stop the nginx-proxy container.
# pull Pull the latest nginx-proxy image.
# logs [docker logs opts] Show the logs of the nginx-proxy container.
# show-config Show the current nginx configuration.
# status Show the status of the nginx-proxy container.
# add-network <name> Create a new Docker network with the specified name.
# remove-network <name> Remove the Docker network with the specified name.
# list-networks List all Docker networks associated with nginx-proxy.
#
# This script is distributed at https://gist.github.com/anatawa12/271001a500730fdcaf99f474a8452cc7 under MIT License.
# To install or update, run:
# curl -L -o nginx-proxy-manager https://gist.github.com/anatawa12/271001a500730fdcaf99f474a8452cc7/raw/nginx-proxy-manager
# chmod +x nginx-proxy-manager
#
# MIT License
#
# Copyright (c) 2025 anatawa12
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Configuration
# Configuration are defined in .env file silting to this script.
# Variables
# CONTAINER_NAME: Name of the nginx-proxy container (default: nginx-proxy)
# NGINX_IMAGE: Docker image for nginx-proxy (default: nginxproxy/nginx-proxy:latest)
# HTTP_PORT: Port for HTTP traffic (default: 80)
# HTTPS_PORT: Port for HTTPS traffic (default: 443)
# CERTS_DIR: Directory for SSL certificates (default: ./certs)
# Note that CONTAINER_NAME must not include regex special characters.
BASE_DIR="$(cd "$(dirname "$0")" || exit 1; pwd)"
. "${BASE_DIR}/.env"
CONTAINER_NAME="${CONTAINER_NAME:-nginx-proxy}"
NGINX_IMAGE="${NGINX_IMAGE:-nginxproxy/nginx-proxy:latest}"
HTTP_PORT="${HTTP_PORT:-80}"
HTTPS_PORT="${HTTPS_PORT:-443}"
CERTS_DIR="${CERTS_DIR:-./certs}"
# utility functions
# docker inspect --type container anatawa12-server-nginx-proxy 2>/dev/null | jq '. | length'
is_container_running() {
[ -n "$(docker ps --all --quiet -f name="^/${CONTAINER_NAME}$")" ]
}
reload_nginx_proxy() {
# nginx-proxy regenerates its configuration automatically when new container are started/stopped
# therefore, we can trigger a reload by creating dummy container
docker run --rm "$NGINX_IMAGE" sh
}
# command
help_exit() {
echo "Usage: $0 <command> [options] [arguments]" >&2
echo "Commands:" >&2
echo " start Start or restart the nginx-proxy container." >&2
echo " stop Stop the nginx-proxy container." >&2
echo " pull Pull the latest nginx-proxy image." >&2
echo " logs [docker logs opts] Show the logs of the nginx-proxy container." >&2
echo " show-config Show the current nginx configuration." >&2
echo " status Show the status of the nginx-proxy container." >&2
echo " add-network <name> Create a new Docker network with the specified name." >&2
echo " remove-network <name> Remove the Docker network with the specified name." >&2
echo " list-networks List all Docker networks associated with nginx-proxy." >&2
echo " help Show this help message." >&2
exit 0
}
start_container() {
if is_container_running; then
echo "Removing existing container: $CONTAINER_NAME"
networks="$(docker inspect anatawa12-server-nginx-proxy -f '{{range .NetworkSettings.Networks}}{{.NetworkID}} {{end}}')"
docker rm -f "$CONTAINER_NAME"
fi
# resolve CERTS_DIR
CERTS_DIR="$(cd "$BASE_DIR" || exit 1; cd "$CERTS_DIR" || exit 1; pwd)"
echo "Creating new container: $CONTAINER_NAME"
docker container create \
--name "$CONTAINER_NAME" \
--publish "$HTTP_PORT:80" \
--publish "$HTTPS_PORT:443" \
--mount "type=bind,src=/var/run/docker.sock,dst=/tmp/docker.sock,readonly" \
--mount "type=bind,src=${CERTS_DIR},dst=/etc/nginx/certs,readonly" \
--restart unless-stopped \
"$NGINX_IMAGE"
# connect to previsously connected networks
for network in $networks; do
docker network connect "$network" "$CONTAINER_NAME"
done
# start new container
docker start "$CONTAINER_NAME"
}
stop_container() {
echo "Stopping container: $CONTAINER_NAME"
docker stop "$CONTAINER_NAME"
}
pull_image() {
echo "Pulling latest image: $NGINX_IMAGE"
docker pull "$NGINX_IMAGE"
}
logs_container() {
docker logs "$CONTAINER_NAME" "$@"
}
show_config() {
docker exec "$CONTAINER_NAME" cat /etc/nginx/conf.d/default.conf
}
status_container() {
if is_container_running; then
docker ps -f name="^/${CONTAINER_NAME}$"
else
echo "Container $CONTAINER_NAME does not exist."
fi
}
add_network() {
NETWORK_NAME="$1"
if [ -z "$NETWORK_NAME" ]; then
echo "Error: Network name is required." >&2
exit 1
fi
echo "Connecting container $CONTAINER_NAME to network $NETWORK_NAME"
docker network connect "$NETWORK_NAME" "$CONTAINER_NAME"
reload_nginx_proxy
}
remove_network() {
NETWORK_NAME="$1"
if [ -z "$NETWORK_NAME" ]; then
echo "Error: Network name is required." >&2
exit 1
fi
echo "Disconnecting container $CONTAINER_NAME from network $NETWORK_NAME"
docker network disconnect "$NETWORK_NAME" "$CONTAINER_NAME"
reload_nginx_proxy
}
list_networks() {
echo "NETWORK ID NAME"
docker inspect --format='{{range $name, $network := .NetworkSettings.Networks}}{{ printf "%.12s %s\n" $network.NetworkID $name }}{{end}}' "$CONTAINER_NAME"
}
main() {
COMMAND="$1"
${1+shift} # shift if $1 is set
case "$COMMAND" in
start)
start_container "$@"
;;
stop)
stop_container "$@"
;;
pull)
pull_image "$@"
;;
logs)
logs_container "$@"
;;
status)
status_container "$@"
;;
show-config)
show_config "$@"
;;
add-network)
add_network "$@"
;;
remove-network)
remove_network "$@"
;;
list-networks)
list_networks "$@"
;;
help|--help|-h|"")
help_exit
;;
*)
echo "Error: Unknown command: $COMMAND" >&2
help_exit
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment