The Dockerfile
The special file that does the work of Dockerizing any of our applications is called a Dockerfile.
The Dockerfile is basically a blueprint for how our Docker image and Docker containers should be built. It contains all the instructions for how to Dockerize our application.
Important Dockerfile Instructions
FROM — Base Image
Every Docker image we want to create is based on a base image.
When we write FROM, after it we define our base image.
What is a Base Image?
Our Node.js application requires Node to be set up on the system to run it.
Similarly, when our application becomes a Docker image or container, there are specific dependencies that should already be set up inside that container.
For a Node.js application, the base image is the node image.
This is also what we mean by layering — our test-app image is built on top of the Node image, which itself may be built on top of another base image (like Debian).
Layer on layer on layer — this is how layering works in Docker images.
ENV — Environment Variables
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PASSWORD=qwerty
ENV is used to define environment variables inside the Docker image.
These variables become available inside the container during runtime.
RUN — Execute Commands Inside the Container
RUN is used to execute commands while building the Docker image.
We can have multiple RUN commands in a Dockerfile — for every different command we want to execute.
Each RUN instruction creates a new layer in the image.
COPY — Copy Files From Host to Image
This copies files from the host machine into the Docker image.
Format:
COPY <host-path> <container-path>
Example:
- Host path:
. (current directory)
- Container path:
/home/app
This copies all files and folders from our current directory into the /home/app folder inside the container.
CMD — Default Command to Run the Container
CMD ["node", "/home/app/server.js"]
We can have multiple RUN commands but only one CMD command.
CMD is the single command that gets executed once the container is fully set up — it is what starts our application inside the container.
Important Notes:
- CMD defines the default startup command
- It runs only when the container starts
- Written in JSON array format (list of strings)
Example format:
CMD ["command", "arg1", "arg2"]
EXPOSE — Expose a Port
EXPOSE is used to inform Docker that the container listens on a specific port.
Important:
- It does NOT publish the port automatically
- It acts as documentation inside the Dockerfile
- Actual port mapping is done using
-p in docker run
Summary of Dockerfile Instructions
| Instruction | Purpose |
|---|
| FROM | Defines base image |
| ENV | Sets environment variables |
| RUN | Executes commands during image build |
| COPY | Copies files from host to image |
| CMD | Defines default container startup command |
| EXPOSE | Declares container port |
Key Idea
A Dockerfile is a step-by-step blueprint that tells Docker:
- What base environment to start from
- What dependencies to install
- What files to include
- What command to run when the container starts
This is how a simple Node.js application becomes a fully containerized Docker image.