Docker — Containers Simplified

Docker packages applications and their dependencies into portable containers so they run consistently across environments.
1. Introduction
Docker is a containerization platform that automates the deployment of applications inside lightweight, portable containers. A container bundles your application code with its runtime, system tools, libraries, and settings — ensuring the app runs the same on a developer laptop, CI server, or production host. It has several features like:
- Consistency: “Works on my machine” problems vanish.
- Isolation: Containers isolate processes and dependencies.
- Portability: Images run anywhere with the Docker runtime.
- Efficiency: Containers are lightweight compared to full VMs.
- Speed: Faster startup and simplified CI/CD.
2. Architecture
Docker uses a client-server architecture where the Docker client communicates with the Docker daemon, which builds, runs, and manages containers using images and Linux kernel features like namespaces and cgroups.
2. Key concepts
2.1 Virtualization
it is technique that allows the computer to act like multiple independent computers by creating virtual versions of hardware and software. somehow docker uses OS-level virtualization which is called containerization.
Architecture:
2.2 Containerization
It is a lightweight form of virtualization where apps run in isolated environments called continers.Containers share the host kernel and isolation is provided by namespaces (PID, mount, network, user, etc.) and cgroups for resource limits.
Benefits: Fast, secure, portable, scalable, lightweight.
Architecture:
2.3 Components
Docker Engine: The runtime that builds, runs, and manages containers (via the Docker CLI and API).
Image: An immutable, layered artifact built from a Dockerfile. Images are versioned and pushed to registries.
Container: A running instance of an image (an isolated process namespace).
Registry: Storage for images (Docker Hub, GitHub Container Registry, private registries).
Volume: Persistent storage mounted into containers.
Network: Bridge, host, or overlay networks for service connectivity.
Dockerfile: A text file with instructions (FROM, COPY, RUN, CMD) used to build an image.
Docker Compose: A tool for defining and running multi-container applications with a YAML file.
3. Common Docker commands
| Command | Description |
|---|---|
docker run |
Create and run a new container from an image |
docker exec |
Execute a command in a running container |
docker ps |
List running containers |
docker ps -a |
List all containers (running and stopped) |
docker build |
Build an image from a Dockerfile |
docker bake |
Build from a file |
docker pull |
Download an image from a registry |
docker push |
Upload an image to a registry |
docker images |
List all downloaded Docker images |
docker login |
Authenticate to a Docker registry |
docker logout |
Logout from a Docker registry |
docker search |
Search Docker Hub for images |
docker version |
Show Docker version info |
docker info |
Display system-wide Docker info |
4. Container & Image Lifecycle Commands
List All Containers
docker ps -a
Delete a container
docker rm <container_id>
List All Images
docker images
Delete an image
docker rmi <image_id>
- Every time you run a Docker container, a new container ID is generated.
- Remove containers before deleting their images.
4.1 Step-by-Step: Docker Workflow
Step 1. Search for Images on Docker Hub
docker search <image_name>
Step 2. Pull an image from Docker Hub
docker pull <image_name>
Step 3. Check Containers (None Created Yet)
docker ps -a
Step 4. Create a Container from the Image
docker create <image_name>
Step 5. Verify Container is Created
docker ps -a
Step 6. Start the Container
docker start <container_id or container_name>
Step 7. Confirm Container is Running
docker ps -a
Step 8. Pause the Container (Optional)
docker pause <container_id>
Step 9. Stop the container
docker stop <container_id>
Step 10. Remove the container
docker rm <container_id>
Step 11. Verify Container is Removed
docker ps -a
Running First Container
This document provides a list of basic Docker CLI commands used in the lecture.
Check Docker Version
|
|
Run a Test Container: Run a test container using the hello-world image to verify Docker installation.
|
|
Pull a Docker Image: Pull the hello-world image from Docker Hub.
|
|
Check running containers: To show all currently running containers.
|
|
Check all containers: Show all containers, both running and stopped.
|
|
List available images: List all Docker images downloaded and available locally.
|
|
Packaging the Spring Boot Web App
This document provides a list of basic Docker CLI commands used in the lecture.
Step 1: Package the Project Using Maven
|
|
Step 2: Run the JAR File
|
|
Running SpringBoot App on Docker
Step 1: Check Running Containers
|
|
Step 2. List All Files in the Container (JDK Environment)
|
|
Lists all folders and files in the container’s root directory.
Step 3. Check Contents of /tmp Directory"
|
|
It will contain only one file in /tmp at the initial stage.
Step 4. Copy the Spring Boot JAR File into the Container
|
|
This copies the rest-demo.jar into the container’s /tmp directory.
Step 5. Verify the JAR File is Present
|
|
The rest-demo.jar file will be available in addition to the existing content.
Step 6. Commit the Container to Create a New Docker Image
|
|
Creates a new Docker image named telusko/rest-demo with tag v1 from the current container state.
Step 7. List Docker Images
|
|
Verifies that telusko/rest-demo:v1 image has been created successfully.
Step 8. Default Behavior: JShell
|
|
When running telusko/rest-demo:v1, the container defaults to JShell.
Step 9. Set Default Command to Run JAR Using –change
|
|
This sets the default command to run the JAR directly when the image is run.
Step 10. Run the Updated Image (v2)
|
|
This will now run the Spring Boot application from the JAR instead of entering JShell.
Step 11. Map Ports While Running the Container"
|
|
Maps port 8081 of the container to 8081 on the host machine.
- Official docs: https://docs.docker.com
- Dockerfile reference: https://docs.docker.com/engine/reference/builder/
- Docker Compose: https://docs.docker.com/compose/
- Image scanning: https://github.com/aquasecurity/trivy
Thanks for reading!
Running JDK Docker Container
This document provides a list of basic Docker CLI commands used in the lecture.
JShell
- JShell is a REPL (Read-Eval-Print Loop) for Java introduced in Java 9.
- It allows running Java code interactively, without needing to compile and run entire class files.
Pull OpenJDK Docker Image
1. Search for the OpenJDK image on Docker Hub
|
|
2. Pull a specific OpenJDK Image
|
|
3.Check Available Images
|
|
4. Run the OpenJDK image
|
|
5. Run OpenJDK image in interactive mode
|
|
The
-itflag starts the container in interactive terminal mode.
6. Check Running Containers
|
|
Enter JShell inside the Container
|
|
Make sure the openjdk image version you pull supports jshell
Docker File for Docker Images
Build the Docker Image, Navigate to the directory that contains your Dockerfile and run:
|
|
List Available Docker Images
|
|
Run the Docker Image with Port Mapping
|
|
Docker Compose
This document provides a list of basic Docker CLI commands used in the lecture.
Clean and Package the Application: Use Maven to clean and create a JAR package of the Spring Boot project:
|
|
Build and Start Docker Containers: Use Docker Compose to build the images and start the containers:
|
|
This will use the docker-compose.yml file to build the application.
List Docker Images
|
|
Running Multiple Containers
This document provides a list of basic Docker CLI commands used in the lecture.
Stop and Remove All Running Containers
|
|
Clean and Package the Spring Boot App
|
|
Build and Start Containers:
|
|
Check Running Containers:
|
|
View Docker Networks: To list all Docker networks on your system:
|
|
Sm0king B!ts