Refactor Dockerfile and docker-compose to use supervisord for process management and update cloudflared installation method
This commit is contained in:
@@ -18,21 +18,20 @@ COPY . .
|
|||||||
# Build the application with optimizations
|
# Build the application with optimizations
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o monaco-backend .
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o monaco-backend .
|
||||||
|
|
||||||
# Get cloudflared from official image
|
# Final stage
|
||||||
FROM cloudflare/cloudflared:latest AS cloudflared
|
|
||||||
|
|
||||||
# Use a smaller image for the final container
|
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
# Install Docker client
|
# Install Docker client and supervisor
|
||||||
RUN apk update && apk add --no-cache docker-cli
|
RUN apk update && apk add --no-cache docker-cli supervisor wget
|
||||||
|
|
||||||
|
# Get cloudflared directly from GitHub (more reliable than the tarball)
|
||||||
|
RUN wget -O cloudflared https://github.com/cloudflare/cloudflared/releases/download/2023.5.0/cloudflared-linux-amd64 && \
|
||||||
|
chmod +x cloudflared && \
|
||||||
|
mv cloudflared /usr/local/bin/
|
||||||
|
|
||||||
# Create directories for cloudflared
|
# Create directories for cloudflared
|
||||||
RUN mkdir -p /etc/cloudflared
|
RUN mkdir -p /etc/cloudflared
|
||||||
|
|
||||||
# Copy cloudflared from official image
|
|
||||||
COPY --from=cloudflared /usr/local/bin/cloudflared /usr/local/bin/cloudflared
|
|
||||||
|
|
||||||
# Copy the certificate file and config
|
# Copy the certificate file and config
|
||||||
COPY cert.pem /etc/cloudflared/cert.pem
|
COPY cert.pem /etc/cloudflared/cert.pem
|
||||||
COPY config.json /etc/cloudflared/config.json
|
COPY config.json /etc/cloudflared/config.json
|
||||||
@@ -40,24 +39,32 @@ COPY config.json /etc/cloudflared/config.json
|
|||||||
# Copy the binary from builder
|
# Copy the binary from builder
|
||||||
COPY --from=builder /app/monaco-backend /monaco-backend
|
COPY --from=builder /app/monaco-backend /monaco-backend
|
||||||
|
|
||||||
# Create startup script inside the container to ensure correct line endings
|
# Create supervisord config
|
||||||
RUN printf '#!/bin/sh\\n' > /start.sh && \
|
RUN mkdir -p /etc/supervisor/conf.d/
|
||||||
printf '# Start the backend\\n' >> /start.sh && \
|
RUN echo "[supervisord]" > /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf '/monaco-backend &\\n' >> /start.sh && \
|
echo "nodaemon=true" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'BACKEND_PID=$!\\n' >> /start.sh && \
|
echo "user=root" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'echo "Backend started with PID: $BACKEND_PID"\\n\\n' >> /start.sh && \
|
echo "" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf '# Wait for backend to start\\n' >> /start.sh && \
|
echo "[program:backend]" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'echo "Waiting for backend to initialize..."\\n' >> /start.sh && \
|
echo "command=/monaco-backend" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'sleep 5\\n\\n' >> /start.sh && \
|
echo "autostart=true" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf '# Start cloudflared tunnel using config file\\n' >> /start.sh && \
|
echo "autorestart=true" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'echo "Starting Cloudflare tunnel to api.ishikabhoyar.tech..."\\n' >> /start.sh && \
|
echo "stdout_logfile=/dev/stdout" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'cloudflared tunnel --no-autoupdate run --config /etc/cloudflared/config.json\\n\\n' >> /start.sh && \
|
echo "stdout_logfile_maxbytes=0" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf '# If cloudflared exits, kill the backend too\\n' >> /start.sh && \
|
echo "stderr_logfile=/dev/stderr" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
printf 'kill $BACKEND_PID\\n' >> /start.sh && \
|
echo "stderr_logfile_maxbytes=0" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
chmod +x /start.sh
|
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 "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 && \
|
||||||
|
echo "stdout_logfile_maxbytes=0" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
|
echo "stderr_logfile=/dev/stderr" >> /etc/supervisor/conf.d/supervisord.conf && \
|
||||||
|
echo "stderr_logfile_maxbytes=0" >> /etc/supervisor/conf.d/supervisord.conf
|
||||||
|
|
||||||
# Expose port for local access
|
# Expose port for local access
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# Run the startup script
|
# Use supervisord to manage processes
|
||||||
ENTRYPOINT ["/start.sh"]
|
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
backend:
|
backend:
|
||||||
build:
|
build:
|
||||||
@@ -7,7 +5,7 @@ services:
|
|||||||
dockerfile: Dockerfile.tunnel
|
dockerfile: Dockerfile.tunnel
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- //var/run/docker.sock:/var/run/docker.sock
|
||||||
# Port is only exposed locally, traffic comes through the tunnel
|
# Port is only exposed locally, traffic comes through the tunnel
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:8080:8080"
|
- "127.0.0.1:8080:8080"
|
||||||
|
|||||||
Reference in New Issue
Block a user