Contents

Docker — Containers Simplified

Docker packages applications and their dependencies into portable containers so they run consistently across environments.

Note
Use the table of contents to jump to the section you need.

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>

Note
  • 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

1
docker --version

Run a Test Container: Run a test container using the hello-world image to verify Docker installation.

1
docker run hello-world

Pull a Docker Image: Pull the hello-world image from Docker Hub.

1
docker pull hello-world

Check running containers: To show all currently running containers.

1
docker ps

Check all containers: Show all containers, both running and stopped.

1
docker ps -a

List available images: List all Docker images downloaded and available locally.

1
docker images

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

1
mvn package

Step 2: Run the JAR File

1
java -jar target/rest-demo.jar

Running SpringBoot App on Docker

Step 1: Check Running Containers

1
docker ps

Step 2. List All Files in the Container (JDK Environment)

1
docker exec container_name ls -a

Lists all folders and files in the container’s root directory.

Step 3. Check Contents of /tmp Directory"

1
docker exec container_name ls /tmp 

It will contain only one file in /tmp at the initial stage.

Step 4. Copy the Spring Boot JAR File into the Container

1
docker cp target/rest-demo.jar container_name:/tmp 

This copies the rest-demo.jar into the container’s /tmp directory.

Step 5. Verify the JAR File is Present

1
docker exec container_name ls /tmp 

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

1
docker commit container_name telusko/rest-demo:v1

Creates a new Docker image named telusko/rest-demo with tag v1 from the current container state.

Step 7. List Docker Images

1
docker images 

Verifies that telusko/rest-demo:v1 image has been created successfully.

Step 8. Default Behavior: JShell

1
docker run telusko/rest-demo:v1 

When running telusko/rest-demo:v1, the container defaults to JShell.

Step 9. Set Default Command to Run JAR Using –change

1
docker commit --change='CMD ["java", "-jar", "/tmp/rest-demo.jar"]' container_name telusko/rest-demo:v2 

This sets the default command to run the JAR directly when the image is run.

Step 10. Run the Updated Image (v2)

1
docker run telusko/rest-demo:v2 

This will now run the Spring Boot application from the JAR instead of entering JShell.

Step 11. Map Ports While Running the Container"

1
docker run -p 8081:8081 telusko/rest-demo:v2

Maps port 8081 of the container to 8081 on the host machine.

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

1
docker search openjdk

2. Pull a specific OpenJDK Image

1
docker pull openjdk:22-jdk

3.Check Available Images

1
docker images

4. Run the OpenJDK image

1
docker run openjdk:22-jdk

5. Run OpenJDK image in interactive mode

1
docker search openjdk

The -it flag starts the container in interactive terminal mode.

6. Check Running Containers

1
docker ps

Enter JShell inside the Container

1
2
3
4
jshell
int num = 8;
System.out.println("Hello World");
/exit  #to exit from JShell

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:

1
2
docker build -t telusko/rest-demo:v3 .
# This builds a Docker image named telusko/rest-demo with tag v3 using the current directory (.) as the build context.

List Available Docker Images

1
docker images

Run the Docker Image with Port Mapping

1
2
docker run -p 8081:8081 telusko/rest-demo:v3
# This runs the container from the newly built image and maps port 8081 of the host to port 8081 of the container.

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:

1
mvn clean package

Build and Start Docker Containers: Use Docker Compose to build the images and start the containers:

1
docker-compose up --build

This will use the docker-compose.yml file to build the application.

List Docker Images

1
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

1
docker-compose down

Clean and Package the Spring Boot App

1
mvn clean package -DskipTests

Build and Start Containers:

1
docker-compose up --build

Check Running Containers:

1
docker ps

View Docker Networks: To list all Docker networks on your system:

1
docker network ls