Docker for Beginners: Step-by-Step Guide to Your First Project

Introduction:

Welcome to our comprehensive and easy-to-follow guide on Docker for beginners. If you’re new to Docker and looking to kickstart your first project, you’ve come to the right place. In this step-by-step guide, we’ll walk you through everything you need to know to get started with Docker, from understanding the fundamentals to launching your project. Let’s dive in!

What is Docker?

Before we delve into the technicalities, let’s take a moment to understand what Docker is and why it has gained immense popularity in the world of software development and deployment. Docker is an open-source platform that enables you to automate the deployment and management of applications using containerization. Containers provide a lightweight and isolated environment for running software, ensuring consistency across different computing environments.

Getting Started with Docker

1. Installing Docker

To embark on your Docker journey, the first step is to install Docker on your system. Docker provides installation packages for various operating systems, including Windows, macOS, and Linux. Visit the official Docker website and follow the installation instructions tailored to your operating system.

2. Docker Basics

Once Docker is successfully installed, it’s time to familiarize yourself with some key concepts and terminologies. Here are a few essential terms you’ll encounter frequently:

  • Images: Docker images are the building blocks of containers. An image is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
  • Containers: Containers are the running instances of Docker images. They encapsulate the application and its dependencies, making it portable and consistent across different environments.
  • Dockerfile: A Dockerfile is a text file that contains instructions on how to build a Docker image. It specifies the base image, environment variables, required dependencies, and other configuration details.

3. Building Your First Docker Image

Now that you have a basic understanding of Docker, let’s build your first Docker image. Follow these steps:

Step 1: Create a Dockerfile

First, create a project directory and navigate into it.

mkdir developernoob-docker-html-example
cd developernoob-docker-html-example

Create an HTML file named index.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World! From DeveloperNoob</h1>
</body>
</html>

Now, Start by creating a new file in your project directory called Dockerfile. This file will define the instructions for building your Docker image.

Step 2: Define the Base Image

In the Dockerfile, specify the base image you want to use. For example, if you’re working on an HTML project then we can add a lightweight server like nginx

# Use a lightweight web server as the base image
FROM nginx:alpine

Step 3: Install Dependencies

If your application requires any dependencies, use the appropriate package manager (e.g., npm for Node.js) to install them within the Docker image. Since we are going to run only an HTML file so there is no need for any dependencies.

Step 4: Copy Application Files

Copy your application files into the Docker image, ensuring that they are placed in the appropriate directory.

# Copy the HTML file to the web server's document root
COPY index.html /usr/share/nginx/html

Step 5: Expose Ports (If Needed)

If your application listens on a specific port, you’ll need to expose that port in the Docker image using the EXPOSE instruction.

Step 6: Define the Startup Command

Finally, specify the command that should be executed when a container is launched from the image. This command typically starts your application.

docker-for-beginners-code-example

Step 7: Finally Build your First App

# Here 'developernoob-docker-html-example' is the docker image name.
# you can specify any name to it
docker build -t developernoob-docker-html-example .

4. Running Your Docker Container

With your Docker image built, it’s time to run a container and see your application in action. To Run a Docker container using the image: follow the below command

# Here we have specify the 3080 port of your host you can give any port here 
docker run -d -p 3080:80 developernoob-docker-html-example

Now you can access the application in your web browser by navigating to http://localhost:3080. You should see the “Hello, World! From DeveloperNoob” message displayed.

docker-for-beginners-output

We are happy that the post, Docker for Beginners will help you to build your first Docker app.

Find the project Github link – https://github.com/kahnu044/docker-html-hello-world/

For more tutorials please visit – Here

Leave a Comment