forked from tandrup/docker-snmpsim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-run.sh
More file actions
executable file
·106 lines (91 loc) · 3.06 KB
/
Copy pathlocal-run.sh
File metadata and controls
executable file
·106 lines (91 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
# local-run.sh - run the snmpsim container locally for development/testing
#
# Usage:
# ./local-run.sh [options] -- [docker run extra args]
#
# Options:
# -t, --tag TAG Image tag to run (default: local/snmpsim:latest)
# -n, --name NAME Container name (default: snmpsim-local)
# -p, --port HOST:CONTAINER
# Port mapping; can be provided multiple times
# (default: 1161:1161/udp 1162:1162/udp for SNMP/Traps)
# -v, --volume HOST:CONTAINER
# Bind mount a host path into the container; may repeat
# -d, --detach Run container in background (detached)
# -i, --interactive Run container interactively (overrides -d)
# -r, --remove Remove container after exit (uses --rm)
# -h, --help Show this help
#
# Examples:
# ./local-run.sh -t local/snmpsim:latest
# ./local-run.sh -t local/snmpsim:latest -n my-snmpsim -p 1161:1161/udp -p 1162:1162/udp -d
# ./local-run.sh -v $(pwd)/data:/data -t local/snmpsim:latest
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
DEFAULT_TAG="local/snmpsim:latest"
TAG="$DEFAULT_TAG"
NAME="snmpsim-local"
DETACH="false"
REMOVE="true"
INTERACTIVE="false"
PORT_ARGS=()
VOLUME_ARGS=()
EXTRA_DOCKER_ARGS=()
print_help() {
sed -n '1,200p' "$0" | sed -n '1,120p'
}
while [[ $# -gt 0 ]]; do
case "$1" in
-t|--tag)
TAG="$2"; shift 2;;
-n|--name)
NAME="$2"; shift 2;;
-p|--port)
PORT_ARGS+=("-p" "$2"); shift 2;;
-v|--volume)
VOLUME_ARGS+=("-v" "$2"); shift 2;;
-d|--detach)
DETACH="true"; shift;;
-i|--interactive)
INTERACTIVE="true"; shift;;
-r|--remove)
REMOVE="true"; shift;;
-h|--help)
print_help; exit 0;;
--)
shift; EXTRA_DOCKER_ARGS+=("$@"); break;;
-*) echo "Unknown option: $1" >&2; print_help; exit 1;;
*) EXTRA_DOCKER_ARGS+=("$1"); shift;;
esac
done
# Default UDP port mappings commonly used by SNMP simulator
if [[ ${#PORT_ARGS[@]} -eq 0 ]]; then
PORT_ARGS+=("-p" "161:161/udp")
PORT_ARGS+=("-p" "162:162/udp")
fi
DOCKER_RUN=(docker run --name "$NAME")
if [[ "$REMOVE" == "true" ]]; then
DOCKER_RUN+=(--rm)
fi
if [[ "$INTERACTIVE" == "true" ]]; then
DOCKER_RUN+=(-it)
elif [[ "$DETACH" == "true" ]]; then
DOCKER_RUN+=(-d)
fi
# Append port and volume args
DOCKER_RUN+=("${PORT_ARGS[@]}" "${VOLUME_ARGS[@]}")
# Default to showing container logs/attach behavior when not detached
DOCKER_RUN+=("$TAG")
echo "Running container from image: $TAG"
echo "Container name: $NAME"
echo "Command: ${DOCKER_RUN[*]} ${EXTRA_DOCKER_ARGS[*]}"
if [[ "$DETACH" == "true" ]]; then
"${DOCKER_RUN[@]}" "${EXTRA_DOCKER_ARGS[@]}"
echo "Container started (detached). Use: docker ps --filter name=$NAME"
echo "To view logs: docker logs -f $NAME"
else
# Run in foreground so user sees output; allow additional args
"${DOCKER_RUN[@]}" "${EXTRA_DOCKER_ARGS[@]}"
fi
exit 0