Setting Up a CTF Lab: Docker and VM part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
Docker containers for pentesting
Why use Docker if you already have a virtual machine?

A virtual machine is a fully-fledged OS with its own kernel, bootloader, and system services. Metasploitable 2 takes up ~6 GB of disk space and consumes 512 MB of RAM even when idle. A Docker container is an isolated process that uses the host kernel. DVWA in Docker: ~200 MB of disk space, startup in 3-5 seconds instead of a minute.

In practice, this means that on a laptop with 8 GB of RAM, you can run Kali as a VM and simultaneously run three to five vulnerable services in Docker. Trying to run all of these as separate VMs will result in a swap-hell on the second service. I've verified this myself—don't repeat it.

The Docker CTF lab is built from pre-built images: images for most vulnerable applications are already available on Docker Hub and in the Vulhub project.
Vulnerable images for Docker training

DVWA (Damn Vulnerable Web Application) is a standard web pentest simulator. Launch it with a single command: docker run -d -p 127.0.0.1:8080:80 vulnerables/web-dvwa. You open http://127.0.0.1:8080in your browser, go through the initial database setup, and get an environment with SQL injections, XSS, CSRF, and a dozen other vulnerabilities from the OWASP Top 10 .

Binding to 127.0.0.1instead of 0.0.0.0— is no small matter. Without it, a vulnerable application will be exposed to the local network. This is a common mistake when setting up a CTF lab, which can lead to the host being compromised from the outside.

OWASP Juice Shop is a more modern simulator with an online store interface: docker run -d -p 127.0.0.1:3000:3000 bkimminich/juice-shopInside is a built-in scoreboard showing progress on completing tasks. Essentially, it's a CTF in a single container.

Vulhub (github.com/vulhub/vulhub) is a collection of docker-compose files with real CVE vulnerabilities. Each scenario is a separate directory with docker-compose.yml, which raises a vulnerable service to the required version. This is closest to the tasks of real CTF competitions like Jeopardy.
Docker Compose for a Multi-Container Lab

When there are more than two target services, starting each with a separate command is inconvenient and easy to get confused. Docker Compose describes the entire lab in a single YAML file and bootstraps it with a single command:

version: "3.8"
services:
dvwa:
image: vulnerables/web-dvwa
ports: ["127.0.0.1:8080:80"]
networks: [ctflab]
juice-shop:
image: bkimminich/juice-shop
ports: ["127.0.0.1:3000:3000"]
networks: [ctflab]
networks:
ctflab:
internal: true

Parameter internal: true— key. It creates a Docker network with no external route: containers can see each other, but don't connect to the internet. It's similar to the Internal Network in VirtualBox, but for containers. The entire lab runs through docker compose up -dand stops after docker compose down.

File docker-compose.ymlIt's convenient to store in a Git repository. Move to another laptop—clone the repository—one command—and an identical, isolated hacking environment is ready in a minute. Docker Compose for information security is a way to make your lab portable and reproducible.
Hybrid Approach: Docker Inside a Virtual Machine

Running Docker inside a Kali VM provides maximum isolation when preparing for CTF competitions. The following scheme: host (host OS) → VirtualBox → Kali Linux VM (NAT + Host-Only) → Docker inside Kali → vulnerable containers.

Why double isolation? Even if a container escapes to host (MITRE ATT&CK T1611), the attacker will end up inside the Kali VM, not on your main system. A Kali VM snapshot captures all containers within—a complete rollback in a single action. Virtualization for information security here acts as insurance within insurance.

The downside is double overhead. The VM kernel plus container processes require 12–16 GB of RAM on the host. The hybrid will run with 8 GB, but will lag noticeably during active scanning.

Installing Docker in Kali: sudo apt install docker.io docker-compose-plugin, then sudo usermod -aG docker $USERAfter re-login, Docker is ready to work inside the VM.

This approach is justified for those who don't want to worry about the impact of experiments on their main workstation. If you have enough RAM, go for a hybrid—you won't regret it.
Virtual Machine Snapshots and Lab Management

Snapshots are insurance against irreversible actions. The rule is simple: if you do something important, record the state. Strategy for a home pentester's lab:

"Clean Install" — immediately after installing and updating Kali for the first time. This is a basic rollback point you can always return to.

"Pre-CTF [date]" – before each competition. We installed additional tools, configured a VPN, and saved the state.

"Post-CTF [name]" - after the competition, if you want to save the configuration for parsing problems and writing writeup.

VirtualBox stores virtual machine snapshots as differential disks. Each snapshot adds 100 to 500 MB. There's no point in storing more than five to seven snapshots per VM: they take up space and slow down rollbacks. Periodically delete intermediate snapshots, keeping the "Clean Install" and last working ones.

Docker containers are consumable. If they break, they can be deleted and rebuilt in seconds. Complete cleanup: docker system prune -aThis will remove all stopped containers and unused images, freeing up gigabytes. However, it removes EVERYTHING that's inactive—make sure you don't need old images.
Typical pitfalls of newbies and how to avoid them

"Not enough RAM, everything freezes." First, close the browser on the host: Chrome with 20 tabs open uses 2-4 GB. Reduce the Kali VM's RAM to 2 GB, and replace heavy target VMs with Docker containers. On Windows, disable Hyper-V if you're not using WSL2—it conflicts with VirtualBox and hogs resources even in the background. bcdedit /set hypervisorlaunchtype offrebooting solves the problem.

"Kali can't see the target VM." Make sure both virtual machines are connected to the same Host-Only network. In VirtualBox: "File - Host Network Manager" - there should be an adapter (usually vboxnet0) with a DHCP server enabled. If the IP isn't assigned automatically, set it manually: sudo ip addr add 192.168.56.10/24 dev eth1in Kali and a similar command with a different address on the target VM.

"The port is busy, the container won't launch." Check who's listening: ss -tlnp | grep 8080. Or change the startup mapping: instead of -p 8080:80use -p 9090:80.

"Accidentally scanned my home network." A classic with real-world consequences. Before each Nmap run, check the target address. If it's not in the lab subnet range (192.168.56.0/24 for Host-Only, 172.xxx for Docker), stop. Figure out the routing, then scan.

Mac M1/M2 and x86 images. VirtualBox on Apple Silicon doesn't run x86 guests like Metasploitable or VulnHub. Install Kali ARM64 as a VM, and run all target services in Docker. For complete Docker network isolation: docker network create --internal ctflabContainers on this network can only see each other.

Docker and VirtualBox subnet conflicts. Docker uses 172.17.0.0/16 by default, while VirtualBox Host-Only uses 192.168.56.0/24. These ranges usually don't overlap. However, if you've configured custom subnets, check the routes through ip routeon the host.
From the Lab to Real-World CTF Challenges

Lab is assembled, Kali responds, DVWA opens localhost:8080. What's next?

Vulhub is a bridge from training to real-world CVEs. Each Docker-compose file represents a specific vulnerability in a specific product version: Apache Struts, Jenkins, Redis. Clone the repository, navigate to the desired directory, docker compose up -d— the vulnerable setup is ready. It's the closest thing to real-world CTF challenges like Jeopardy.

HackTheBox and TryHackMe connect via VPN from your Kali VM. The lab serves as an isolated entry point: you work with external labs using tools from your own environment. The Host-Only adapter ensures that VPN traffic does not leak into your home network.

Creating your own tasks. Once you've mastered Docker Compose for information security at the consumer level, try building your own CTF task: write a Dockerfile with a vulnerable Flask application, hide the flag in an environment variable, and let a friend solve it. The Build Image on Host technique (T1612 according to MITRE ATT&CK) describes the process of building images on the target system—understanding this process is useful both when creating tasks and solving them.
 
Top Bottom