Building and Running a Dockerized Web Server with Nginx: My CodeAlpha Project 4 Experience
Introduction As part of my CodeAlpha DevOps Internship, I worked on a project that focused on containerizing a web server using Docker. For this project, I built a static website using HTML and CSS, packaged it into a
Introduction
As part of my CodeAlpha DevOps Internship, I worked on a project that focused on containerizing a web server using Docker.
For this project, I built a static website using HTML and CSS, packaged it into a Docker image using Nginx, and ran it as a container.
At first, this looked like a straightforward Docker exercise. However, I wanted to understand more than just how to run a few Docker commands. I wanted to understand why I needed a web server, how Docker packages an application, how files get served from inside a container, how port mapping works, and how to confirm that a running container is actually healthy.
This article documents my implementation, the decisions I made, the commands I used, the verification process, and what I learned along the way.
The project is part of my CodeAlpha DevOps Internship portfolio.
Project repository: CodeAlpha DevOps Internship Projects
Project directory: Project_4_Docker_Web_Server
1. Understanding the Project Requirements
Before writing the Dockerfile, I first considered what I was expected to build.
The objective was to create a web server and run it inside a Docker container.
I broke the work into smaller tasks:
- Create a website using HTML and CSS.
- Select a suitable web server.
- Write a Dockerfile.
- Build a Docker image.
- Run the image as a container.
- Access the website through a browser.
- Verify that the container and web server were working correctly.
I also wanted to include a health check so Docker could monitor whether the web server was responding.
One of the first decisions I needed to make was which web server to use.
2. Creating the Website
I decided to build a simple website called CloudOps Web Server.
I used HTML5 for the structure and CSS3 for the presentation.
The website includes sections introducing containerization, web servers, health monitoring, Docker workflows, and the technologies used in the project.
I kept the application simple because the main objective was to learn containerization and web server deployment rather than build a complex application.
My project files at this stage were:
Project_4_Docker_Web_Server/
│
├── index.html
└── styles.css
I created the HTML file first and then added the stylesheet.
The HTML file contains a reference to the CSS file:
<link rel="stylesheet" href="styles.css">
This was important when I later wrote the Dockerfile. Since the website depends on both files, I needed to make sure that both would be available inside the container.
3. Choosing Nginx as the Web Server
After creating the website, I needed something that could serve the HTML and CSS files over HTTP.
I chose Nginx.
Nginx is a popular web server that can serve static content such as HTML, CSS, JavaScript, and images. It is also available as an official Docker image.
I used the Alpine-based version:
nginx:alpine
I selected this image because it provides a lightweight Nginx environment without requiring me to install and configure a web server manually.
This was a practical choice for a small static website.
Why did I need Nginx?
Creating HTML and CSS files alone does not mean I have a running web server.
I needed a service that could listen for HTTP requests, locate the requested files, and return them to a browser.
Nginx handles that responsibility inside the Docker container.
4. Understanding the Nginx Web Directory
Before writing my Dockerfile, I needed to understand where Nginx expects its website files.
The default document root in the official Nginx image is:
/usr/share/nginx/html/
This is where Nginx looks for static website content.
I therefore needed to copy my two website files into that directory.
The mapping would be:
index.html → /usr/share/nginx/html/index.html
styles.css → /usr/share/nginx/html/styles.css
This was an important part of understanding the Dockerfile.
Rather than copying files randomly into a container, I needed to know which directory the web server actually uses.
5. Writing the Dockerfile
With the application files and Nginx document root understood, I created a file named Dockerfile in the project directory.
This was the Dockerfile I used:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
COPY styles.css /usr/share/nginx/html/styles.css
EXPOSE 80
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -qO- http://127.0.0.1/ || exit 1
I wanted to understand what each instruction was doing instead of simply copying a Dockerfile from somewhere else.
FROM nginx:alpine
FROM nginx:alpine
This specifies the base image.
Docker uses the official Nginx Alpine image as the starting point for building my image.
It provides the Nginx web server and the environment needed to run it.
COPY index.html
COPY index.html /usr/share/nginx/html/index.html
This copies my website's HTML file from the build context on my computer into the Nginx document root inside the container.
COPY styles.css
COPY styles.css /usr/share/nginx/html/styles.css
This copies the stylesheet into the same directory.
I needed both files because the HTML page references the stylesheet. Copying only the HTML file would mean the browser could load the page without its intended styling.
EXPOSE 80
EXPOSE 80
Nginx listens on port 80 by default for HTTP traffic.
The EXPOSE instruction documents the port that the containerized application is expected to use.
It is worth noting that EXPOSE does not publish the port on my computer by itself. I would configure that when starting the container.
HEALTHCHECK
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -qO- http://127.0.0.1/ || exit 1
I added a health check to monitor whether the web server was responding.
Here is what the options mean:
-
--interval=30s: Docker runs the health check every 30 seconds. -
--timeout=3s: The check is allowed three seconds to complete. -
wget -qO-: Sends an HTTP request quietly and writes the response to standard output. -
|| exit 1: Returns a failure status if the request fails.
The health check allows Docker to report whether the container is healthy or unhealthy based on the result of the command.
6. Building the Docker Image
After creating the Dockerfile, I opened my Git Bash terminal in the project directory.
I first checked that I was in the correct location and that the necessary files were present.
I then built the Docker image:
docker build -t cloudops-web-server:1.0 .
Let me break down this command.
-
docker buildtells Docker to build an image. -
-tassigns a name and tag to the image. -
cloudops-web-serveris the image name. -
1.0is the image tag. -
.tells Docker to use the current directory as the build context.
The build context matters because Docker needs access to the Dockerfile and the files referenced by the COPY instructions.
Since I was running the command from the project directory, Docker could access index.html and styles.css.
The build completed successfully, creating:
cloudops-web-server:1.0
At this point, I had an image, but I did not yet have a running container.
That distinction is important: an image is the packaged application, while a container is a running instance of that image.
7. Running the Docker Container
After building the image, I needed to start a container from it.
I used:
docker run -d --name cloudops-web-server -p 8080:80 cloudops-web-server:1.0
This command starts the container in detached mode.
Here is what the options mean:
-
docker run: Creates and starts a container from an image. -
-d: Runs the container in the background. -
--name cloudops-web-server: Assigns a recognizable name to the container. -
-p 8080:80: Maps port 8080 on my computer to port 80 inside the container. -
cloudops-web-server:1.0: Specifies the image to run.
Understanding port mapping
The port mapping was one of the important concepts in this project.
I used:
8080:80
The first port is the host port, while the second port is the container port.
My Computer Docker Container
----------- ----------------
localhost:8080 ----------> Nginx:80
Nginx listens on port 80 inside the container.
However, I wanted to access the website from my computer using port 8080.
Docker's port mapping makes that possible.
After starting the container, Docker returned a container ID, confirming that the container had been created and started.
8. Verifying the Running Container
I did not want to assume that the container was working simply because the docker run command returned an ID.
I needed to verify it.
First, I ran:
docker ps
This command lists running containers.
I checked for the following:
- Container name:
cloudops-web-server - Status: Running
- Port mapping:
8080->80/tcp
The container appeared in the output, confirming that it was running and that the expected port mapping had been configured.
9. Checking the Container Health
I also wanted to confirm that the health check defined in the Dockerfile was working.
I used:
docker inspect --format='{{.State.Health.Status}}' cloudops-web-server
The command returned:
healthy
This was an important verification step.
It showed that Docker was able to execute the health check successfully and that the Nginx server was responding to the request.
I also observed that a container can briefly show a starting health status immediately after launch. This is expected while Docker waits for the initial health check to complete.
The important thing was to allow the check to run and verify the final status rather than assume that starting meant the deployment had failed.
10. Testing the Website from the Browser
With the container running and reporting a healthy status, I opened my browser and visited:
http://localhost:8080
The CloudOps Web Server website loaded successfully.
This confirmed that:
- The Docker container was running.
- Nginx was serving the website.
- Docker's port mapping was working.
- My computer could access the web server through the published host port.
This was the point where the different parts of the project came together.
The website files were inside the Docker image, Nginx was running inside the container, and I could access the application from my browser.
11. Testing the Website with curl
I also wanted to verify the web server from the terminal rather than relying only on what I could see in the browser.
I used:
curl http://localhost:8080
The command returned the HTML content of my website.
This confirmed that the web server was responding to HTTP requests.
I then tested the CSS file separately:
curl -I http://localhost:8080/styles.css
The response returned:
HTTP/1.1 200 OK
Content-Type: text/css
This was useful because a website can return its HTML successfully while still having a problem loading its stylesheet.
By testing the CSS file separately, I confirmed that Nginx could serve both the HTML document and the stylesheet.
12. Updating and Rebuilding the Container
During the project, I made some corrections to the website's displayed Docker command examples and the repository link in the footer.
I needed to make sure that the changes were reflected in the container.
One important thing I learned here is that changing a file on my computer does not automatically update a file that has already been copied into a Docker image.
The files are included in the image during the build process.
Therefore, I rebuilt the image and recreated the container.
The commands I used were:
docker build -t cloudops-web-server:1.0 .
docker stop cloudops-web-server
docker rm cloudops-web-server
docker run -d --name cloudops-web-server -p 8080:80 cloudops-web-server:1.0
Here is what happened:
- I rebuilt the image using the updated website files.
- I stopped the existing container.
- I removed the old container.
- I created a new container using the rebuilt image.
I then repeated the verification steps:
docker ps
docker inspect --format='{{.State.Health.Status}}' cloudops-web-server
curl http://localhost:8080
The new container started successfully, the health check returned healthy, and the updated HTML was returned by the web server.
This helped me understand the difference between updating files in a build context and updating an existing container.
13. Project Structure
After completing the implementation and documentation, I organised the project as follows:
Project_4_Docker_Web_Server/
│
├── docs/
│ └── screenshots/
│ ├── container.PNG
│ ├── curl css.PNG
│ ├── curl.PNG
│ ├── docker build.PNG
│ ├── docker desktop container.PNG
│ ├── docker destop image.PNG
│ ├── docker health bash.PNG
│ ├── flowchat.png
│ └── website.PNG
│
├── index.html
├── styles.css
├── Dockerfile
└── README.md
I kept the screenshots in a separate documentation directory so that the main project files would remain easy to identify.
14. Challenges and Things I Learned
This project was smaller than my Azure CI/CD project, but it helped reinforce several important Docker concepts.
Understanding the difference between an image and a container
One thing I paid attention to was the difference between building an image and running a container.
Building an image does not automatically mean the application is running.
I first built cloudops-web-server:1.0, then created a container named cloudops-web-server from that image.
Understanding this distinction made the Docker workflow clearer.
Understanding where Nginx serves files from
I needed to understand the default Nginx document root before writing the COPY instructions.
This helped me understand why the destination path in a Dockerfile matters.
Understanding port mapping
I used port 8080 on my computer and port 80 inside the container.
Understanding the relationship between the host and container ports helped me troubleshoot access to the website and understand how Docker publishes container services.
Understanding Docker health checks
I added a health check rather than relying only on docker ps.
This helped me distinguish between a container that is running and an application inside that container that is responding successfully to a health probe.
Understanding image rebuilds
When I updated the website files, I needed to rebuild the image and recreate the container.
This reinforced the idea that a Docker image contains the files and configuration captured during its build.
15. What I Would Improve in a Future Version
There are several things I would consider if I extended this project.
- Add a CI/CD pipeline to automate image builds.
- Publish the image to a container registry.
- Deploy the container to a cloud hosting environment.
- Configure HTTPS and a domain name.
- Introduce a more comprehensive monitoring solution.
These are possible improvements rather than features implemented in this project.
The current implementation focuses on building and running a Dockerized Nginx web server locally.
16. Final Thoughts
This project allowed me to practise the fundamentals of Docker using a practical example.
I started with two simple website files and worked through selecting a web server, writing a Dockerfile, building an image, running a container, mapping ports, and checking application health.
More importantly, I focused on understanding what each instruction and command was doing.
For me, the value of this project was not just getting a website to load in a browser. It was understanding how the website moved from local source files into a Docker image and eventually into a running container.
This is another step in my journey towards becoming a Cloud and DevOps Engineer.
Project repository: CodeAlpha DevOps Internship Projects
Project 4 source files: CloudOps Web Server – GitHub
This project was completed as part of the CodeAlpha DevOps Internship.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.