Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Write a Dockerfile for a Small Node.js App, Build the Image, Run a Container, Tag It and Read Its Layers

Beginner40 minDocker Desktop or Docker Engine · Terminal · curl

Course: DevOps · Chapter 6: Why Docker, Images and How a Container Is Created · Lesson: 6.12 Lab: one image, three names

Chapter 6 explains images, containers and the four ways to create a container; this lab uses the most important one, docker build.

Chala mitrano! "Maazya laptop var chalta" he vaakya aaj band. Ek image banvuya, ji sagLikade sarkhich chalel.

Suppose we are…

Suppose we are on an Infosys team shipping a small Node.js badge API. On the developer's laptop it works; on the test server it fails because Node is a different version. A Docker image packs the app with its exact runtime, so the same image runs everywhere.

In this lab we write a Dockerfile, build an image, run it as a container, give it version tags and look inside its layers.

Goal of this lab

By the end you will have:

  • An image badge-api:1.0 built from your own Dockerfile.
  • A running container answering on http://localhost:3000.
  • The same image with three names, and a container that runs as a non-root user.

What you need

  • Docker Desktop (Windows or Mac) or Docker Engine (Linux). docker version shows Client and Server.
  • A terminal and curl (or a browser).
  • 40 minutes.

Part 1: The app and the Dockerfile

  1. Create a folder with two app files:

    mkdir badge-api && cd badge-api
    cat > server.js <<'EOF'
    const http = require("http");
    const port = process.env.PORT || 3000;
    http.createServer((req, res) => {
      res.end(JSON.stringify({ app: "badge-api", version: process.env.APP_VERSION || "dev" }) + "\n");
    }).listen(port, () => console.log("listening on " + port));
    EOF
    cat > package.json <<'EOF'
    { "name": "badge-api", "version": "1.0.0", "main": "server.js", "scripts": { "start": "node server.js" } }
    EOF
    
  2. Create .dockerignore so junk and secrets never go into the image:

    printf "node_modules\n.env\n.git\n*.log\n" > .dockerignore
    
  3. Create the Dockerfile:

    FROM node:20-alpine
    WORKDIR /app
    COPY package.json ./
    RUN npm install --omit=dev
    COPY server.js ./
    ENV APP_VERSION=1.0
    EXPOSE 3000
    USER node
    CMD ["node", "server.js"]
    

    USER node runs the app as the built-in non-root user of the Node image.

Part 2: Build and run

  1. Build the image. The dot means "this folder is the build context":

    docker build -t badge-api:1.0 .
    

    What you should see: steps [1/5] to [5/5] and naming to docker.io/library/badge-api:1.0.

  2. Run a container in the background, mapping laptop port 3000 to container port 3000:

    docker run -d --name badge -p 3000:3000 badge-api:1.0
    docker ps
    

    What you should see: one row with 0.0.0.0:3000->3000/tcp and status Up.

  3. Test it:

    curl -s http://localhost:3000
    

    What you should see: {"app":"badge-api","version":"1.0"}

  4. Read the logs and check the user:

    docker logs badge
    docker exec badge whoami
    

    What you should see: listening on 3000 and node (not root).

Part 3: One image, three names, and its layers

  1. Add two more tags. Tags are names; no copy is made:

    docker tag badge-api:1.0 badge-api:latest
    docker tag badge-api:1.0 YOURDOCKERHUBUSER/badge-api:1.0
    docker images badge-api
    

    What you should see: the same IMAGE ID on every line.

  2. Read how the image was built, layer by layer:

    docker history badge-api:1.0
    

    What you should see: one line per Dockerfile instruction, with the size each added.

  3. Optional: push to your own Docker Hub account (after docker login):

    docker push YOURDOCKERHUBUSER/badge-api:1.0
    

Clean up

  1. Stop and remove the container and the images:

    docker rm -f badge
    docker rmi badge-api:1.0 badge-api:latest YOURDOCKERHUBUSER/badge-api:1.0
    docker ps -a
    

    What you should see: no badge container left.

Ravindra Bagale's Tip

Copy package.json and run npm install before copying the code. Then a code change does not reinstall every package, and builds stay fast.

Common mistakes

MistakeWhat happensFix
port is already allocatedThe container does not startStop the other app or use -p 3001:3000
Forgetting the dot in docker buildrequires exactly 1 argumentRun docker build -t badge-api:1.0 .
Putting a .env with real passwords in the imageAnyone who pulls the image can read itAdd .env to .dockerignore; pass values at run time
Running everything as rootA bug in the app gets root inside the containerAdd USER node (or another non-root user)

Self-check checklist

0 of 5 done

Try-at-home challenge

Build a 1.1 image with --build-arg or a changed APP_VERSION, run it on port 3001 next to 1.0, and curl both. Then check the image size with docker images and compare node:20 with node:20-alpine.

Samajla ka? One Dockerfile, one image, runs the same everywhere. Aata pudhe jaauya Docker Compose.