skip to content
Sean Thawe
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.

Docker Volumes are the best way to manage persistent data. They are managed directly by the Docker engine, offering better performance and portability.

Setup:

Terminal window
# First, create a persistent volume
docker volume create kali-storage
# Run your container, mounting the volume to the /root directory
docker 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-headless

Advantages:

  • 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:

Terminal window
# Create a directory on your host machine
mkdir ~/kali-data
# Run the container, mounting the host directory
docker run -it --name my-kali -v ~/kali-data:/root kalilinux/kali-rolling

Advantages:

  • 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.

This strategy involves using two separate containers that share the same persistent volume but have different privilege levels.

Implementation:

Terminal window
# Step 1: Create the shared volume
docker volume create kali-storage
# Step 2: Run a temporary "builder" container to install tools into the volume
docker 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 container
docker run -it --name my-kali-unpriv \
-v kali-storage:/root kalilinux/kali-rolling
# Step 4: Create a separate, privileged container for tasks that need it
docker run -it --name my-kali-priv --privileged \
-v kali-storage:/root kalilinux/kali-rolling

Usage 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.

Terminal window
# Execute a single privileged command
docker exec -it --privileged my-kali-unpriv nmap -sS target
# Or, open a temporary privileged shell in your unprivileged container
docker exec -it --privileged my-kali-unpriv /bin/bash

Part 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-headless metapackage 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:

Terminal window
# Attach to the unprivileged container
alias kali-unpriv='docker start my-kali-unpriv && docker attach my-kali-unpriv'
# Attach to the privileged container
alias kali-priv='docker start my-kali-priv && docker attach my-kali-priv'
# Get a temporary privileged shell
alias kali-temp-priv='docker exec -it --privileged my-kali-unpriv /bin/bash'

3.3. Maintenance Commands

Keep your Docker environment clean with these commands:

Terminal window
# View all containers (running and stopped)
docker ps -a
# List all volumes
docker volume ls
# Clean up unused containers, networks, and volumes
docker system prune --volumes

Part 4: Security Best Practices

  1. Least Privilege: Always default to your unprivileged container.
  2. Temporary Escalation: Only use privileged mode when strictly necessary.
  3. Regular Updates: Periodically run apt update && apt upgrade inside your containers.
  4. Clean Environment: Regularly use docker system prune to 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: