โ† Back to DevOps
ยฉ 2025 sampathirao yoganandh
Week 7 & 8 ยท CO-3

Docker & Containers

Docker Deployment, Containerization, OS Image Management, Jenkins + Docker CI/CD  CO-3

What is Docker?

Docker is a containerization platform that allows applications to run in isolated environments called containers. Instead of installing Apache Tomcat directly on the server, Docker packages Tomcat and the application together inside a container image. This ensures consistency, portability, and easier deployment across different environments.

Why Docker? Containers are lightweight, portable, and share the host system's kernel while maintaining separate environments. "It works on my machine" problems are eliminated โ€” the container is identical everywhere.

Architecture Flow

Docker CI/CD Pipeline GitHub โ†’ Jenkins โ†’ Maven Build โ†’ Docker Image Build โ†’ Docker Container โ†’ Browser
ToolRole
Java 17Application runtime
Apache MavenBuild automation tool
JenkinsCI/CD automation server
DockerContainerization platform
GitHubSource code repository
AWS EC2 UbuntuCloud server environment

Deploy Java Web Application with Docker

Aim: Deploy a Java Maven web application inside a Docker container instead of installing Apache Tomcat directly on the server.

Dockerfile

A Dockerfile is a text file containing instructions to build a Docker image. It defines the base image, copies application files, and sets the startup command.

FROM tomcat:10.1-jdk17

RUN rm -rf /usr/local/tomcat/webapps/*
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]

Step-by-Step Procedure

Step 1: Install Docker

sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

Step 2: Give Jenkins Permission to Use Docker

sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
Important Jenkins must be in the Docker group to run Docker commands during pipeline builds. Restart Jenkins after adding it to the group.

Step 3: Add Dockerfile to Project Repository

Create a file named Dockerfile in the root of your project and add the contents shown above.

Step 4: Update Jenkinsfile for Docker Deployment

Include pipeline stages to build WAR, build Docker image, remove old container, and run new container with port mapping.

Reference Repository Fork this repo for a complete Jenkinsfile and Dockerfile:
https://github.com/syoganandh/spring-boot-maven-example-helloworld.git

Step 5: Open AWS Security Group Port

Allow inbound traffic on port 8090 in your AWS EC2 Security Group settings.

Step 6: Run Jenkins Pipeline

Click Build Now in Jenkins to build the Docker image and start the container.

Step 7: Access Application

# Open in browser:
http://<EC2-IP>:8090/

OS Container Management

Aim: Install Docker and create containers using different OS images โ€” Ubuntu, Alpine, CentOS.

Download OS Images

docker pull ubuntu
docker pull alpine
docker pull centos
docker images

Run OS Containers

# Run Ubuntu container
docker run -it ubuntu /bin/bash

# Run Alpine Linux container (lightweight, ~5MB)
docker run -it alpine /bin/sh

# Run CentOS container
docker run -it centos /bin/bash

Manage Containers

# View running containers
docker ps

# View all containers (including stopped)
docker ps -a

# Stop a container
docker stop <container-id>

# Remove a container
docker rm <container-id>

# Remove an image
docker rmi <image-name>

Key Docker Commands

CommandDescription
docker pull <image>Download image from Docker Hub
docker imagesList all local images
docker run -it <image>Run container interactively
docker run -d -p 8090:8080 <image>Run in background with port mapping
docker psList running containers
docker ps -aList all containers
docker stop <id>Stop a running container
docker rm <id>Remove a container
docker build -t <name> .Build image from Dockerfile
docker exec -it <id> bashEnter a running container
๐Ÿ“„

Docker & Containers โ€” Notes PDF

Week 7 & 8 ยท Containerization, Dockerfile, OS Image Management

โฌ‡ Download PDF