Unleashing the Potential of Apache for Web Applications: A Guide to HTTPD Server Configuration on Docker Containers

Pramod Kumar Gupta
4 min readJul 26, 2023

--

First pull an image .

docker pull image_name

Now launch the container with the following comnand …

docker run -it --name wb1  -p 80:80 centos:7

docker run: This command is used to create and start a new Docker container based on a specified image.

-it: These are two options passed to the docker run command. It combines two flags: -i (interactive) and -t (pseudo-tty). This option allows you to interact with the container and attach to its standard input, so you can see the container's output and interact with its shell.

--name wb1: This option assigns a name to the running container. In this case, the container will be named "wb1".

-p 80:80: This option is used for port mapping. It maps port 80 from the host machine to port 80 inside the container. The syntax is -p HOST_PORT:CONTAINER_PORT. With this mapping, any traffic coming to port 80 on the host machine will be forwarded to port 80 inside the container.

centos:7: This specifies the Docker image that the container will be based on. In this case, it's the official CentOS 7 image from Docker Hub. If the image is not already available on the local machine, Docker will automatically pull it from Docker Hub before creating the container

And install httpd indise the container.

yum install httpd -y

Also let’s install net-tools packagae for getting ip .

yum install net-tools -y

By default, the httpd software reads HTML files from the /var/www/html directory. Therefore, it is necessary to place the HTML files within this directory to ensure proper functionality.

Here I have created index.html file.

cd /var/www/html/

echo "This is webserver running on docker container" >> index.html

Finally, to initiate the httpd service, we need to start it. In cases where the ‘systemctl’ command is unavailable by default, an alternative approach is to use the ‘/usr/sbin/httpd’ file. The reason being that ‘systemctl’ internally loads this file to initiate the service. Therefore, using ‘/usr/sbin/httpd’ can effectively start the httpd service in such situations.

We need to run the webserver in background . So exit from containaer and run the foolowing command -

 docker exec -d wb1 /usr/sbin/httpd -DFOREGROUND

Let’s break down the command:

docker: This is the command-line interface (CLI) for Docker, a platform used to develop, ship, and run applications in containers.

exec: This subcommand allows you to execute a command inside a running container.

-d: This flag stands for "detached" mode, which means the container will run in the background and the terminal will not be attached to it. In other words, you'll get your command prompt back immediately after executing the command.

wb1: This is the name or ID of the container where you want to execute the command. Docker containers are isolated instances of an image.

/usr/sbin/httpd -DFOREGROUND: This is the command that will be executed inside the container. In this case, it starts the Apache HTTP Server with the -DFOREGROUND flag. The -DFOREGROUND flag is often used to run Apache in the foreground, meaning it will not daemonize and will log output directly to the terminal, which is suitable for running within a container. The /usr/sbin/httpd is the path to the Apache HTTP Server binary.

Now check the whether the webser is ruuning by going to to url

httpd://IP:port/index.html

Thanks for reading …

--

--