Project 1 โ€บ 02 Write a basic single-stage DockerfileGitHub โ†—
๐Ÿค– AI Prompt โ€” stuck? paste this

โ€œWrite a simple single-stage Dockerfile for a Node.js 20 Express app whose entry point is index.js on port 3000. Add a comment explaining each line.โ€

02 โ€” Write a basic single-stage Dockerfile

Category: Dockerfile ยท Priority: High ยท Step: 3

Get it working first. Use the official node:20 (or python:3.12) base image.

๐ŸŽฏ What is this task?

A Dockerfile is a recipe that tells Docker how to package your app into a container. Think of it like a shopping list + cooking instructions โ€” Docker reads it top to bottom and builds an image. In this task, you write a simple first version.

๐Ÿค– AI Prompt

"Write a simple single-stage Dockerfile for a Node.js Express app. The app entry point is index.js, it runs on port 3000, and uses Node.js 20. Include comments explaining each line."

๐Ÿ“‹ Step-by-step

Step 1 โ€” Make sure Docker is installed

docker --version

You should see something like Docker version 24.x.x.

Step 2 โ€” Create the Dockerfile

In your project folder (my-docker-app), create a new file called exactly Dockerfile (no extension):

# Use the official Node.js 20 image as the base
FROM node:20

# Set the working directory inside the container
WORKDIR /app

# Copy package.json first (so Docker can cache the npm install step)
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the app files
COPY . .

# Tell Docker this app listens on port 3000
EXPOSE 3000

# The command to run when the container starts
CMD ["node", "index.js"]

Step 3 โ€” Build the image

docker build -t my-docker-app .
  • -t my-docker-app gives your image a name (tag)
  • The . at the end means "look for the Dockerfile in the current folder"

You'll see Docker downloading the Node.js base image and running each step. This is normal and only takes long on the first run.

Step 4 โ€” Run the container

docker run -p 3000:3000 my-docker-app
  • -p 3000:3000 maps port 3000 on your computer to port 3000 inside the container

Open http://localhost:3000 in your browser โ€” it should work exactly like before!

Step 5 โ€” Stop the container

Press Ctrl+C, or open a new terminal and run:

docker ps
docker stop <container_id>

โœ… Done when

  • Dockerfile exists in your project folder
  • docker build -t my-docker-app . completes without errors
  • docker run -p 3000:3000 my-docker-app starts the server
  • http://localhost:3000 responds in the browser

๐Ÿ“ Quiz

Answer all 3 questions correctly to complete this lesson.

1. What does a Dockerfile describe?
2. Why COPY package*.json before COPY . . ?
3. What does `docker build -t my-docker-app .` do?
โ† PreviousPass the quiz to completeNext โ†’