dockerfile bash 73 lines · 2 tabs

Docker fundamentals: images, containers, and layers

Ryan Nakamura Feb 2026
2 tabs
# Multi-stage build for a Node.js application

# Stage 1: Build
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files first (leverage layer caching)
COPY package.json package-lock.json ./
RUN npm ci --only=production

# Copy source code (changes more frequently)
COPY src/ ./src/
COPY tsconfig.json ./

RUN npm run build

# Stage 2: Production
FROM node:20-alpine AS production

# Create non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

WORKDIR /app

# Copy only production artifacts
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

# Set ownership
RUN chown -R appuser:appgroup /app

USER appuser

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3   CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1

CMD ["node", "dist/server.js"]
2 files · dockerfile, bash Explain with highlit

Docker packages applications into lightweight, portable containers. A Dockerfile defines build instructions—each instruction creates an immutable layer. The FROM directive sets the base image. COPY and ADD bring files into the image. RUN executes commands during build. CMD and ENTRYPOINT define the container's startup command. The docker build command creates images from Dockerfiles. docker run starts containers from images. Layer caching dramatically speeds up builds—order instructions from least to most frequently changing. Multi-stage builds reduce final image size by separating build and runtime dependencies. Use .dockerignore to exclude unnecessary files. Tag images with semantic versions, never rely solely on latest. Understanding the image layer system is key to writing efficient Dockerfiles.


Related snips

javascript
// Express app with health checks and graceful shutdown
const express = require('express');
const { createServer } = require('http');

const app = express();
const server = createServer(app);

Container health checks and graceful shutdown patterns

docker kubernetes health-checks
by Ryan Nakamura 1 tab
javascript
// k6 Load Test Configuration
// Run: k6 run load-test.js --env BASE_URL=https://api.example.com

import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend, Counter } from 'k6/metrics';

Load testing APIs with k6 for performance validation

k6 load-testing performance
by Ryan Nakamura 1 tab
hcl
# Using the module

module "api_service" {
  source = "./modules/ecs_service"

  service_name       = "api"

Terraform modules for reusable infrastructure

terraform modules iac
by Ryan Nakamura 2 tabs
bash
#!/usr/bin/env bash
set -euo pipefail

# ==========================================================
# Production Incident Response Runbook
# ==========================================================

Incident response runbook and diagnostic scripts

incident-response sre production
by Ryan Nakamura 1 tab
yaml
- name: Build image
  run: docker build -t app:${{ github.sha }} .

- name: Scan image
  uses: aquasecurity/trivy-action@0.24.0
  with:

Trivy image scanning in pull request pipelines

trivy containers ci
by Kai Nakamura 1 tab
swift
import UIKit

class ImageCache {
    static let shared = ImageCache()

    private let cache = NSCache<NSString, UIImage>()

Image caching with NSCache and async loading

swift swiftui images
by Sofia Martinez 2 tabs

Share this code

Here's the card — post it anywhere.

Docker fundamentals: images, containers, and layers — share card
Link copied