Dockerfiles: Your Guide to Building Docker Images

Tech Entusiast and DevOps Practitioner
In the realm of software development, Docker has emerged as an indispensable tool for streamlining application deployment and management. At the heart of Docker lies the Dockerfile, a text-based instruction manual that guides the creation of Docker images*, the building blocks of *Docker containers.
👉Understanding Dockerfiles: The Essence of Docker Images
A Dockerfile is a text file that contains a series of instructions, each defining a step in the process of building a Docker image. These instructions range from setting up the base environment to installing dependencies, copying application files, and configuring the application itself.
👉The Anatomy of a Dockerfile
A typical Dockerfile consists of several sections, each serving a specific purpose:
FROM: This instruction specifies the base image from which the new image will be built. Base images provide the underlying operating system and runtime environment for the application.
WORKDIR: This instruction sets the working directory for subsequent commands.
COPY: This instruction copies files from the local machine to the image.
RUN: This instruction executes commands within the image's environment.
CMD: This instruction specifies the command that will be executed when the container is started.
EXPOSE: This instruction exposes ports within the container that can be accessed from outside the container.
ENV: This instruction sets environment variables within the image.
👉Creating The First Dockerfile & A Simple Project hosted on AWS
Let's embark on a simple project to showcase Dockerfile creation, gain a concise understanding of AWS EC2*, and revel in the final outcome! Get ready for an exciting journey!*
Let's create a docker file -
FROM node:12.2.0-alpineImagine you're starting a new project and want to build a sturdy foundation. This command is like laying the foundation of your project by specifying the base image you'll be using. In this case, you're choosing the
node:12.2.0-alpineimage, which provides a lightweight and secure Node.js environment.WORKDIR app/Think of your project as a vast landscape and you want to set up a base camp for your development activities. This command is like establishing that base camp by setting the working directory to
app/. This tells Docker where to find your application files and where to execute subsequent commands.COPY . /appPicture yourself packing your essential gear for the project – your application files. This command is like loading your gear onto a cargo plane and transporting it to the base camp. It copies all the files from the current directory (
.) to the working directory (/app) inside the container.RUN npm installNow that you're at the base camp, you need to gather the necessary tools and supplies to build your project. This command is like going to the nearest town and purchasing all the required dependencies for your Node.js application using
npm install.CMD ["node","app.js"]Imagine setting up a command center to control your project's operations. This command is like establishing that command center by specifying the command to execute when the container starts. It tells Docker to run the
nodecommand with theapp.jsfile as an argument, effectively launching your Node.js application within the container.Here is the final Dockerfile
FROM node:12.2.0-alpine WORKDIR app/ COPY . /app RUN npm install CMD ["node","app.js"]

Now we need to build a docker image -


Now we need to run the recently created docker image

Here, you can observe that the container is running successfully as the container hash is printed on the console without any errors. 🙂
So, let's check if the application is running or not.
The public IP address can be found on the EC2 dashboard. Here, we can see that the application is running on port 8000, as specified by the developer in the code.
However, we are encountering an error message stating, 'This site can't be reached.' 😒

- Now we need to allow traffic to port 8000 of the EC2 instance, which we have already mapped to the host and container while running the container in detached mode. Refer to the image in point 3.


- After configuring the port you can see the final outcome!

Yeah! The app is running.....
Thank you for reading through this blog and I hope that you found it informative.



