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.
Architecture Flow
| Tool | Role |
|---|---|
| Java 17 | Application runtime |
| Apache Maven | Build automation tool |
| Jenkins | CI/CD automation server |
| Docker | Containerization platform |
| GitHub | Source code repository |
| AWS EC2 Ubuntu | Cloud 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
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.
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
| Command | Description |
|---|---|
docker pull <image> | Download image from Docker Hub |
docker images | List all local images |
docker run -it <image> | Run container interactively |
docker run -d -p 8090:8080 <image> | Run in background with port mapping |
docker ps | List running containers |
docker ps -a | List 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> bash | Enter a running container |
Docker & Containers โ Notes PDF
Week 7 & 8 ยท Containerization, Dockerfile, OS Image Management