Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

Create Dockerfile with sshd inside and blocking acces with ufw

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-mini-2025-04-14’s answer.
  1. User

    Create Dockerfile with sshd inside and blocking acces with ufw

  2. Qwen Assistant

    Here's a Dockerfile that sets up an Ubuntu-based container with:

    • openssh-server (sshd)
    • A non-root user for SSH access
    • ufw configured to:
      • Allow only SSH on port 22
      • Deny all other incoming traffic

    ✅ Prerequisites

    • You should provide an SSH public key to allow login via SSH.
    • The Docker container must be run in privileged mode or at least with --cap-add=NET_ADMIN to allow ufw to function properly.

    🐳 Dockerfile

    # Use the latest Ubuntu image
    FROM ubuntu:latest
    
    # Set environment variables to avoid interactive prompts during package installation
    ENV DEBIAN_FRONTEND=noninteractive
    
    # Install necessary packages
    RUN apt-get update && \
        apt-get install -y openssh-server ufw curl && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*
    
    # Create a non-root user (replace 'myuser' and 'mygroup' as needed)
    ARG USERNAME=myuser
    ARG GROUPNAME=mygroup
    RUN groupadd ${GROUPNAME} && \
        useradd -m -s /bin/bash -g ${GROUPNAME} ${USERNAME} && \
        mkdir -p /home/${USERNAME}/.ssh && \
        chmod 700 /home/${USERNAME}/.ssh
    
    # Add your public SSH key here (replace id_rsa.pub with your actual public key)
    COPY id_rsa.pub /tmp/id_rsa.pub
    RUN cat /tmp/id_rsa.pub >> /home/${USERNAME}/.ssh/authorized_keys && \
        chmod 600 /home/${USERNAME}/.ssh/authorized_keys && \
        chown -R ${USERNAME}:${GROUPNAME} /home/${USERNAME}/.ssh
    
    # Set password for the user (optional, you can remove if using keys only)
    RUN echo "${USERNAME}:password" | chpasswd
    
    # Configure SSH
    RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config && \
        sed -i 's/PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
        sed -i 's/UsePAM yes/UsePAM no/' /etc/ssh/sshd_config && \
        echo "AllowUsers ${USERNAME}" >> /etc/ssh/sshd_config
    
    # Expose SSH port
    EXPOSE 22
    
    # Setup UFW firewall rules
    COPY ufw_setup.sh /usr/local/bin/ufw_setup.sh
    RUN chmod +x /usr/local/bin/ufw_setup.sh
    
    # Start services
    CMD ["/usr/local/bin/ufw_setup.sh"]
    

    ⚙️ ufw_setup.sh

    Create this script next to your Dockerfile:

    #!/bin/bash
    
    # Start SSH service
    service ssh start
    
    # Enable UFW firewall
    echo "Configuring UFW..."
    ufw --force reset
    ufw default deny incoming
    ufw default allow outgoing
    ufw allow 22/tcp
    ufw --force enable
    
    # Keep container running
    tail -f /dev/null
    

    🧪 Build and Run

    Build the image

    docker build -t sshd-ufw .
    

    Run the container (important flags for ufw)

    docker run -d \
      --name sshd-container \
      --cap-add=NET_ADMIN \
      -p 2222:22 \
      sshd-ufw
    

    You can now SSH into the container using:

    ssh myuser@localhost -p 2222
    

    🔒 Security Notes

    • Only allow SSH access using public keys if possible.
    • Avoid exposing unnecessary ports.
    • Consider limiting root capabilities further.
    • Regularly update base images and installed packages.

    Let me know if you'd like to add persistent storage, use Alpine Linux instead, or integrate with Docker Compose!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.