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.
FROM nodeEvery Docker image we want to create is based on a base image.
When we write FROM, after it we define our 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 MONGO_DB_USERNAME=admin \
MONGO_DB_PASSWORD=qwertyENV is used to define environment variables inside the Docker image.
These variables become available inside the container during runtime.
RUN mkdir -p /home/appRUN 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 . /home/appThis copies files from the host machine into the Docker image.
COPY <host-path> <container-path>. (current directory)/home/appThis copies all files and folders from our current directory into the /home/app folder inside 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.
Example format:
CMD ["command", "arg1", "arg2"]EXPOSE 5050EXPOSE is used to inform Docker that the container listens on a specific port.
-p in docker run| 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 |
A Dockerfile is a step-by-step blueprint that tells Docker:
This is how a simple Node.js application becomes a fully containerized Docker image.