How to Build a Web Server Docker File?

Last Updated : 29 Sep, 2026

In this article, you can learn how to create your own docker customized image and get familiar with the docker file. Similarly, you can build a web server image that can be used to build containers. Here we will be exploring the process of using an Apache Web Server on Ubuntu to build our docker image.

Follow the below steps to achieve so:

Step 1: The first step is to build our Docker file for which you can use vim editor. 

vim Dockerfile

Note:- Name of the file has to be "Dockerfile"

Dockerfile

Write the below information into the Docker file.

FROM ubuntu

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean

EXPOSE 80

CMD ["apache2ctl","-D","FOREGROUND"]

Description of the Above Commands

  • FROM ubuntu uses Ubuntu as the base image for creating the Apache web server.
  • ENV DEBIAN_FRONTEND=noninteractive sets the environment to non-interactive mode during package installation.
  • RUN apt-get update updates the package lists available in the Ubuntu image.
  • RUN apt-get install apache2 -y installs the Apache2 web server.
  • RUN apt-get install apache2-utils -y installs the required Apache utility packages.
  • RUN apt-get clean removes cached package files to reduce unnecessary data in the image.
  • EXPOSE 80 documents that the Apache web server listens on port 80 inside the container.
  • CMD ["apache2ctl", "-D", "FOREGROUND"] starts Apache in the foreground, allowing the web server to keep running while the container is active.
docker file

Step 2: Next step is to build the docker file by using the docker build command.

docker build -t="mywebserver"

Command:
-t: this option is to tag the image, mywebserver is the tag to our image.
docker build

After the building of the image is finished, a message will print in the end that the has to build.

build completion

Step 3: The web server file has built, the next step is to create a container from the image for that we use the docker run command.

docker run -d -p 80:80 tag_name

Commands:
-d: This option is used to run the container in detached mode i.e the container
can run in the background.

-p: This option is used to map our port number with 5000 port numbers on our localhost.
container creation

 Now run the docker images command to see the built image.

docker image

If you go to your web browser and write localhost_ip:80 your Apache server is up and running on that port.

successful build of docker image server

This is the complete process to build a web server docker file.

Comment