Open In App

Dockerizing a simple Web application

Last Updated : 18 Oct, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

The goal of this article is to show how to run a Web Application into a Docker container.

  • First of all we will create a sample Web app and then,
  • Build a Docker image of that application and run it.

    Setting up Web appFirst, you'll need a dedicated directory for your project. This keeps all your files organized and ensures only the necessary files are included in your Docker image.
  • /my-nginx-project/
    |-- Dockerfile
    |-- index.html

    Project Structure

    First, you'll need a dedicated directory for your project. This keeps all your files organized and ensures only the necessary files are included in your Docker image.

    1. Prepare the Build Context

    Create an isolated directory for your project. This is your "build context"—everything in this folder will be sent to the Docker daemon.

    mkdir my-nginx-project && cd my-nginx-project


    docker1


    2. Write the Nginx Dockerfile

    This is the most important part. Create a file named Dockerfile in your project directory.

    vi Dockerfile

    Now, add the following content. This Dockerfile uses the official lightweight Nginx image from Docker Hub, which is a common best practice.

    # Base image: Official lightweight Nginx image
    FROM nginx:alpine
    # 1. Copy your custom HTML page into the Nginx document root
    COPY index.html /usr/share/nginx/html/index.html
    # 2. Expose port 80 for HTTP traffic (good practice for documentation)
    EXPOSE 80
    # 3. The base Nginx image already includes a command to start the server,
    # so you don't need to add a CMD instruction.

    3. Create the HTML Page

    Next, create a simple index.html file in the same directory. This is the webpage your Nginx server will display.

    # Create the HTML file
    vi index.html

    Add some example content to it:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Welcome to Nginx</title>
    </head>
    <body>
    <h1>Hello from my Nginx Docker Container!</h1>
    </body>
    </html>

    4. Build the Docker Image


    Now, use the docker image build command to create your image from the Dockerfile. It's a good practice to tag your image with a name and version.

    # The '.' at the end specifies the current directory as the build context  

    docker image build -t my-nginx-image:v1 .

    After the build completes, you can verify that your new image exists:

    docker image ls 


    docker2

    5. Test the Container

    Finally, run a container from your newly created image to test it. This command will start the container in detached mode (-d) and map port 8080 on your computer to port 80 inside the container.

    docker container run -d -p 8080:80 --name nginx-test my-nginx-image:v1
    docker3
    You will see the image of you application.

    You can now open your web browser and navigate to http://localhost:8080.



    Explore