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:

  1. Download the image
  2. Create container
  3. Execute container
  4. 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:

  1. Build JAR
  2. Create Dockerfile
  3. Build image
  4. 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



This site uses Just the Docs, a documentation theme for Jekyll.