How to set up Docker and run Kali on Debian

Docker is more lightweight and faster than vms since it isn’t fully isolated and uses the host machine’s kernel. This article explains how to quickly set up kali on debian.

Install Docker Engine

Set up the apt repository

First install prerequisites:

sudo apt install ca-certificates crl
sudo install -m 0755 -d /etc/apt/keyrings

Add docker’s GPG key:

sudo curl -fsSL https:download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Add the repository to your apt sources:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

Now you can use apt to download Docker.

Install the docker packages

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

You can verify if it’s succcesfully installed with this:

sudo docker run hello-world

If apt doesn’t work, you can get the deb file from https://download.docker.com/linux/debian/dists/.

Run Kali

First pull the kali linux docker image:

sudo docker pull kalilinux/kali-rolling

Run kali in a docker container:

sudo docker run --tty -it kalilinux/kali-rolling /bin/bash

-it option is a shorthand for –terminal and it opens an interactive terminal. If you don’t know much about -tty and have some time to spare, I recommend reading this: ‘The TTY demystified’.

Now you’re on kali but you have no tools. You need to install tools you need one by one or get them in bulk.

sudo apt update && apt -y install kali-linux-headless

How to use GUI apps

X11 forwarding

The common way to use GUI apps on docker is using X11 forwarding and displaying GUI from a container to your host machine.

Start the container with display configuration:

sudo docker run -it \
    --env="DISPLAY" \
    --volume="$HOME/.Xauthority:/root/.Xauthority:rw" \
    --net=host \
    kalilinux/kali-rolling

You should probably make a shell alias or a script as it’s too long to run every single time. When you run an image you create, replace kalilinux/kali-rolling with your image’s name.

Set up GUI environment

Although it’s not really meant to be, you can set up a GUI environment on docker too.

Install a desktop environment:

sudo apt install -y kali-desktop-xfce

I like xfce but you can also choose gnome or kde.

Install a display manager:

sudo apt install -y lightdm

Start it:

sudo systemctl start lightdm

How to use Docker

List the container IDs:

sudo docker ps -a

Note that this lists container IDs not image IDs.

Start the container with an ID:

sudo docker start *id*

You can stop it by replacing start with stop.

You need attach it after starting:

sudo docker attach *id*

To run a docker command in a running container:

sudo docker exec -it *id_or_name* /bin/bash

This opens a new interactive shell. To be clear, you should run this command outside the container.

To delete a container:

sudo docker rm *id_or_name*

Using images

You can save the current state as an image and make it portable:

sudo docker commit *container_name_or_id* *image_name*

To run an image:

sudo docker run -it kalilinux/kali-rolling

Difference between ‘attach’ and ‘run’

When you use run, it creates a new instance from a container. You should use it when you wanna make a new environment, for example when starting working with a new client.

On the other hand, attach doesn’t make a new instance. It just connects to the interactive shell of an existing container. This is enough when you wanna keep using the same container and don’t need to revert changes you make.