• Explore
  • About Us
  • Log In
  • Get Started
  • Explore
  • About Us
  • Log In
  • Get Started

Writing a Dockerfile for a Node.js App

Scenario

You need to containerize a simple Node.js microservice. The application code is available in a GitHub repository. Your task is to clone this repository and create a Dockerfile that efficiently builds a production-ready image for the application.

Requirements

  1. Clone the provided GitHub repository: https://github.com/tdevsin/nodejs-app.git.
  2. Navigate into the cloned repository's directory.
  3. Read the README.md file in the repository to understand the application structure.
  4. Write a Dockerfile in the root of the cloned repository to containerize the Node.js application.
  5. The Dockerfile should use an official Node.js base image.
  6. It should copy package.json and package-lock.json first to leverage Docker's build cache.
  7. Install application dependencies.
  8. Copy the rest of the application code.
  9. Expose the port on which the application listens.
  10. Define the command to start the application.

Acceptance Criteria:

  • A Docker image named nodejs-docker-app is successfully built.
  • The container runs without errors and the application is accessible on the exposed port.
  • The image size is reasonable for a Node.js application.

Resources

  • Provided Application Repository
  • Dockerfile reference
  • Best practices for writing Dockerfiles

Possible Ways to Implement

  • Start with a minimal Node.js base image (e.g., node:18-alpine).
  • Use WORKDIR to set the working directory inside the container.
  • Copy package.json and package-lock.json before running npm install.
  • Use COPY . . to copy the remaining application code.
  • Use EXPOSE to document the port.
  • Use CMD to define the default command to run the application.

Real-World Significance

Containerizing applications with Docker is a fundamental skill in modern software development and DevOps. A well-crafted Dockerfile ensures that your application runs consistently across different environments, from development to production. Understanding how to optimize Dockerfiles for build caching and minimal image size is crucial for efficient CI/CD pipelines and faster deployments, directly impacting development velocity and operational costs. This exercise lays the groundwork for deploying applications to Kubernetes, where container images are the primary deployment unit.

    CKAD Practice Exercises

    Unlock All Exercises

  • Application Design and Build
    • Writing a Dockerfile for a Node.js App
    • Optimizing Images with Multi-Stage Builds
    • Understanding Image Layer Caching
    • Running a One-Off Database Migration with a Job
    • Scheduling a Nightly Backup with a CronJob
    • Managing Job Completion and Failure Policies
  • Application Deployment
    • Creating and Inspecting Deployments
    • Scaling a Deployment to Handle More Traffic
    • Performing a Rolling Update on a Deployment
    • Rolling Back a Failed Deployment
    • Controlling the Speed of a Rolling Update
    • Implementing a Blue/Green Deployment Strategy
    • Implementing a Canary Release Strategy
  • Application Observability
    • Configuring Liveness Probes to Restart Unhealthy Containers
    • Configuring Readiness Probes to Manage Application Traffic
    • Using Startup Probes for Slow-Starting Applications
    • Accessing and Streaming Container Logs
    • Using a Logging Sidecar Container
    • Debugging a Pod in a Pending State
    • Debugging a Crashed Pod Using Logs and Events
    • Using Ephemeral Containers for Live Debugging
  • Application Environment, Configuration, and Security
    • Creating ConfigMaps from Literals and Files
    • Injecting ConfigMap Data as Environment Variables
    • Mounting a ConfigMap as a Volume
    • Managing Application Secrets
    • Using Secrets as Environment Variables and Files
    • Understanding and Assigning ServiceAccounts to Pods
    • Configuring Resource Requests and Limits
    • Understanding Quality of Service (QoS) Classes
    • Interacting with Custom Resources (CRDs)
  • Services & Networking
    • Exposing a Deployment Internally with a ClusterIP Service
    • Exposing a Service Externally with a NodePort Service
    • Creating a Headless Service for Stateful Apps
    • Creating a Default Deny Network Policy
    • Allowing Traffic Between Pods Using Network Policies
    • Controlling Ingress Traffic from Specific Namespaces
    • Creating a Basic Ingress Resource for a Service
    • Configuring Host-Based Routing with Ingress
    • Configuring Path-Based Routing with Ingress