Running Kali Linux inside a lightweight container is a quick way to spin up a disposable penetration-testing lab. The steps below show how to provsiion the distribution with Docker Compose, map useful ports, and persist your tools and findings on the host.
Prerequisites
- A Linux host with
sudoaccess (Ubuntu 22.04 is used in the examples). - Outbound Internet connectivity to pull images.
- Install Docker Engine
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
- Install Docker Compose v2
Ubuntu 22.04 ships the legacy Python-based docker-compose. Prefer the newer plugin:
sudo apt install -y docker-compose-v2
Verify with docker compose version.
- Create the Project Directory
mkdir ~/kali-lab && cd ~/kali-lab
- Compose File
Save the following as compose.yml:
services:
kali:
image: kalilinux/kali-rolling:latest
container_name: kali-ctf
stdin_open: true
tty: true
ports:
- "2222:22" # SSH jump host
- "8080:8080" # Web proxies (Burp, ZAP, etc.)
- "4444:4444" # Reverse shells
volumes:
- ./workspace:/home/kali/workspace
working_dir: /home/kali
command: ["/bin/bash"]
- Bring the Lab Online
docker compose up -d
The first run downloads ~3 GB of image layers.
- Enter the Container
docker exec -it kali-ctf bash
You now have a root shell in a minimal Kali environment. Install your preferred toolset:
apt update && apt install -y kali-linux-headless
- Optional SSH Access
Inside the container:
service ssh start
passwd kali # set a password for the kali user
From the host:
ssh -p 2222 kali@localhost
- Persisting State
The ./workspace directory on the host is mounted at /home/kali/workspace. Scripts, wordlists, and reports placed there survive container restarts and rebuilds.
- Tear Down
docker compose down # stops and removes the container
docker image prune -f # optional: reclaim disk space
This setup gives you a reproducible, isolated Kali instance that can be started in seconds and discarded just as quickly, making it ideal for CTF events, red-team exercises, or ad-hoc vulnerability assessments.