Introduction
Dockerfile has become an indispensable tool for creating consistent and reproducible containerized applications. Dockerfile acts as a blueprint, guiding Docker on how to build your application image. In this guide, we'll make Dockerfile easy to understand. We'll learn what it does, how to create it, and how to use it in simple ways. Let's unravel the secrets of Dockerfile together!
What is a Dockerfile?
At its core, a Dockerfile is a script used to create a Docker image. It contains a set of instructions, defining the base image, application code, dependencies, and configurations. Docker reads these instructions and creates a standalone, executable software package known as a container.
How to Create a Dockerfile
Before diving in, ensure you have Docker installed on your system. To begin, create a file named Dockerfile
(without an extension) anywhere on your system. This is the blueprint for building your Docker image. We'll construct a stylish "Hello, World!" web page and serve it using NGINX, a high-performance web server.
FROM nginx
COPY index.html /usr/share/nginx/html
FROM nginx
sets the base image as NGINX, providing a ready-to-use web server environment.COPY index.html /usr/share/nginx/html
copies theindex.html
file (our Hello World webpage) into the NGINX server's default HTML directory.
The index.html file
Now we need to create the index.html
file and it needs to be in the same folder as the Dockerfile
. Here's the content of our index.html
file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World - NGINX</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: 'Arial', sans-serif;
}
.container {
text-align: center;
padding: 20px;
border: 2px solid #3498db;
border-radius: 10px;
background-color: #ffffff;
box-shadow: 0px 0px 10px 0px #3498db;
}
.title {
font-size: 36px;
color: #3498db;
margin-bottom: 20px;
}
.message {
font-size: 24px;
color: #555555;
}
</style>
</head>
<body>
<div class="container">
<div class="title">Hello, World!</div>
</div>
</body>
</html>
- We create a stylish "Hello, World!" message centered on the page.
Now that you've crafted your Dockerfile and created the "Hello, World!" web page, it's time to build your Docker image and run the container.
Building the Docker Image
Make sure you're in the same directory where your Dockerfile
and index.html
files are located. Run the following command to build your Docker image:
docker build -t nginx-web .
In this command, nginx-web
is the name you're giving to your Docker image. The .
at the end of the command signifies that the Dockerfile
is in the current directory.
Verifying Your Docker Image
Once you've built your Docker image for the "Hello, World!" web page, you might want to double-check that it's successfully created. To do this, use the following command in your terminal or command prompt:
docker images
This command lists all the Docker images on your system. Look for the image name you specified while building the image (for example, nginx-web
). If everything went smoothly, you should see your newly created Docker image in the list. This step ensures that your image is ready to be used for running containers and deploying your web application.
Running the Docker Container
Once the image is built, you can run a Docker container based on this image. Execute the following command:
docker run -d -p 80:80 --name hello-world-container nginx-web
In this command:
-d
runs the container in detached mode, which means it runs in the background.-p 80:80
maps port 80 of your host machine to port 80 in the container, allowing you to access the NGINX web server.--name hello-world-container
assigns the name "hello-world-container" to your running container.nginx-web
specifies the name of the Docker image to use.
Accessing Your Hello World Web Page
Once the container is running, open your web browser and navigate to http://localhost
. Voila! You'll see your "Hello, World!" web page served by NGINX inside a Docker container.
You've not only built a Docker image but also deployed a web application in a containerized environment. Congratulations on your successful containerization journey!