Comprehensive Guide: Setting Up Secure Docker-Based Development Environments with SSH Access and Optional OpenVPN on Debian Server
This is a detailed step-by-step guide for each of the points from 1 to 8, covering the installation of Docker, creating a Docker image, setting up Docker containers, and generating OpenVPN configuration. Let's get started:
Step 1: Install Docker on Debian Server
-
Update the package repository on your Debian server:
sudo apt update
-
Install Docker and related dependencies:
sudo apt install docker.io
-
Start the Docker service:
sudo systemctl start docker
-
Enable Docker to start at boot:
sudo systemctl enable docker
Step 2: Create a Docker Image for the Development Environment
-
Create a Dockerfile for the development environment on your Debian server. Use a text editor to create a file named
Dockerfile.dev
:sudo nano Dockerfile.dev
-
Inside the
Dockerfile.dev
, paste the following content (customize as needed):FROM ubuntu:20.04 # Install SSH server and necessary tools RUN apt-get update && \ apt-get install -y openssh-server sudo && \ rm -rf /var/lib/apt/lists/* && \ mkdir /var/run/sshd # Set up a user (replace "developer" with the desired username) RUN useradd -m -d /home/developer -s /bin/bash developer && \ echo 'developer:password' | chpasswd && \ echo 'developer ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers # Expose SSH port EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]
-
Save the
Dockerfile.dev
and exit the text editor. -
Build the Docker image (replace
my-dev-image
with your desired image name):sudo docker build -t my-dev-image -f Dockerfile.dev .
Step 3: Create a Docker Container for SSH Access
-
For each user, create a Docker container based on the image you built. Replace
user1-container
anduser1-key.pub
with the desired container name and the path to the user's public SSH key:sudo docker run -d -p 2222:22 --name user1-container my-dev-image sudo docker cp /path/to/user1-key.pub user1-container:/home/developer/.ssh/authorized_keys
Repeat this step for each user you want to grant SSH access.
Step 4: Generate OpenVPN Configuration (Optional)
- If needed, install and configure OpenVPN on your Debian server. Refer to OpenVPN documentation for detailed instructions.
Step 5: Set Up User Access
-
Generate OpenVPN client profiles and securely distribute them to users.
-
Instruct users to install an OpenVPN client on their local machines and import their client profiles.
-
Users can connect to the OpenVPN server to establish a secure connection.
Step 6: Secure SSH Access (Optional)
-
Inside each Docker container, secure SSH access:
-
Edit the SSH configuration file
/etc/ssh/sshd_config
inside the container to disable password authentication (PasswordAuthentication no
) and use SSH keys for authentication. -
Restart the SSH service inside the container:
sudo service ssh restart
-
Step 7: Network Configuration and Security
- Implement firewall rules on your server to restrict access to the OpenVPN server and Docker containers' SSH ports from trusted IP addresses only. Use a tool like
ufw
or configureiptables
as needed.
Step 8: Data Persistence (Optional)
- To persist user data and configurations beyond the container's lifecycle, consider using Docker volumes or bind mounts. This ensures that data remains intact even if containers are recreated.
With these detailed steps, you've set up a Docker-based development environment with SSH access, addressed security concerns, and optionally added OpenVPN for secure connections. Customize the setup according to your specific requirements and security policies.