Building an Apache Web Server through a Dockerfile and uploading it to Docker Hub

·

3 min read

What is Apache Server?

The Apache HTTP Server is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Source: Wikipedia

Need to Dockerize Apache WebServer

Setting up an Apache server on the workstation requires extensive configuration.To reduce this effort, Docker introduced the concept of Dockerfile to easily create and set up configurations.

Steps to create an Apache Server through a Dockerfile

Step 1: First, we use the mkdir command to create a special directory for all Apache-related files.

$ mkdir apache2

Step 2:Having created a folder, now we go ahead and create a Dockerfile within that folder with the vi editor:

$ cd apache2/
~/apache2$ vi Dockerfile

Once we run the previous command, a vi editor opens. Paste the following content into the Dockerfile:
FROM ubuntu
RUN apt update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt clean
RUN apt clean
EXPOSE 80
CMD [“apache2ctl”, “-D”, “FOREGROUND”]

To exit the editor, press ESC then:wq then Enter.

Step 3:Tag and build the Docker image.

Now, we build the Dockerfile using the docker build command. Within this, we tag the image to be created as 1.0 and give a customized name to our image (i.e., apache_webserver).

docker build -t apache_webserver:1.0 .

Once the image has been built, we should check for the presence of the image using the docker images command.

mudasir@ubuntuserver:~/apache2$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache_webserver 1.0 fbaffc7acf8c About a minute ago 228MB

Step 4: Run the Docker image as a container

Once the image is created, run the image locally as a container:
We run the container in the detached mode so that it runs continuously in the background. Add -d to the docker run command.

To host the Apache server, we provide port 80 (HTTP) for it. Use -p 8080:80 to run the server on localhost.

So, the docker run command also takes the image along with the associated tag as input to run it as a container.
docker run --name myapache -d -p 8080:80 apache_webserver:1.0
The docker ps will confirm the creation of the docker container:

mudasir@ubuntuserver:~/apache2$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

b6530e137e89 apache_webserver:1.0 "/bin/sh -c '[“apach…" 6 seconds ago Created myapache

Step 5: Verify the online presence of the Apache Server

In order to test the presence of the Apache server on the system, visit any local browser and type localhost: or the IP address of the Server

Step 5:How to push the image to Docker Hub

We need to log into our Docker Hub Account in order to push the Image.
The command to log in is:
docker login -u NAME
where NAME is your Docker Hub username. You will be prompted for your Docker Hub password. You can also use your Access Token.

Finally, we’re going to tag our new image and then push it to Docker Hub. First tag the image with: 1.0 using the command:

docker image tag apache_webserver:1.0 bmudasir/myapache:1.0

Where bmudasir is my username.
Now that the image is tagged, we can push it to Docker Hub with the command:

docker image push bmudasir/myapache:1.0

When the push completes, you should find the myapache:1.0 image in your Docker Hub repository.

And that’s all there is to build a Docker image and push it to your Docker Hub repository.


Thanks