diff --git a/.drone.yml b/.drone.yml index 04ba0de..2ec66f8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -65,27 +65,3 @@ steps: - type=local\\,src=/cache/${DRONE_REPO}/ubuntu-20.04 cache_to: - type=local,dest=/cache/${DRONE_REPO}/ubuntu-20.04,mode=max - - - name: ubuntu-18 - image: *image - depends_on: *depends - environment: - UBUNTU_VER: 18.04 - settings: - <<: *settings - repo: ${DRONE_REPO_LINK:8}/ubuntu-18.04 - extra_tags: - - ${DRONE_REPO_LINK:8}:18.04 - cache_from: - - type=local\\,src=/cache/${DRONE_REPO}/ubuntu-18.04 - cache_to: - - type=local,dest=/cache/${DRONE_REPO}/ubuntu-18.04,mode=max - - # - name: send_n8n - # image: curlimages/curl:latest - # depends_on: - # - ubuntu-22 - # - ubuntu-20 - # - ubuntu-18 - # commands: - # - curl https://n8n.ahkhan.me/webhook/drone/gitea/docker/ubuntu -H 'Authorization:Bearer wJlK5lDvTUS03Cfd4RWKyQ' diff --git a/Dockerfile b/Dockerfile index f5f1122..8032a9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,40 +2,60 @@ ARG UBUNTU_VER FROM ubuntu:${UBUNTU_VER} -ARG TARGETPLATFORM - ENV DEBIAN_FRONTEND=noninteractive -# Enable autocompletion -RUN rm /etc/apt/apt.conf.d/docker-* -RUN echo "if [ -f /etc/bash_completion ] && ! shopt -oq posix; then \ - . /etc/bash_completion; \ - fi" >> /root/.bashrc +# Enable autocompletion and install packages in optimized layers +RUN rm /etc/apt/apt.conf.d/docker-* && \ + echo 'if [ -f /etc/bash_completion ] && ! shopt -oq posix; then . /etc/bash_completion; fi' >> /root/.bashrc -RUN apt update +RUN apt update && apt install -y \ + # Basic utilities + curl wget nano lsb-release sudo bash-completion jq git screen cron \ + # Basic networking tools + net-tools iputils-ping iproute2 iptables dnsutils \ + # Network diagnostic and probing tools + tcpdump traceroute netcat-openbsd nmap telnet whois mtr-tiny socat \ + # Network performance tools + iperf3 speedtest-cli \ + # Network configuration utilities + resolvconf bridge-utils vlan \ + # VPN and tunneling tools + wireguard-tools openvpn stunnel4 \ + # Packet analysis tools + tshark \ + # Programming languages and runtimes + python3 python3-pip python3-venv python3-dev \ + # Development tools + software-properties-common build-essential \ + # Remote access + openssh-server \ + # Localization + locales + # Note: Keeping apt cache for dev convenience -RUN apt install -y curl wget nano lsb-release nano sudo bash-completion jq git screen cron +# Install Node.js using official NodeSource repository +RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - && \ + apt-get install -y nodejs -RUN apt install -y net-tools iputils-ping tcpdump traceroute iproute2 iptables iperf3 dnsutils speedtest-cli - -RUN apt install -y software-properties-common - -RUN apt install -y openssh-server - -# Installing locales -RUN apt install -y locales +# Configure locales ENV LC_ALL=en_US.UTF-8 ENV LANG=en_US.UTF-8 RUN locale-gen en_US.UTF-8 -# Allow non-root users to run sudo command without password. +# Configure SSH +RUN mkdir -p /var/run/sshd + +# Allow non-root users to run sudo command without password RUN echo "ALL ALL=NOPASSWD:ALL" > /etc/sudoers.d/01-allow-sudo COPY scripts/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh ENV LOG_FILE=/dev/null # Reset DEBIAN_FRONTEND ENV DEBIAN_FRONTEND="" -CMD [ "/entrypoint.sh" ] +EXPOSE 22 + +CMD ["/entrypoint.sh"] diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 7064f7b..4e3091c 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,33 +1,78 @@ #!/usr/bin/env bash set -x -term_handler() { - eval $USER_COMMAND_EXIT - eval $SYS_COMMAND_EXIT +# Gracefully update package lists if network is available +echo "Checking network connectivity..." +if ping -c 1 -W 5 8.8.8.8 >/dev/null 2>&1 || ping -c 1 -W 5 1.1.1.1 >/dev/null 2>&1; then + echo "Network detected, refreshing package lists..." + if sudo apt update; then + echo "Package lists updated successfully" + else + echo "Warning: apt update failed despite network connectivity" + echo "Falling back to cached package lists" + fi +else + echo "No network connectivity detected, using cached package lists" +fi - exit 143; # 128 + 15 -- SIGTERM +# Global variable to track background processes +TAIL_PID="" + +term_handler() { + echo "Received termination signal, cleaning up..." + + # Kill the tail process if it's running + if [[ -n "$TAIL_PID" ]]; then + kill "$TAIL_PID" 2>/dev/null + fi + + # Run user-defined exit commands + eval "$COMMAND_EXIT" + + echo "Cleanup completed, exiting..." + exit 0 } -# setup handler when the container is exited -trap 'kill ${!}; term_handler' SIGTERM +# Setup signal handlers for graceful shutdown +trap 'term_handler' SIGTERM SIGINT # setup home directory for the current user. It is useful for attaching vscode with container. user_name=$(whoami) user_home="/home/$user_name" -sudo mkdir -p $user_home -sudo chown -R $(id -u):$(id -g) $user_home -cp -r /etc/skel/. $user_home -if [[ $LOG_FILE != "/dev/null" ]]; -then - sudo touch $LOG_FILE - sudo chown -R $(id -u):$(id -g) $LOG_FILE +# Only create home directory if it doesn't exist (handles mounted /etc/passwd case) +if [[ ! -d "$user_home" ]]; then + sudo mkdir -p "$user_home" + sudo chown -R "$(id -u):$(id -g)" "$user_home" + # Copy skeleton files only if home directory was created + cp -r /etc/skel/. "$user_home" 2>/dev/null || true fi +if [[ $LOG_FILE != "/dev/null" ]]; then + sudo touch "$LOG_FILE" + sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE" +fi + +echo "Starting SSH service..." sudo service ssh start -eval $USER_COMMAND_INIT -eval $SYS_COMMAND_INIT -eval $USER_COMMAND_SETUP +# Run initialization commands +eval "$COMMAND_INIT" -tail -f $LOG_FILE & wait ${!} +# Start the main process loop +if [[ $LOG_FILE == "/dev/null" ]]; then + # If no log file, just wait for signals + echo "Container ready, waiting for signals..." + while true; do + sleep 1 & + wait $! + done +else + # If log file specified, tail it + echo "Container ready, tailing log file: $LOG_FILE" + tail -f "$LOG_FILE" & + TAIL_PID=$! + + # Wait for the tail process or signals + wait $TAIL_PID +fi