Containerization-and-DevOps

Deploying NGINX Using Different Base Images and Comparing Image Layers


Lab Objectives

After completing this lab, students will be able to:


Prerequisites


Step 1: Pull the Image

docker pull nginx:latest

Image 1

Step 2: Run the Container

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

Image 2

Step 3: Verify

curl http://localhost:8080

Image 3

You should see the NGINX welcome page.


Key Observations

docker images nginx

Image 4


Part 2: Custom NGINX Using Ubuntu Base Image

Step 1: Create Dockerfile

Image 6

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;"]

Image 7

Step 2: Build Image

docker build -t nginx-ubuntu .

Image 8

Step 3: Run Container

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

Image 8

Observations

docker images nginx-ubuntu

Image 8


Part 3: Custom NGINX Using Alpine Base Image

Step 1: Create Dockerfile

Image 8

FROM alpine:latest

RUN apk add --no-cache nginx

EXPOSE 80

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

Image 8

Step 2: Build Image

docker build -t nginx-alpine .

Image 8

Step 3: Run Container

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

Image 9


Observations

docker images nginx-alpine

Image 10


Part 4: Image Size and Layer Comparison

Compare Sizes

docker images | grep nginx

Image 11

Typical result (approx):

Image Type Size
nginx:latest ~140 MB
nginx-ubuntu ~220+ MB
nginx-alpine ~25–30 MB

Observations:


Part 5: Functional Tasks Using NGINX

Task 1: Serve Custom HTML Page

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

Image 13

Run:

docker run -d \
  -p 8083:80 \
  -v $(pwd)/html:/usr/share/nginx/html \
  nginx

Image 14