DOCKER



1. What is Docker?

Docker is a tool that runs applications inside isolated environments called containers.

Imagine your college computer lab has 30 computers. You want every student to have Python, Scapy, Nmap, and tcpdump installed. Installing and configuring everything separately on each computer takes time.

With Docker, you can prepare the environment once and give every student a separate container with the required tools.

For example, you could have:

  • Student 1 — Python and Scapy container.

  • Student 2 — Nmap and tcpdump container.

  • Student 3 — Web security practice container.

  • Student 4 — Digital forensics container.

Each container has its own isolated filesystem and processes, while sharing the host computer's Linux kernel.

2. Understand these five Docker terms

A Step-by-Step Guide to Creating a Docker Image | by Harpreet Sahota | Pachyderm Community Blog | Medium

1. Dockerfile — The recipe

A text file that describes how to prepare your environment, such as installing Python, Scapy and Nmap.

Understanding Docker Image Layers: Optimizing Build Performance - DEV Community

2. Image — The prepared template

A reusable package containing the filesystem and software needed to run your environment.

Docker Basics — The Duckiebot Operation Manual - daffy

3. Container — The running environment

An instance created from an image. You can create multiple containers from the same image.

Learn How to Optimize Usage with New Docker Hub Dashboards | Docker

4. Registry — The image store

A service such as Docker Hub where you can download and upload images.

Tailscale, a virtual programmable network for DevOps

5. Docker Engine — The runtime

The software on your Ubuntu machine that builds images and runs containers.

The basic workflow is:

Write a Dockerfile
Build an image
Run a container
Use the application inside it

3. Start practising Docker on Ubuntu 24.04

Open your Ubuntu terminal and follow these steps in order.

Step 1 — Check whether Docker is installed

docker --version
sudo systemctl status docker

If Docker is installed and running, continue to Step 2. If the command is missing, Docker needs to be installed first.

Step 2 — Run your first container

sudo docker run hello-world

What happens?

  1. Docker looks for the hello-world image on your computer.

  2. If it is missing, Docker downloads it from Docker Hub.

  3. Docker creates and runs a container.

  4. The container prints a success message and exits.

This is your first Docker application!

Step 3 — Start an Ubuntu container

sudo docker run -it --name myubuntu ubuntu:24.04 bash

You are now inside a separate Ubuntu environment. Your prompt will change.

Try these commands inside the container:

cat /etc/os-release
whoami
pwd
ls

Install a package inside the container:

apt update
apt install -y python3
python3 --version

To leave the container, type:

exit

Important: Exiting stops this container, but it does not delete it. You can start it again later.

Step 4 — Learn the essential Docker commands

Try these commands from your normal Ubuntu terminal, not from inside the container.

Command

Purpose

sudo docker ps

Show running containers

sudo docker ps -a

Show all containers, including stopped ones

sudo docker images

List downloaded and built images

sudo docker start myubuntu

Start your stopped container

sudo docker stop myubuntu

Stop a running container

sudo docker exec -it myubuntu bash

Open a terminal in a running container

sudo docker logs myubuntu

View container logs

sudo docker rm myubuntu

Delete the stopped container

sudo docker rmi ubuntu:24.04

Delete an image if no containers depend on it

You can start your container and enter it again like this:

sudo docker start myubuntu
sudo docker exec -it myubuntu bash

4. Create your own Docker image

Now let's create a simple Python environment. This is the same basic process you will use later to prepare your cybersecurity lab.

Create a project directory:

mkdir -p ~/docker-practice
cd ~/docker-practice

Create a file named Dockerfile:

nano Dockerfile

Enter this content:


FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y python3 && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

CMD ["python3", "--version"]

Save in Nano with Ctrl+O, press Enter, and exit with Ctrl+X.

Here is what each line means:

  • FROM — selects the starting image.

  • RUN — executes commands while building the image.

  • WORKDIR — sets the working directory.

  • CMD — specifies the default command when the container starts.

Build your image:

sudo docker build -t my-python-lab:1.0 .

The -t option gives your image a name and version. The final . means that Docker should use the current directory as the build context.

Run your image:

sudo docker run --rm my-python-lab:1.0

Expected output will be similar to:

Python 3.12.x

The exact version may differ. The --rm option automatically removes the container after it exits, but keeps the image.

5. Run separate containers for students

Once you have prepared an image, you can run multiple containers from it.

For example:

sudo docker run -dit --name student01 my-python-lab:1.0
sudo docker run -dit --name student02 my-python-lab:1.0
sudo docker run -dit --name student03 my-python-lab:1.0

These commands create three separate containers from the same image. The -d option runs them in the background, while -i and -t enable interactive terminal use.

View them:

sudo docker ps

Open Student 1's container:

sudo docker exec -it student01 bash


INSTALLATION

1. Ubuntu / Debian Setup
For standard Ubuntu (22.04, 24.04, 26.04) or Debian (12, 13) environments: [1, 2]
StepCommand / Action
1. Remove old packagessudo apt-get remove docker docker-engine docker.io containerd runc
2. Update system repositorysudo apt-get update
3. Install prerequisitessudo apt-get install ca-certificates curl gnupg
4. Add Docker's GPG keysudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg (Note: Swap ubuntu for debian in the URL if using Debian)
5. Set up repositoryecho "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
6. Install Docker Enginesudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin



Start and Enable the Docker Service

  1. Start the engine:
    bash
    sudo systemctl start docker
    
    Use code with caution.
  2. Enable autostart on system boot:
    bash
    sudo systemctl enable docker

Optional: Run Docker Without Sudo
If you prefer not to type sudo before every docker command, add your current user account to the system docker group:
  1. Create the docker group: sudo groupadd docker
  2. Add your user: sudo usermod -aG docker $USER
  3. Activate group changes: newgrp docker

Verify it works 

Test your new native installation by pulling a lightweight test image: 
 bash 
  docker run hello-world

Errors Occurred:

labadmin@Q70:~/Downloads$ docker run hello-world permission denied while trying to connect to the docker API

The native Docker Engine is installed and running, but your labadmin user doesn't have permission to talk to the background service yet. The group change we applied in the previous step requires a fresh session initialization to apply. Run the following command to immediately activate the group permissions without needing to restart your computer:

bash newgrp docker
DOCKER RUN IMAGES IN ADMIN MODE
$
docker run -it \ --name student02-lab \ --hostname student02 \ --user root \ --cap-add NET_RAW \ --cap-add NET_ADMIN \ cyberlab:1.0 \ /bin/bash


Step 1: Start the existing container

On your Ubuntu machine H45, run:

docker start student02-lab

Step 2: Log in to the container

docker exec -it student02-lab /bin/bash

Step 3: Verify access

Inside the container, run:

whoami
hostname

If the container was configured to run as root, whoami should return root.

If the container stops immediately again

Check its status and exit reason:

docker ps -a --filter name=student02-lab
docker logs student02-lab



1. Copy files between your local machine and Docker

Assume your container name is student02-lab.

Docker container → Local machine

Download a file from the container

docker cp student02-lab:/workspace/output.txt ~/output.txt

Local machine → Docker container

Upload a file into the container

docker cp ~/input.txt student02-lab:/workspace/input.txt

These commands work even if the container is stopped, provided it still exists.

2. Copy an entire folder

Copy a folder from the container to your local machine:

docker cp student02-lab:/workspace ~/student-workspace

Copy a local folder into the container:

docker cp ~/student-files student02-lab:/workspace/

3. Copy files with different permissions

If you get a permission error, use sudo on the Ubuntu host if your account has administrator privileges:

sudo docker cp student02-lab:/workspace/output.txt /home/student/output.txt
sudo chown student:student /home/student/output.txt

Replace student with your actual Ubuntu username if different.

4. Example for your cybersecurity lab

Suppose a student runs Scapy and saves a capture or report as /workspace/capture.pcap inside the container.

Copy it to the Ubuntu host:

docker cp student02-lab:/workspace/capture.pcap ~/capture.pcap

Copy a Python practical file into the container:

docker cp ~/scapy_lab.py student02-lab:/workspace/scapy_lab.py

To check the container name and status:

docker ps -a

Comments

Popular posts from this blog