Skillversitas

Docker Images

A Docker image is a read-only template for creating containers. An image contains everything needed to run an application.

Image Structure

Image Layer Structure:
+-------------------+
|   Application     |  <- Layer 4
+-------------------+
|   Dependencies    |  <- Layer 3
+-------------------+
|   Runtime         |  <- Layer 2
+-------------------+
|   Base OS         |  <- Layer 1
+-------------------+

Basic Image Commands

# List all images
docker images

# Pull an image from a registry
docker pull ubuntu:22.04

# Delete an image
docker rmi image_name

# Delete all unused images
docker image prune

Creating an Image from a Container

# Run a container
docker run -it ubuntu bash

# Install something inside the container
apt-get update && apt-get install -y nginx

# Commit the changes into a new image
docker commit <container_id> my-nginx:1.0

Building an Image with a Dockerfile

# Build an image from a Dockerfile
docker build -t myapp:1.0 .

# Build with a specific tag
docker build -t username/myapp:latest .

Managing Images

# View image details
docker inspect ubuntu:22.04

# View image history
docker history ubuntu:22.04

# Copy an image to another registry
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0

Multi-Stage Build

# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build

# Stage 2: Production
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Use Case: Application Image

# Dockerfile for the application
FROM node:18-alpine

WORKDIR /app

# Install dependencies
COPY package*.json ./
RUN npm ci --only=production

# Copy source code
COPY . .

# Environment variables
ENV NODE_ENV=production
ENV DB_HOST=database
ENV DB_PORT=5432

EXPOSE 3000

CMD ["node", "server.js"]

Image Tagging

# Tag with a version
docker build -t myapp-app:1.0.0 .
docker build -t myapp-app:1.0 .
docker build -t myapp-app:latest .

# Push all tags
docker push myapp-app:1.0.0
docker push myapp-app:1.0
docker push myapp-app:latest

Everyday Use Case

Case: A developer needs to rebuild the image when the code changes.

# Rebuild the image
docker build -t myapp-api:latest .

# Stop the old container
docker stop myapp-api

# Run the new container
docker run -d \
  --name myapp-api \
  -p 3000:3000 \
  myapp-api:latest

Best Practices

  1. Use a minimal base image: Alpine Linux for a smaller size
  2. Order instructions: Put COPY package*.json before COPY . for caching
  3. Multi-stage build: To separate build and runtime
  4. Delete unneeded files: Use a .dockerignore
  5. Use specific tags: Avoid latest in production

Use Case: Image Optimization

# Before (large size)
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt

# After (smaller size)
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

Troubleshooting

Image not showing up

# Make sure the image has been built
docker images

# If not, rebuild it
docker build -t myapp:latest .

"image not found" error

# Check the correct image name
docker images | grep myapp

# Pull the image if it comes from a registry
docker pull myapp:latest

Conclusion

Docker images are the foundation of containers. Understanding how to create, manage, and optimize images is essential for efficient application deployment.