Skip to content

Dockerize a Simple Application

Beginner ~20 min

What you'll build

A Docker image for a simple Node.js web application that can be built and run anywhere with a single command.

Goals

  • Understand the structure of a Dockerfile
  • Learn to build a Docker image from a Dockerfile
  • Run a container from the image and access the application
  • Understand image layering and caching

Prerequisites

  • Basic command line skills
  • Docker installed on your machine
  • A simple Node.js application (we provide a starter)

Steps

  1. 1

    Create a simple Node.js app

    Create a basic Express.js server that responds with 'Hello from Docker!' on port 3000. Create package.json and app.js files.

    Code
    // app.js
    const express = require('express');
    const app = express();
    app.get('/', (req, res) => res.send('Hello from Docker!'));
    app.listen(3000, () => console.log('Server running on port 3000'));

    Expected result

    You have a working Node.js app that runs locally with node app.js.

  2. 2

    Write the Dockerfile

    Create a file named Dockerfile (no extension) that starts from the official Node.js image, sets a working directory, copies package files, installs dependencies, copies the app code, and defines the start command.

    Code
    FROM node:18-alpine
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    EXPOSE 3000
    CMD ["node", "app.js"]

    Expected result

    A Dockerfile exists in your project root that describes how to build the image.

  3. 3

    Build the Docker image

    Use the docker build command to create an image from your Dockerfile. Tag it with a name so you can reference it later.

    Code
    docker build -t my-node-app .

    Expected result

    Docker builds the image layer by layer and shows a success message. Run docker images to see it.

  4. 4

    Run the container

    Use docker run to start a container from your image. Map port 3000 from the container to port 3000 on your host so you can access it in a browser.

    Code
    docker run -p 3000:3000 my-node-app

    Expected result

    Open http://localhost:3000 in your browser and see 'Hello from Docker!'

  5. 5

    Add a health check endpoint

    Add a /health endpoint returning 200 OK for orchestration systems (Kubernetes, load balancers) to probe liveness. Health checks are foundational for production deployment.

    Code
    # app.py (Flask example)
    from flask import Flask, jsonify
    app = Flask(__name__)
    
    @app.route("/health")
    def health():
        return jsonify({"status": "ok"}), 200
    
    @app.route("/ready")
    def ready():
        # Check DB connection etc.
        return jsonify({"ready": True}), 200

    Expected result

    curl localhost:5000/health returns {"status":"ok"} with a 200 status code.

Try it yourself

What you learned

You learned how to containerize an application with Docker: writing a Dockerfile, building an image, and running a container. This is the foundation of all container-based DevOps workflows.

Related Resources

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic