SkillSprout
Chapter 1 · Lesson 8
Beginner
2 min read

Build a Custom Image

Overview

A Dockerfile is a text file containing ordered instructions for building an image. Docker executes the instructions in sequence. FROM chooses the starting filesystem, COPY adds application files, RUN executes a build-time command, and CMD defines the default process when a container starts.

Detailed Notes

A simple Node.js Dockerfile

dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
  • FROM: start from a minimal image that already contains Node.js 22.
  • WORKDIR: create /app if needed and make it the current directory.
  • COPY package*.json ./: copy dependency files before all source files.
  • RUN npm ci: install exact dependencies during image build.
  • COPY . .: copy the remaining source.
  • EXPOSE: document that the app listens on port 3000.
  • CMD: run npm start when the container starts.

The JSON array form in CMD avoids an extra shell and makes process termination clearer.

Build and run:

bash
docker build -t my-node-app:1.0 .
docker run --rm -p 3000:3000 my-node-app:1.0
  • -t gives the image a tag.
  • . is the build context directory.
  • --rm removes the container after it stops.

Exercise

Create index.html and a Dockerfile:

dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80

Build it and visit port 8080.

Dockerfile to container
Architecture
DockerfileImageContainerdocker builddocker run
Key point. Docker reads instructions and the build context.

A Dockerfile is transformed into a reusable image, then instantiated as a container.

Common Interview Questions

Finished this lesson?

0 of 27 lessons done · 0% of the course