Post

Building a Containerized Security Testing Lab with Docker

A practical approach to building portable, reproducible, and isolated security testing environments using Docker. Suitable for research, professional assessments, and controlled lab work.

Building a Containerized Security Testing Lab with Docker

Modern security assessments benefit from environments that are portable, reproducible, and isolated from the host operating system.

Containers provide a lightweight method for packaging tooling, dependencies, and configurations into disposable environments that can be recreated consistently across research tasks, internal testing, and client engagements.

This guide outlines a simple workflow for building and managing a containerized security testing environment using Docker.

“Containerization enables controlled, reproducible, and isolated environments for security testing.”


1. Install Docker

1
2
3
4
5
sudo apt update
sudo apt install docker.io -y
sudo usermod -aG docker $USER
sudo systemctl start docker
sudo systemctl enable docker

Test it:

1
2
docker version
docker run hello-world

You may need to reboot for the user group to take effect.


2. Run a Kali Linux Container

1
docker pull kalilinux/kali-rolling

Interactive session with full power:

1
docker run -it --net=host --privileged --name kali_lab kalilinux/kali-rolling /bin/bash

Flags Breakdown

  • --net=host: Container shares host’s network (needed for tools like Bettercap, Nmap, etc.)
  • --privileged: Grants access to host devices like wlan0
  • --name: Set a name for easier container management

This is your hacker lab. Full access. No compromises.


3. Install Tools Inside the Container

Once inside Kali:

1
apt update && apt install -y metasploit-framework nmap bettercap net-tools netexec john hashcat sqlmap feroxbuster seclists exploitdb git python3-pip

Add anything else you need. It’s your custom lab.


4. Managing Docker Containers

ActionCommand
List runningdocker ps
List alldocker ps -a
Start containerdocker start kali_lab
Attach + Outputdocker start -ai kali_lab
Exec new shelldocker exec -it kali_lab /bin/bash
Stop containerdocker stop kali_lab
Remove containerdocker rm kali_lab
Remove imagedocker rmi kalilinux/kali-rolling

Why docker start -ai?

This command starts the container and immediately attaches to its interactive output, meaning you land directly inside it like when you ran it the first time.

Think of it like “reviving your lab exactly where you left off.”


5. Saving Your Hacking Environment

By default, changes stay in the container. But if you delete it, they’re gone.

To save your work permanently:

1
docker commit kali_lab mykali/hacked

Export for backup:

1
docker save mykali/hacked -o mykali_backup.tar

Later, restore it:

1
docker load -i mykali_backup.tar

Always commit after major installs or configuration changes.


6. Networking for Hackers

If you used --net=host:

  • Tools like nmap, bettercap, and tcpdump work like on your host.
  • Wireless interfaces, raw packets, MITM tools all functional.

If you don’t, you’re isolated on a virtual bridge and must expose ports manually with -p.

For real-world testing, use --net=host. For isolated labs, bridges are fine.


7. Useful One-Liners for Practice Labs

1
2
3
4
5
docker run -it --rm vulnerables/web-dvwa
docker pull tleemcjr/metasploitable2
docker run -it tleemcjr/metasploitable2
docker run -d -p 3000:3000 bkimminich/juice-shop
docker search vuln

These are perfect for training and testing in your containerized lab.


8. Operational Practices

  • Use --privileged when doing hardware or packet-level work
  • Use --net=host to avoid painful port mapping issues
  • Use docker commit after setup, tool installs, or tweaks
  • Use docker save/load to back up and move environments
  • Avoid bloating your host, keep the dirty work in the container

Treat your container like a disposable VM with snapshot superpowers.


Quick Summary Table

ActionCommand
Run Kalidocker run -it --net=host --privileged --name kali_lab kalilinux/kali-rolling /bin/bash
Save Progressdocker commit kali_lab mykali/hacked
Backup Imagedocker save mykali/hacked -o mykali_backup.tar
Restore Imagedocker load -i mykali_backup.tar
Attach with Outputdocker start -ai kali_lab
Exec Shelldocker exec -it kali_lab /bin/bash
Stop Containerdocker stop kali_lab
Start Containerdocker start kali_lab

Final Word

Containerized environments reduce host contamination, simplify setup time, and ensure consistent results across assessments.

Build once, document clearly, and recreate reliably.

This post is licensed under CC BY 4.0 by the author.