Week 8 - Docker and Deployment
π Introduction
Modern software development is no longer limited to simply writing code. Backend developers today are also expected to understand:
- How applications are packaged
- How applications are deployed
- How environments are managed
- How applications run consistently across systems
This is where Docker becomes extremely important.
Docker helps developers package applications together with their dependencies into lightweight, portable containers that can run consistently across environments.
In this week, we will learn the fundamentals of Docker and how backend applications are deployed using containers.
π³ What is Docker?
Docker is a containerization platform that allows applications to run inside isolated environments called containers.
A container contains:
- Application code
- Runtime
- Libraries
- Dependencies
- Configuration
This ensures that the application behaves the same way everywhere.
β Why Do We Need Docker?
Before Docker, developers often faced the problem:
βIt works on my machine but not on the server.β
Different systems may have:
- Different operating systems
- Different Java versions
- Different libraries
- Different configurations
Docker solves this problem by packaging everything required for the application together.
π₯ Containers vs Virtual Machines
Virtual Machines
Virtual machines emulate complete operating systems.
Each VM contains:
- Full OS
- Kernel
- Libraries
- Application
This makes them heavy and resource intensive.
Containers
Containers share the host operating system kernel.
They are:
- Lightweight
- Faster
- Easier to start
- Easier to deploy
π¦ Important Docker Concepts
1. Docker Image
A Docker image is a read-only template used to create containers.
An image contains:
- Application code
- Runtime
- Dependencies
- Configuration
Example:
openjdk:17
This is a Java 17 image.
2. Docker Container
A running instance of a Docker image is called a container.
Image β Blueprint Container β Running application
3. Dockerfile
A Dockerfile is a text file that contains instructions for building Docker images.
Example:
FROM openjdk:17
COPY app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
4. Docker Hub
Docker Hub is a public repository where Docker images are stored.
Example images:
- MySQL
- PostgreSQL
- Redis
- Nginx
- OpenJDK
β Installing Docker
Windows
Install:
- Docker Desktop
Linux
Install:
sudo apt install docker.io
π Verifying Docker Installation
Run:
docker --version
Example output:
Docker version 27.x.x
π Running Your First Docker Container
Run:
docker run hello-world
Docker will:
- Download the image
- Create container
- Execute container
- Display message
π Common Docker Commands
List Running Containers
docker ps
List All Containers
docker ps -a
List Images
docker images
Stop Container
docker stop <container-id>
Remove Container
docker rm <container-id>
Remove Image
docker rmi <image-id>
π Understanding Dockerfiles
Dockerfiles define how images are built.
Example Dockerfile
FROM openjdk:17
WORKDIR /app
COPY target/student-app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Understanding Each Instruction
FROM
Defines base image.
FROM openjdk:17
WORKDIR
Sets working directory inside container.
WORKDIR /app
COPY
Copies files from host to container.
COPY target/student-app.jar app.jar
EXPOSE
Documents application port.
EXPOSE 8080
ENTRYPOINT
Defines startup command.
ENTRYPOINT ["java", "-jar", "app.jar"]
π Building Docker Images
Command:
docker build -t student-app .
Explanation:
| Part | Meaning |
|---|---|
| docker build | Build image |
| -t | Tag image |
| student-app | Image name |
| . | Current directory |
βΆ Running Docker Containers
Command:
docker run -p 8080:8080 student-app
Port Mapping
Format:
host-port:container-port
Example:
8080:8080
π Viewing Logs
docker logs <container-id>
Logs help debug issues.
π§ Executing Commands Inside Containers
docker exec -it <container-id> bash
This opens terminal inside container.
π± Spring Boot and Docker
Spring Boot applications are ideal for containerization because:
- Self-contained JARs
- Embedded servers
- Easy startup
Typical workflow:
- Build JAR
- Create Dockerfile
- Build image
- Run container
π Running Databases in Docker
Example MySQL container:
docker run -d \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=password \
mysql:8
Environment Variables
Environment variables allow configuration without modifying code.
Example:
-e MYSQL_ROOT_PASSWORD=password
π Docker Compose
Docker Compose helps run multi-container applications.
Example:
- Spring Boot application
- MySQL database
Together.
Example docker-compose.yml
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
mysql:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: password
Running Docker Compose
docker compose up
Stopping Docker Compose
docker compose down
π§ͺ Debugging Containers
Common issues:
| Problem | Cause |
|---|---|
| Application not starting | Wrong command |
| Port already used | Port conflict |
| Database connection failure | Wrong configuration |
| Image build failure | Incorrect Dockerfile |
π Best Practices
1. Keep Images Small
Use lightweight base images.
2. Avoid Hardcoding Secrets
Use environment variables.
3. Use Specific Image Versions
Good:
openjdk:17
Avoid:
latest
4. Keep Containers Stateless
Do not store important data inside containers.
5. Use Logs Properly
Logs are essential for debugging.
β Docker in Real Industry
Docker is widely used in:
- Cloud-native applications
- Kubernetes
- CI/CD pipelines
- Microservices
- DevOps workflows
Companies use Docker because it provides:
- Faster deployment
- Environment consistency
- Scalability
- Easier maintenance
π₯ Introduction to Kubernetes
Docker runs containers.
Kubernetes manages containers at scale.
Kubernetes helps with:
- Scaling
- High availability
- Self-healing
- Rolling updates
You will learn Kubernetes in future advanced topics.
π§ Summary
In this week, you learned:
- What Docker is
- Why containers are important
- Docker images and containers
- Dockerfiles
- Docker commands
- Spring Boot containerization
- Docker Compose basics
- Deployment fundamentals
These are foundational skills for modern backend engineering and cloud-native development.
π Recommended Practice
Practice repeatedly:
- Build images
- Run containers
- Stop containers
- Inspect logs
- Debug failures
- Modify Dockerfiles
The more hands-on practice you do, the easier deployment concepts will become.
π License
This project is licensed under the GNU GPL-3.0 License.
Maintained by Aditya Pratap Bhuyan LinkedIn: https://linkedin.com/in/adityabhuyan