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.
3. Key concepts
3.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.
3.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.
4. Main Components
Docker Engine: The runtime that builds, runs, and manages containers (via the Docker CLI and API).
Container: Containers are isolated processes for each of the app’s components. all the components like frontend react, backend python, postgres database run in the completely isolated environment.Containers are self-contained, isolated, independent and portable.
Image: To share the container we need image. A Container image is a standerized package that includes all of the files, binaries and configuration to run the container. THere are two imp principle of images
-
Images are immutable: once an image is created it cant be modifie. you can only make new image or add changes on top of it.
-
Container images are composed of layers. Each layer represents a set of file system changes that add, remove, or modify files.
Docker Hub Docker hub is the default global marketplace for storing and distributing images. it has more than 1 lac images created by the devs. we can dwnld and run the images.
Registry: An image registry is a centralized location for storing and sharing the container images. It can be either public or private.(Docker Hub, GitHub Container Registry, private registries).
Volume: Persistent storage mounted into containers.It is separate from a container’s lifecycle. It is used to store data so it doesn’t get lost when a container stops, restart or deleted.
Network: Network means a virtual communication layer that allows containers to talk to each other and to the outside world. It connects containers securely.
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.
5. Common Docker commands
Most Commonly used 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 |
6. 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.
6.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
7. Running First Container (Hello World)
Step 1. Check Docker Version
|
|
Step 2. Run a test container using the hello-world image to verify Docker installation.
|
|
Step 3. Pull the hello-world image from Docker Hub.
|
|
Step 4. Check all currently runnings containers.
|
|
|
|
Step 5. Show all containers, both running and stopped.
|
|
Step 6. ist all Docker images downloaded and available locally.
|
|
8. Running SpringBoot App on Docker
To run the spring boot web app on the docker we need the jar file. so 1st create the jar file then upload the jar file to the container.
8.1 Package the 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
|
|
8.2 Run the 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 n4ksum/rest-demo with tag v1 from the current container state.
Step 7. List Docker Images
|
|
Verifies that n4ksum/rest-demo:v1 image has been created successfully.
Step 8. Default Behavior: JShell
|
|
When running n4ksum/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.
9. Running JDK Docker Container
This document provides a list of basic Docker CLI commands used in the lecture.
9.1 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.
9.2 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
|
|
9.3 Enter JShell inside the Container
|
|
Make sure the openjdk image version you pull supports jshell
10 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
|
|
11. Docker Compose
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
|
|
12. 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:
|
|
- 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!
Sm0king B!ts