Create Dockerfile with sshd inside and blocking acces with ufw
- User
Create Dockerfile with sshd inside and blocking acces with ufw
Assistant
Here's a Dockerfile that sets up an Ubuntu-based container with:
openssh-server(sshd)- A non-root user for SSH access
ufwconfigured 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_ADMINto allowufwto 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-ufwYou 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!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.