Package your own app into an image — write a Dockerfile for a Node app step by step, build it with docker build, run it, and keep junk out of the image with a .dockerignore.
A Dockerfile is the recipe for building your image — each line is a step. FROM picks a base image (Node on a tiny Alpine Linux). WORKDIR sets the folder inside the image. You copy package files and install first (more on why next lesson), then copy the rest of the code. EXPOSE documents the port, and CMD is the command that runs when the container starts.
# Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]docker build turns the Dockerfile into an image. -t names ("tags") it, and the . tells Docker to build using the current folder as the context. Then docker run starts a container from your image, publishing the port so you can open it in a browser.
Build the image and tag it "myapp"
docker build -t myapp .Run it, mapping the app's port 3000 to localhost:3000
docker run -p 3000:3000 myappCOPY . . would copy everything — including node_modules and your .git history — into the build, making it slow and bloated. A .dockerignore file (same idea as .gitignore) excludes those so the build context stays small and your image only contains what it needs.
# .dockerignore
node_modules
npm-debug.log
.git
.env
Dockerfile
.dockerignore