Build Images with a Dockerfile

Learn to write a Dockerfile, package a Node.js application into an image, and run it.

🎯 Introduction

So far, we have used images built by others, such as nginx and mysql. By the end of this article, you will be able to write your own Dockerfile, package a Node.js application into an image, and run it. Like the official software images, your application can then run with one command on a machine with Docker installed.


🧱 What Is a Dockerfile?

A Dockerfile is a text file containing instructions for building an image step by step.

Think of a recipe:

  • A recipe lists ingredients and steps in order. Follow it to make a dish.
  • A Dockerfile specifies a base image, files to copy, and commands to execute. Follow its instructions to build an image.

A Dockerfile turns a build from manually performed steps passed along by word of mouth into a documented, repeatable process. Anyone with the file can follow the same process to build the image.


🛠 Prepare a Node.js Application

Start with a minimal Node.js application. This article was written and tested with Node.js 20 and the node:20 image. Create a directory named my-app, then create server.js inside it:

server.js
const http = require('http');

const server = http.createServer((req, res) => {
    res.end('Hello from Docker');
});

server.listen(3000, () => {
    console.log('server is running on port 3000');
});

This application has no third-party dependencies. It uses Node.js's built-in http module to start a server listening on port 3000.


🧱 Common Dockerfile Instructions

Create a file named Dockerfile (without an extension) alongside server.js:

Dockerfile
FROM node:20

WORKDIR /app

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

Here is what each instruction does:

  • FROM node:20: Build on the node:20 image. Our application needs Node.js, so we use the official image as its foundation.
  • WORKDIR /app: Set /app as the working directory inside the container for subsequent operations.
  • COPY . .: Copy the current directory's project files from the build context into the image's working directory.
  • EXPOSE 3000: Document that the container listens on port 3000.
  • CMD ["node", "server.js"]: Specify the command to run when the container starts, launching our application.

🛠 Build and Run

Run the build command in the directory containing Dockerfile:

docker build -t my-web .
  • -t my-web: Name the resulting image my-web.
  • .: Use the current directory as the build context, where Docker reads the Dockerfile and files to copy.

After the build, docker images lists my-web. Run it just like any other image:

docker run -d --name app -p 3000:3000 my-web

Open http://localhost:3000. Seeing “Hello from Docker” confirms that your image works. Share the image with someone who has Docker installed, and the same command runs the same application.


🪤 Image Size and .dockerignore

COPY . . copies the current directory's files into the image. If the project contains node_modules, that entire directory is copied too, making the image larger and builds slower.

As with .gitignore, create a .dockerignore file in the project root to list files to exclude:

.dockerignore
node_modules

Docker skips these files during the build.

Real projects usually install dependencies inside the image with a Dockerfile instruction such as RUN npm install, rather than copying the host's node_modules. This example has no third-party dependencies, so it does not need RUN; for now, just be aware of this usage.

❓ Questions and Answers

Q1: Why do we need FROM? Can we start from scratch?

Images consist of layers and almost always build on a base image. FROM node:20 gives your image an existing Node.js environment, saving you from installing it yourself.

Q2: How does EXPOSE 3000 differ from docker run -p 3000:3000?

EXPOSE documents that the container uses port 3000; it does not publish the port. The -p option in docker run makes it accessible from your computer. Use them here to document the port and publish it, respectively.

Q3: Does changing the code update the image?

No. A built image is fixed. After changing the code, run docker build again to build a new image, then recreate the container from it.

Q4: What is the difference between CMD and RUN?

RUN executes while the image is being built, for example to install dependencies. Its filesystem changes become part of the image. CMD specifies what runs when the container starts, typically your application. Think of RUN as installing software and CMD as running it.

Q5: What causes a “COPY failed” build error?

A common cause is running docker build from the wrong location. The final . means the current directory, so make sure you run the command in the directory containing Dockerfile.


🧪 Practice

  1. Change the response in server.js to a message of your own, rebuild the image, and run it. Verify the new response in your browser:
# Hint: after changing the code, run docker build and docker run again
# Stop and remove the old container with docker stop and docker rm first
# Write your commands here
  1. Add a .dockerignore that excludes node_modules, rebuild, and compare image sizes with docker images.

🎉 You can now package your application into an image! Next, we will use Docker Compose to manage multiple containers with one configuration file.