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
1. Dockerfile — The recipe
A text file that describes how to prepare your environment, such as installing Python, Scapy and Nmap.
2. Image — The prepared template
A reusable package containing the filesystem and software needed to run your environment.
3. Container — The running environment
An instance created from an image. You can create multiple containers from the same image.
4. Registry — The image store
A service such as Docker Hub where you can download and upload images.
5. Docker Engine — The runtime
The software on your Ubuntu machine that builds images and runs containers.
The basic workflow is:
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 dockerIf 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-worldWhat happens?
Docker looks for the
hello-worldimage on your computer.If it is missing, Docker downloads it from Docker Hub.
Docker creates and runs a container.
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 bashYou are now inside a separate Ubuntu environment. Your prompt will change.
Try these commands inside the container:
cat /etc/os-release
whoami
pwd
lsInstall a package inside the container:
apt update
apt install -y python3
python3 --versionTo leave the container, type:
exitImportant: 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 |
|---|---|
| Show running containers |
| Show all containers, including stopped ones |
| List downloaded and built images |
| Start your stopped container |
| Stop a running container |
| Open a terminal in a running container |
| View container logs |
| Delete the stopped container |
| 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 bash4. 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-practiceCreate a file named Dockerfile:
nano DockerfileEnter 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.0Expected output will be similar to:
Python 3.12.xThe 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.0These 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 psOpen Student 1's container:
sudo docker exec -it student01 bash
INSTALLATION1. Ubuntu / Debian Setup
Step Command / Action 1. Remove old packages sudo apt-get remove docker docker-engine docker.io containerd runc2. Update system repository sudo apt-get update3. Install prerequisites sudo apt-get install ca-certificates curl gnupg4. Add Docker's GPG key sudo install -m 0755 -d /etc/apt/keyringscurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpgsudo chmod a+r /etc/apt/keyrings/docker.gpg(Note: Swapubuntufordebianin the URL if using Debian)5. Set up repository echo "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/null6. Install Docker Engine sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start and Enable the Docker Service
- Start the engine:
bashsudo systemctl start dockerUse code with caution.- Enable autostart on system boot:
bashsudo systemctl enable dockerOptional: Run Docker Without SudoIf you prefer not to typesudobefore everydockercommand, add your current user account to the system docker group:
- Create the docker group:
sudo groupadd docker- Add your user:
sudo usermod -aG docker $USER- Activate group changes:
newgrp dockerVerify it works
Test your new native installation by pulling a lightweight test image:bashdocker run hello-worldErrors Occurred:
labadmin@Q70:~/Downloads$ docker run hello-world permission denied while trying to connect to the docker APIThe 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 dockerDOCKER 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/bashStep 1: Start the existing container
On your Ubuntu machine
H45, run:docker start student02-labStep 2: Log in to the container
docker exec -it student02-lab /bin/bashStep 3: Verify access
Inside the container, run:
whoami hostnameIf the container was configured to run as root,
whoamishould returnroot.If the container stops immediately again
Check its status and exit reason:
docker ps -a --filter name=student02-lab docker logs student02-lab1. 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.txtLocal machine → Docker container
Upload a file into the container
docker cp ~/input.txt student02-lab:/workspace/input.txtThese 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-workspaceCopy 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
sudoon 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.txtReplace
studentwith 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.pcapinside the container.Copy it to the Ubuntu host:
docker cp student02-lab:/workspace/capture.pcap ~/capture.pcapCopy a Python practical file into the container:
docker cp ~/scapy_lab.py student02-lab:/workspace/scapy_lab.pyTo check the container name and status:
docker ps -a
Comments
Post a Comment