Labs · DevOps
Lab: Write a Dockerfile for a Small Node.js App, Build the Image, Run a Container, Tag It and Read Its Layers
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.0built 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 versionshows Client and Server. - A terminal and
curl(or a browser). - 40 minutes.
Part 1: The app and the Dockerfile
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" } } EOFCreate
.dockerignoreso junk and secrets never go into the image:printf "node_modules\n.env\n.git\n*.log\n" > .dockerignoreCreate 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 noderuns the app as the built-in non-root user of the Node image.
Part 2: Build and run
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]andnaming to docker.io/library/badge-api:1.0.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 psWhat you should see: one row with
0.0.0.0:3000->3000/tcpand statusUp.Test it:
curl -s http://localhost:3000What you should see:
{"app":"badge-api","version":"1.0"}Read the logs and check the user:
docker logs badge docker exec badge whoamiWhat you should see:
listening on 3000andnode(not root).
Part 3: One image, three names, and its layers
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-apiWhat you should see: the same IMAGE ID on every line.
Read how the image was built, layer by layer:
docker history badge-api:1.0What you should see: one line per Dockerfile instruction, with the size each added.
Optional: push to your own Docker Hub account (after
docker login):docker push YOURDOCKERHUBUSER/badge-api:1.0
Clean up
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 -aWhat you should see: no
badgecontainer 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
| Mistake | What happens | Fix |
|---|---|---|
port is already allocated | The container does not start | Stop the other app or use -p 3001:3000 |
Forgetting the dot in docker build | requires exactly 1 argument | Run docker build -t badge-api:1.0 . |
Putting a .env with real passwords in the image | Anyone who pulls the image can read it | Add .env to .dockerignore; pass values at run time |
| Running everything as root | A bug in the app gets root inside the container | Add 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.