FROM alpine:3.19

# Install required packages
RUN apk add --no-cache \
    dnsmasq \
    iptables \
    ip6tables \
    supervisor \
    bash \
    curl \
    bind-tools \
    iperf \
    tcpdump \
    python3 \
    py3-pip

# Install Python dependencies for metadata server
RUN pip3 install --no-cache-dir --break-system-packages flask requests requests-unixsocket

# Create directories
RUN mkdir -p /etc/dnsmasq.d /var/log/supervisor /var/run

# Copy configuration files
COPY supervisord.conf /etc/supervisord.conf
COPY dnsmasq.conf /etc/dnsmasq.conf
COPY entrypoint.sh /entrypoint.sh
COPY backend/metadata-proxy.py /metadata_proxy.py

# Make entrypoint executable
RUN chmod +x /entrypoint.sh

# Expose DNS ports and metadata HTTP port
EXPOSE 53/udp 53/tcp 80/tcp

# Copy health check script
COPY healthcheck.sh /healthcheck.sh
RUN chmod +x /healthcheck.sh

# Health check - minimal check for dnsmasq process
# Fast checks to quickly recognize when container is healthy
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=3 \
    CMD /healthcheck.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]