From 20680719f54b5e32bc1b4c7f845178809402b0f8 Mon Sep 17 00:00:00 2001 From: Arnab-Afk Date: Fri, 15 Aug 2025 14:09:36 +0530 Subject: [PATCH] Update Dockerfile and docker-compose to enhance cloudflared configuration and startup process --- new-backend/Dockerfile.tunnel | 2 +- new-backend/Dockerfile.tunnel.new | 66 +++++++++++++++++++++++++++ new-backend/config.json.new | 13 ++++++ new-backend/docker-compose.tunnel.yml | 3 ++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 new-backend/Dockerfile.tunnel.new create mode 100644 new-backend/config.json.new diff --git a/new-backend/Dockerfile.tunnel b/new-backend/Dockerfile.tunnel index 1d0a252..2f12a16 100644 --- a/new-backend/Dockerfile.tunnel +++ b/new-backend/Dockerfile.tunnel @@ -55,7 +55,7 @@ RUN echo "[supervisord]" > /etc/supervisor/conf.d/supervisord.conf && \ echo "stderr_logfile_maxbytes=0" >> /etc/supervisor/conf.d/supervisord.conf && \ echo "" >> /etc/supervisor/conf.d/supervisord.conf && \ echo "[program:cloudflared]" >> /etc/supervisor/conf.d/supervisord.conf && \ - echo "command=cloudflared tunnel --no-autoupdate run --config /etc/cloudflared/config.json" >> /etc/supervisor/conf.d/supervisord.conf && \ + echo "command=cloudflared tunnel --no-autoupdate --config /etc/cloudflared/config.json run" >> /etc/supervisor/conf.d/supervisord.conf && \ echo "autostart=true" >> /etc/supervisor/conf.d/supervisord.conf && \ echo "autorestart=true" >> /etc/supervisor/conf.d/supervisord.conf && \ echo "stdout_logfile=/dev/stdout" >> /etc/supervisor/conf.d/supervisord.conf && \ diff --git a/new-backend/Dockerfile.tunnel.new b/new-backend/Dockerfile.tunnel.new new file mode 100644 index 0000000..f9a7aee --- /dev/null +++ b/new-backend/Dockerfile.tunnel.new @@ -0,0 +1,66 @@ +FROM golang:1.19-alpine AS builder + +# Install git and required dependencies +RUN apk update && apk add --no-cache git + +# Set working directory +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum* ./ + +# Download dependencies +RUN go mod download + +# Copy source code +COPY . . + +# Build the application with optimizations +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o monaco-backend . + +# Final stage +FROM alpine:latest + +# Install Docker client and tools +RUN apk update && apk add --no-cache docker-cli bash wget + +# Get cloudflared directly +RUN wget -O cloudflared https://github.com/cloudflare/cloudflared/releases/download/2023.7.3/cloudflared-linux-amd64 && \ + chmod +x cloudflared && \ + mv cloudflared /usr/local/bin/ + +# Create directories for cloudflared +RUN mkdir -p /etc/cloudflared + +# Copy the certificate file and config +COPY cert.pem /etc/cloudflared/cert.pem +COPY config.json /etc/cloudflared/config.json + +# Copy the binary from builder +COPY --from=builder /app/monaco-backend /monaco-backend + +# Create a simple bash script to run both services +RUN echo '#!/bin/bash\n\ +# Start the backend in the background\n\ +/monaco-backend &\n\ +BACKEND_PID=$!\n\ +echo "Backend started with PID: $BACKEND_PID"\n\ +\n\ +# Wait for backend to start\n\ +echo "Waiting for backend to initialize..."\n\ +sleep 5\n\ +\n\ +# Start cloudflared with proper arguments\n\ +echo "Starting Cloudflare tunnel to api.ishikabhoyar.tech..."\n\ +# Use the specific tunnel name from config.json\n\ +cloudflared tunnel run monaco-backend-tunnel\n\ +\n\ +# If cloudflared exits, kill the backend\n\ +kill $BACKEND_PID\n\ +' > /start.sh && chmod +x /start.sh + +# Expose port for local access +EXPOSE 8080 + +# Run the startup script +CMD ["/start.sh"] diff --git a/new-backend/config.json.new b/new-backend/config.json.new new file mode 100644 index 0000000..7846252 --- /dev/null +++ b/new-backend/config.json.new @@ -0,0 +1,13 @@ +{ + "tunnel": "monaco-backend-tunnel", + "credentials-file": "/etc/cloudflared/cert.pem", + "ingress": [ + { + "hostname": "api.ishikabhoyar.tech", + "service": "http://localhost:8080" + }, + { + "service": "http_status:404" + } + ] +} diff --git a/new-backend/docker-compose.tunnel.yml b/new-backend/docker-compose.tunnel.yml index 8b2c065..45a180c 100644 --- a/new-backend/docker-compose.tunnel.yml +++ b/new-backend/docker-compose.tunnel.yml @@ -16,6 +16,9 @@ services: - DEFAULT_TIMEOUT=30 - SANDBOX_NETWORK_DISABLED=true - SANDBOX_PIDS_LIMIT=50 + # Define cloudflared environment variables + - TUNNEL_ORIGIN_CERT=/etc/cloudflared/cert.pem + - NO_AUTOUPDATE=true # Isolated network to prevent conflicts with host cloudflared networks: - monaco-backend-network