A Comprehensive Guide to Setting Up Kali Linux on Docker
What is Docker
Docker is a platform and tool designed to simplify developing, deploying, and managing applications. It achieves this through containerization, which is a lightweight form of virtualization.
Dockerās key components and concepts
- Containers: Containers are standalone, executable packages that contain everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. They are isolated from the host system and other containers, ensuring consistency and portability across different environments.
- Docker Engine: Docker Engine is the core component of Docker. Itās a lightweight runtime and set of tools that enables you to create, run, and manage containers. It includes the Docker daemon (a background service) and the Docker CLI (Command-Line Interface).
- Images: Docker images are read-only templates that define the contents and configuration of a container. Images are used to create running containers, and they can be versioned, shared, and stored in repositories like Docker Hub.
- Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, the application code, dependencies, and configuration settings. Docker images are created by building them from Dockerfiles.
- Docker Compose: Docker Compose is a tool for defining and running multi-container applications. It allows you to define an applicationās services, networks, and volumes in a single YAML file, making it easier to manage complex applications with multiple containers.
- Docker Hub: Docker Hub is a cloud-based registry service where you can find, share, and distribute Docker images. It provides access to a vast repository of pre-built images that you can use as a starting point for your containers.
Docker Installation
To install Docker on your system, you need to follow platform-specific instructions. Docker provides installation guides for various operating systems, including Linux, Windows, and macOS.
The process is relatively straightforward.
- Download Docker Desktop by visiting the official Docker website (https://www.docker.com/products/docker-desktop).
- Install Docker Desktop by running the downloaded installer.
- Run Docker Desktop to start Docker.
This process is specifically for macOS, providing a convenient way to use Docker on your Mac system.
Docker Images VS Docker Containers
Before proceeding, one must understand the fundamental difference between an Image and a Container.
Docker Image:
A read-only lightweight, standalone, and executable package includes everything needed to run the software, including the code, a runtime, libraries, environment variables, and config files. They are used as templates or blueprints for creating containers. They are typically built from a Dockerfile, a set of instructions for creating an image.
Docker Container:
A container is a runnable instance of a Docker image. It is a lightweight and isolated environment that runs applications and processes in an isolated manner. Containers execute applications and services consistently and reproducibly across different environments.
Docker Hub
Docker Hub is a cloud-based platform and a centralized repository that provides a wide range of features and services for Docker users. It is a hub for Docker container images, helping developers and organizations store, distribute, and collaborate on containerized applications and services.
Docker Hub hosts āOfficial Images,ā which are curated and maintained by Docker, Inc. These images are typically well-vetted and serve as trusted base images for various software and services, including operating systems, databases, and web servers.
Having gained a foundational grasp of Docker and its key concepts, we are now ready to establish our initial container.
By executing this command, we can retrieve the Official Kali Image from the Docker Hub.
docker pull kalilinux/kali-rolling
Utilize these commands to observe all the images stored locally and to remove locally stored images.
docker images -a
docker image rm <image_id>
We can initiate a Kali container using the image in the following manner.
docker run -it kalilinux/kali-rolling /bin/bash
In a mere matter of seconds, weāve successfully spawned a Kali Container. This demonstrates the remarkable efficiency of containerization.
Upon reviewing the documentation for this Kali Image, Iāve also noticed that it lacks any pre-installed metapackages. Naturally, we have the option to install the required packages individually or collectively as metapackages.
apt update && apt -y install kali-linux-headless
The duration of this process can vary significantly based on your internet speed. However, the concept of creating a container and subsequently installing numerous packages can be time-consuming. If you find yourself needing to perform this task on a daily basis, it could become quite a burdensome experience.
Our solution to streamline this process is to create a custom image, tailored to our specific needs, which can be shared either publicly or privately as required.
Initially, we establish a directory that will facilitate sharing between our Kali container and our host system. Iāve named mine āshare,ā but you can choose any name you prefer, as long as you remember the absolute path to the shared folder.
Generate a custom Docker image based on a currently running container.
After installing all the desired packages and tailoring the container to meet our specific requirements, we can proceed to create a custom Kali Image, which will serve as the foundation for launching all our future containers.
In a fresh terminal window, we execute the ādocker ps -aā command to display a list of all containers currently running on our local system. Once we have identified the CONTAINER ID, we can proceed to the subsequent steps.
Docker commit is a command used in Docker to create a new image from an existing container. When you run a container, itās based on an image, and you can make changes to that container, such as installing software, configuring settings, or creating files. Docker commit allows you to capture these changes and save them as a new image, effectively creating a snapshot of the containerās current state.
The basic syntax is as follows:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS
: You can specify additional options, such as specifying a commit message.CONTAINER
: This is the ID or name of the container you want to create an image from.REPOSITORY
: This is the name you give to your new image.TAG
: You can optionally provide a tag to version your image. If not specified, it defaults to "latest."
Verify that the custom image has been successfully stored by running the ādocker images -aā command.
The custom image ākali_v2ā has been successfully preserved. Now, we can proceed to create a container using our customized Kali image.
As a reminder, we established a directory named āshareā within our Kali container to facilitate file sharing between the container and the host. On my host machine, Iāve created a folder named ādocker_sharedā to establish a connection with the shared volume on the Kali container.
To create and configure Docker containers, you often use the docker run
command. Let's dissect the following command to understand how it works:
docker run --name kali_v2_custom -v /Users/pg/Documents/docker_shared:/home/share -it kali_v2 /bin/bash
- docker run: This is the fundamental command for launching Docker containers.
- ā name kali_v2_custom: This part assigns a name to the container, in this case, "kali_v2_custom".
- -v /Users/pg/Documents/docker_shared:/home/share : This option establishes a volume mount. It links the local directory /Users/pg/Documents/docker_shared on the host machine with the /home/share directory inside the container. This enables seamless data sharing between the host and container.
- -it: These options allocate a pseudo-TTY (-t) and keep STDIN open (-i), creating an interactive shell session within the container. This allows you to actively interact with the container and run commands.
- kali_v2: Specifies the base Docker image from which the container will be created. In this instance, itās ākali_v2ā
- /bin/bash: Finally, the command to execute inside the container is defined as "/bin/bash," which launches an interactive Bash shell. This provides direct access to the container's shell, enabling you to execute commands and interact with the containerized environment.
Indeed, youāve successfully achieved it ā a customized Kali container up and running in just a matter of seconds. This demonstrates the efficiency and power of Docker containerization.
To confirm the proper setup of the shared folder, letās check if itās working as expected. Iāll create a file called āhello.txtā within the shared folder on the host machine, and it should automatically be accessible from the Kali container.
We have now established a Kali Linux Docker container that is containerized, allowing us to initiate it rapidly, facilitate seamless file sharing between the host and the container, and ensure file persistence through our shared directory.