Shruti_Containerization_lab

Experiment 3: Docker – NGINX & Web Application Containerization

This repository contains Experiment-3, demonstrating Docker fundamentals through NGINX deployment, custom image creation, image comparison, volume usage, and preparation of a sample Flask web application for containerization.


Objective

To understand and implement Docker containerization concepts by:


Experiment Structure


EXPERIMENT-3/
│
├── Part 1/                # Official NGINX image
├── Part 2/                # Custom NGINX (Ubuntu base)
├── Part 3/                # Custom NGINX (Alpine base)
├── Part 4/                # Image comparison
├── Part 5/                # Custom HTML using volumes
├── Experiment 3 Part 2/
│   └── flask-app/         # Sample Flask web application
│       ├── app.py
│       └── requirements.txt
└── Readme.md


Part 1: Running Official NGINX Image

Pull Image

docker pull nginx:latest

Run Container

docker run -d -p 8080:80 --name exp3-nginx nginx

Verify

curl http://localhost:8080

Default NGINX welcome page is displayed.


Part 2: Custom NGINX Using Ubuntu Base Image

Dockerfile

FROM ubuntu:22.04

RUN apt-get update && \
    apt-get install -y nginx && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Build Image

docker build -t nginx-ubuntu .

Run Container

docker run -d -p 8081:80 --name nginx-ubuntu nginx-ubuntu

Custom Ubuntu-based NGINX image successfully created.


Part 3: Custom NGINX Using Alpine Base Image

Dockerfile

FROM alpine:latest

RUN apk add --no-cache nginx

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Build Image

docker build -t nginx-alpine .

Run Container

docker run -d -p 8082:80 --name nginx-alpine nginx-alpine

Observation

docker images nginx-alpine

Alpine-based image created (~16MB), demonstrating lightweight containers.


Part 4: Image Size & Layer Comparison

Compare Images

docker images | findstr nginx

Inspect Layers

docker history nginx
docker history nginx-ubuntu
docker history nginx-alpine

Observations


Part 5: Serving Custom HTML Using Docker Volumes

Create HTML File

mkdir html
echo "<h1>Hello from Docker NGINX</h1>" > html/index.html

Run Container with Volume Mapping (Windows CMD)

docker run -d -p 8083:80 -v "%cd%\html:/usr/share/nginx/html" nginx

Custom HTML page served successfully via NGINX.


Sample Web Application (Flask – Pre-Containerization)

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, Docker!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

requirements.txt

flask

Run Locally

python app.py

Access:

http://localhost:5000

Application tested locally before containerization.


Tech Used