-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
74 lines (64 loc) · 2.91 KB
/
Dockerfile
File metadata and controls
74 lines (64 loc) · 2.91 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
# Stage 1: Build the application
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /source
# Copy the source code into the container
COPY sharpsnmplib-samples/ ./
# Publish both snmpd and snmptrapd projects into separate publish folders
# Target .NET 9 explicitly so only net9.0 binaries are produced for the runtime image
RUN dotnet publish ./Samples/CSharpCore/snmpd/snmpd.csproj -c Release -f net9.0 -o /app/publish/snmpd \
&& dotnet publish ./Samples/CSharpCore/snmptrapd/snmptrapd.csproj -c Release -f net9.0 -o /app/publish/snmptrapd
# Stage 2: Create the runtime image
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS runtime
WORKDIR /app
# Copy the build artifacts from the previous stage (both apps)
COPY --from=build /app/publish .
# Expose both SNMP ports (agent and trap receiver)
EXPOSE 161/udp
EXPOSE 162/udp
# Metadata indicating an image maintainer
LABEL maintainer="support@lextudio.com"
LABEL description="Docker image for running snmpd and snmptrapd (C# SNMP Daemons)"
LABEL version="1.1"
# Default: enable snmptrapd (set SNMPTRAPD_ENABLED=0 or false to disable)
ENV SNMPTRAPD_ENABLED=1
# Create a small start script that launches services and handles signals
RUN printf '%s\n' '#!/bin/bash' \
'set -e' \
'' \
'# Determine whether to start snmptrapd (defaults to enabled). Set SNMPTRAPD_ENABLED=0 or false to disable.' \
'if [ "${SNMPTRAPD_ENABLED:-1}" = "0" ] || [ "${SNMPTRAPD_ENABLED:-1}" = "false" ]; then' \
' echo "snmptrapd disabled by SNMPTRAPD_ENABLED=${SNMPTRAPD_ENABLED}"' \
' START_TRAP=0' \
'else' \
' START_TRAP=1' \
'fi' \
'' \
'if [ "$START_TRAP" -eq 1 ]; then' \
' # Start snmptrapd first (listens on UDP 162)' \
' dotnet /app/snmptrapd/snmptrapd.dll &' \
' SNMPTRAPD_PID=$!' \
'fi' \
'' \
'# Start snmpd (agent) (listens on UDP 161)' \
'dotnet /app/snmpd/snmpd.dll &' \
'SNMPD_PID=$!' \
'' \
'# Signal handler: forward SIGTERM/SIGINT to the running processes' \
'_term() { echo "Stopping services..."; if [ "$START_TRAP" -eq 1 ]; then kill -TERM "$SNMPTRAPD_PID" 2>/dev/null; fi; kill -TERM "$SNMPD_PID" 2>/dev/null; }' \
'trap _term SIGTERM SIGINT' \
'' \
'# Wait for the appropriate processes to exit and then exit' \
'if [ "$START_TRAP" -eq 1 ]; then' \
' wait -n "$SNMPTRAPD_PID" "$SNMPD_PID"' \
' wait "$SNMPTRAPD_PID" 2>/dev/null || true' \
' wait "$SNMPD_PID" 2>/dev/null || true' \
'else' \
' wait "$SNMPD_PID" 2>/dev/null || true' \
'fi' \
> /app/start.sh \
&& chmod +x /app/start.sh
# Set the container entrypoint to the start script
ENTRYPOINT ["/app/start.sh"]
# Healthcheck: ensure both dotnet processes for snmpd and snmptrapd are running
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s \
CMD sh -c 'if [ "${SNMPTRAPD_ENABLED:-1}" = "0" ] || [ "${SNMPTRAPD_ENABLED:-1}" = "false" ]; then pgrep -f snmpd.dll >/dev/null || exit 1; else pgrep -f snmptrapd.dll >/dev/null || exit 1 && pgrep -f snmpd.dll >/dev/null || exit 1; fi'