🐳 What is Docker?
Docker is a platform used to build, ship, and run applications inside containers.
🔹 What is a Container?
A container is a lightweight, portable package that includes everything needed for an application to run — code, libraries, dependencies, and environment settings — so it runs the same on any system.
⚙️ Why Use Docker?
- Works the same on all systems (solves “it works on my machine” issue)
- Fast, lightweight, and uses fewer resources than Virtual Machines
- Easy to deploy and scale across environments
- Great for DevOps, AppSec, and automation tasks
Docker Syntax Categories
- Running a container
- Managing & Inspecting containers
- Managing Docker images
- Docker daemon stats & info
Managing Docker Images
Pulling Images
Download an image with docker pull IMAGE[:TAG].
If no tag is specified → defaults to latest.
docker pull nginxdocker pull ubuntudocker pull ubuntu:22.04docker pull ubuntu:20.04docker pull ubuntu:18.04Tags:
ubuntu:latest→ latest release (could change)ubuntu:22.04→ Jammyubuntu:20.04→ Focalubuntu:18.04→ Bionic
Docker Image Management
shohan@kali:~$ docker image
Usage: docker image COMMAND
Manage images
Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.shohan@kali:~$Example:
pull(we have done this above!)ls(list images)rm(remove an image)build(we will come onto this in the “Building your First Container” task)
Docker Image ls
This command allows us to list all images stored on the local system. We can use this command to verify if an image has been downloaded correctly and to view a little bit more information about it (such as the tag, when the image was created and the size of the image).
shohan@kali:~$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 22.04 2dc39ba059dc 10 days ago 77.8MBnginx latest 2b7d6430f78d 2 weeks ago 142MBshohan@kali:~$| Repository | Tag | Image ID | Size |
|---|---|---|---|
| ubuntu | 22.04 | 2dc39ba059dc | 77.8MB |
| nginx | latest | 2b7d6430f78d | 142MB |
Docker Image rm
If we want to remove an image from the system, we can use docker image rm along with the name (or Image ID). In the following example, I will remove the “ubuntu” image with the tag “22.04”. My command will be docker image rm ubuntu:22.04:
It is important to remember to include the tag with the image name.
shohan@kali:~$ docker image rm ubuntu:22.04Untagged: ubuntu:22.04Untagged: ubuntu@sha256:20fa2d7bb4de7723f542be5923b06c4d704370f0390e4ae9e1c833c8785644c1Deleted: sha256:2dc39ba059dcd42ade30aae30147b5692777ba9ff0779a62ad93a74de02e3e1fDeleted: sha256:7f5cbd8cc787c8d628630756bcc7240e6c96b876c2882e6fc980a8b60cdfa274shohan@kali:~$Running Containers
Syntax
docker run [OPTIONS] IMAGE [COMMAND] [ARGS...]RUN Container
docker run -it helloworld /bin/bashCommon Options
| Option | Description | Example |
|---|---|---|
-it | Interactive shell | docker run -it helloworld /bin/bash |
-d | Detached mode | docker run -d helloworld |
-v | Mount volume | docker run -v /host:/container helloworld |
-p | Port mapping | docker run -p 80:80 webserver |
--rm | Remove container after exit | docker run --rm helloworld |
--name | Assign name | docker run --name mycontainer helloworld |
Listing Containers
docker ps # list runningdocker ps -a # list all (including stopped)Intro to Dockerfiles
Dockerfiles are instruction manuals for building Docker images. They contain commands that define what a container should do.
Syntax: INSTRUCTION argument
🔑 Core Instructions
| Instruction | Description | Example ||-------------|------------|---------|| FROM | Base image | FROM ubuntu:22.04 || RUN | Execute cmd| RUN whoami || COPY | Copy files | COPY ./app/ /app/ || WORKDIR | Set dir | WORKDIR / || CMD | Default cmd| CMD ["./script.sh"] |🚀 Example 1: Simple Dockerfiles
Goal:
- Use Ubuntu 22.04
- Set working directory to root (/)
- Create a helloworld.txt file
Dockerfiles:
# THIS IS A COMMENT# Use Ubuntu 22.04 as the base operating system of the containerFROM ubuntu:22.04
# Set the working directory to the root of the containerWORKDIR /
# Create helloworld.txtRUN touch helloworld.txtBuild the image:
docker build -t helloworld .shohan@kali:~$ docker build -t helloworld .Sending build context to Docker daemon 4.778MBStep 1/3 : FROM ubuntu:22.0422.04: Pulling from library/ubuntu2b55860d4c66: Pull completeDigest: sha256:20fa2d7bb4de7723f542be5923b06c4d704370f0390e4ae9e1c833c8785644c1Status: Downloaded newer image for ubuntu:22.04 ---> 2dc39ba059dcStep 2/3 : WORKDIR / ---> Running in 64d497097f8aRemoving intermediate container 64d497097f8a ---> d6bd1253fd4eStep 3/3 : RUN touch helloworld.txt ---> Running in 54e94c9774beRemoving intermediate container 54e94c9774be ---> 4b11fc80fdd5Successfully built 4b11fc80fdd5Successfully tagged helloworld:latestshohan@kali:~$Check built images:
shohan@kali:~$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEhelloworld latest 4b11fc80fdd5 2 minutes ago 77.8MBubuntu 22.04 2dc39ba059dc 10 days ago 77.8MBshohan@kali:~$🌐 Example 2: Apache Web Server
Goal:
- Use Ubuntu 22.04
- Install Apache2
- Expose port 80
- Start Apache at container launch
Dockerfile:
# THIS IS A COMMENTFROM ubuntu:22.04
# Update the APT repository to ensure we get the latest version of apache2RUN apt-get update -y
# Install apache2RUN apt-get install apache2 -y
# Tell the container to expose port 80 to allow us to connect to the web serverEXPOSE 80
# Tell the container to run the apache2 serviceCMD ["apache2ctl", "-D","FOREGROUND"]Build & Run:
docker build -t webserver .docker run -d --name webserver -p 80:80 webserverOpen in browser: http://localhost

⚡ Optimizing Dockerfiles
Why optimize?
- Smaller images
- Faster builds
- Easier to maintain
Techniques:
- Install only what you need
- Clean caches & temp files
- Use minimal base images (e.g., alpine, ubuntu-minimal)
- Reduce layers by chaining commands
❌ Before (many layers):
FROM ubuntu:latestRUN apt-get update -yRUN apt-get upgrade -yRUN apt-get install apache2 -yRUN apt-get install net-tools -y✅ After (fewer layers):
FROM ubuntu:latestRUN apt-get update -y && \\ apt-get upgrade -y && \\ apt-get install -y apache2 net-tools
Docker Compose

Common Commands
docker-compose updocker-compose startdocker-compose stopdocker-compose downdocker-compose buildExample docker-compose.yml
version: '3.3'services: web: build: ./web networks: - ecommerce ports: - '80:80'
database: image: mysql:latest networks: - ecommerce environment: - MYSQL_DATABASE=ecommerce - MYSQL_USERNAME=root - MYSQL_ROOT_PASSWORD=helloworld
networks: ecommerce:Summary
- Built and managed Docker images using Dockerfiles and docker build
- Pulled, listed, and removed images efficiently (docker pull, docker image ls, docker image rm)
- Deployed and managed containers with key options for interactivity, detached mode, port mapping, and volume mounting (docker run -it/-d -p -v)
- Utilized Docker Compose for orchestrating multi-container applications
- Optimized Dockerfiles to minimize image size, reduce layers, and improve build efficiency