Building an Apache Web Server through a Dockerfile and uploading it to Docker Hub
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 |