
The Complete Kali Linux Docker Guide
/ 5 min read
Table of Contents
Running Kali Linux in Docker is a great way to use its security tools without needing a full virtual machine. The main problem is that Docker containers are temporary by default, which means you can easily lose your work when you stop them. I needed a setup that would save my data and tools between sessions while still being secure.
This guide shows the methods I’ve worked out for creating a practical and reliable Kali Docker setup, focusing on how to save your data and manage permissions correctly.
This guide was tested on a machine running Ubuntu 24.04.3 LTS. Since Docker is a cross-platform tool, the commands and methods described here should work on any operating system that supports Docker.
What You’ll Learn in This Guide
- How to create a persistent Kali environment using Docker Volumes and Bind Mounts.
- A dual-container strategy to separate privileged and unprivileged tasks.
- How to manage on-demand privilege escalation for specific commands.
- Practical workflows and aliases to streamline your daily operations.
- Security best practices for a clean and safe containerized setup.
Part 1: Core Persistence Methods
The first challenge is making sure your data, tools, and configurations aren’t lost when you stop a container. There are two primary methods to achieve this.
1.1. Docker Volumes (Recommended)
Docker Volumes are the best way to manage persistent data. They are managed directly by the Docker engine, offering better performance and portability.
Setup:
# First, create a persistent volumedocker volume create kali-storage
# Run your container, mounting the volume to the /root directorydocker run -it --name my-kali -v kali-storage:/root kalilinux/kali-rolling
# Inside the container, install your preferred tools (one-time setup)apt update && apt -y install kali-linux-headlessAdvantages:
- Performance: Better I/O performance than bind mounts.
- Portability: Not tied to the host’s filesystem structure.
- Management: Easily managed with Docker CLI commands (
docker volume ls,docker volume rm).
1.2. Bind Mounts (Alternative)
Bind Mounts link a directory from your host machine directly into the container. This is useful for direct file access but can be less flexible.
Setup:
# Create a directory on your host machinemkdir ~/kali-data
# Run the container, mounting the host directorydocker run -it --name my-kali -v ~/kali-data:/root kalilinux/kali-rollingAdvantages:
- Simplicity: Easy to set up and access files from the host.
- Backup: Straightforward to back up the data by just copying the host directory.
Part 2: Privilege Management Strategies
Running everything as a privileged user is a security risk. A better approach is to separate tasks based on the permissions they require.
2.1. The Dual-Container System (Recommended)
This strategy involves using two separate containers that share the same persistent volume but have different privilege levels.
Implementation:
# Step 1: Create the shared volumedocker volume create kali-storage
# Step 2: Run a temporary "builder" container to install tools into the volumedocker run -it --name kali-builder --privileged \ -v kali-storage:/root kalilinux/kali-rolling \ bash -c "apt update && apt -y install kali-linux-headless"
# Step 3: Create your day-to-day unprivileged containerdocker run -it --name my-kali-unpriv \ -v kali-storage:/root kalilinux/kali-rolling
# Step 4: Create a separate, privileged container for tasks that need itdocker run -it --name my-kali-priv --privileged \ -v kali-storage:/root kalilinux/kali-rollingUsage Workflow:
- For regular work (reconnaissance, scripting):
docker start my-kali-unpriv && docker attach my-kali-unpriv - For privileged tasks (network scanning with
nmap -sS):docker start my-kali-priv && docker attach my-kali-priv
2.2. On-Demand Privilege Escalation
For quick, one-off tasks, you can grant privileges to an existing container temporarily.
# Execute a single privileged commanddocker exec -it --privileged my-kali-unpriv nmap -sS target
# Or, open a temporary privileged shell in your unprivileged containerdocker exec -it --privileged my-kali-unpriv /bin/bashPart 3: Practical Workflow & Tips
Here’s how to put it all together for an efficient daily workflow.
3.1. Environment Setup
- Use Docker volumes for your primary storage.
- Install the
kali-linux-headlessmetapackage for a solid baseline of tools. - Create the dual-container setup for security separation.
3.2. Efficiency Aliases
To make switching between containers seamless, add these aliases to your ~/.bashrc or ~/.zshrc file on your host machine:
# Attach to the unprivileged containeralias kali-unpriv='docker start my-kali-unpriv && docker attach my-kali-unpriv'
# Attach to the privileged containeralias kali-priv='docker start my-kali-priv && docker attach my-kali-priv'
# Get a temporary privileged shellalias kali-temp-priv='docker exec -it --privileged my-kali-unpriv /bin/bash'3.3. Maintenance Commands
Keep your Docker environment clean with these commands:
# View all containers (running and stopped)docker ps -a
# List all volumesdocker volume ls
# Clean up unused containers, networks, and volumesdocker system prune --volumesPart 4: Security Best Practices
- Least Privilege: Always default to your unprivileged container.
- Temporary Escalation: Only use privileged mode when strictly necessary.
- Regular Updates: Periodically run
apt update && apt upgradeinside your containers. - Clean Environment: Regularly use
docker system pruneto remove old resources.
Part 5: Conclusion & References
This setup provides a persistent, secure, and flexible Kali Linux environment within Docker. By separating storage from the container and managing privileges effectively, you get the best of both worlds: the power of Kali’s tools and the lightweight, isolated nature of containers.
For more information, refer to the official Kali Linux documentation:
- Official Kali Linux Docker Images: www.kali.org/docs/containers/using-kali-docker-images/
- Docker Desktop Installation: docs.docker.com/desktop/setup/install/linux/
- Docker Products Overview: www.docker.com/products/docker-desktop/
- Hackers-Arise Kali Docker Guide: hackers-arise.com/getting-started-with-docker-part-1-installing-kali-linux-in-a-docker-container/
- Installing Docker on Kali: www.kali.org/docs/containers/installing-docker-on-kali/