In this blog, let’s learn about what a Dockerfile is and use the dockerfile to create a container.
1. Dockerfile
Dockerfile is a text file used to define a set of instructions to build a Docker image. When we use the Docker command-line interface (CLI) to build an image, it reads the instructions from the Dockerfile and executes them to create the image.
The name of the docker file will always be dockerfile and nothing else.
A dockerfile will have instructions written in the following format:
keyword argument
A dockerfile will always start with FROM and FROM can only be used once. A RUN keyword can be used multiple times. But wait, what are these keywords? To understand that I have created the dockerfile format:
FROM <baseimage>
RUN <installedsoftware_or_runcommands>
ENV <environmentvariable>
WORKDIR <dafault_directory>
COPY <argument>
ADD <argument>
CMD <argument>
ENTRYPOINT <argument>
EXPOSE <argument>
VOLUME <argument>
Let us understand what these keywords are.
🐳 FROM
FROM is the mandatory keyword. FROM specifies the Base Image that the Docker images are to be built from according to our customization. Docker images are never built from scratch, they are built on base images. Below is an example of how to use the keyword FROM:
FROM ubuntu
⚙️ RUN
RUN is a keyword that can be repeated multiple times. It is used to provide the Linux commands like installing a package, uninstalling, upgrading a package, creating a directory, or permitting users. Below is an example of how to use the keyword RUN:
RUN apt-get update
🌐 ENV
ENV is a keyword used for environment variables. It defines variables and values that can be used in dockerfile. Below is an example of how to use the keyword ENV:
ENV abc=hello
📂 WORKDIR
WORKDIR is used to specify the default directory where the commands will be executed. Below is an example of how to use the keyword WORKDIR:
WORKDIR /path/to/workdir
If the WORKDIR is not specified, it will be created automatically by the Docker compiler and the default will be the ROOT directory (/
).
📦 COPY
COPY is used to copy files from the host machine to the container directory.
➕ ADD
ADD is used to copy files from the host machine to the container directory and it can also copy TAR files and unzip them on the container directory.
🖥️ CMD
By using the keyword CMD, we will provide the commands or name of the script that should run when the container is launched. This can be overwritten by the user.
🏁 ENTRYPOINT
The commands get executed post-container launch while using the keyword ENTRYPOINT. It cannot be overwritten and a new command can be appended.
🌐 EXPOSE
The keyword EXPOSE specifies the port number that needs to be exposed on the container.
💾 VOLUME
The keyword VOLUME is used to inform users of the Docker image that certain directories within the container should be treated as volumes when running a container based on that image.
2. Task: Create a Dockerfile for a simple web application (e.g., a Node.js or Python app).
🏗️ Build the image using the Dockerfile and run the container.
👀 Verify that the application is working as expected by accessing it in a web browser.
🚀 Push the image to a public or private repository (e.g., Docker Hub).**
I have used the following repository for the task: https://github.com/Azure-Samples/nodejs-docs-hello-world.git
Using the git clone command, clone the repository to your machine. Then get into the directory of the cloned repository.
Create a Dockerfile for a simple web application (e.g., a Node.js or Python app).
Using the command vim open the dockerfile “vim dockerfile”. And the following code is present in the file.
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start" ]
FROM node:14
: Use a base image with Node.js pre-installed
WORKDIR /app
: Set the working directory inside the container
COPY package*.json ./
: Copy package.json and package-lock.json to the working directory
RUN npm install
: Install application dependencies
COPY . .
: Copy the application code to the working directory
EXPOSE 3000
: Expose the port that the application listens on
CMD ["npm", "start"]
: Specify the command to run the application
🏗️ Build the image using the Dockerfile and run the container.
To build the image, save the Dockerfile in a directory alongside your application code and run the following command in that directory:
docker build . -t sample_application
Once the image is built, run the container using the following command:
docker run -p 3000:3000 sample_application
👀 Verify that the application is working as expected by accessing it in a web browser.
Edit the inbound rules for the EC2 instance in AWS. Open port 3000 for your IP for all traffic.
Connect to your application on the Internet (browser) using:
Public_IPv4_address:3000
The application is running!
🚀 Push the image to a public or private repository (e.g., Docker Hub).
To push the image to a repository, an account on Docker Hub, or another container registry. If you have already not signed up, sign up and log in to the docker hub.
In your CLI, log in to the docker hub using the following command and enter your username and password.
Once you have logged in, tag the image with the repository’s name and push it using the following commands:
docker tag sample_application mudit097/sample_nodejs:latest
docker push mudit097/sample_nodejs:latest
Verify it in the docker hub. My docker push seems a success.
Well, this was it for this blog. Until then, keep reading my blogs and connect with me on LinkedIn and let’s have a conversation.
#Day17 #90DaysofDevops