Configuring Web Server on Docker Container

Hello, we will see how to set up httpd (apache) web server inside a docker container.

First launching a container, to launch a docker container we can use docker run command or docker container run command (newer),

note: docker should be installed and docker service should be running.

Here in the example below I am launching a container with name ‘myos1’ using the docker image of centos:latest. I am exposing the port number 80 for this container (PAT) and can access it using port 81 from outside the container, -it option will give me interactive terminal. After running this command we will get bash shell of this container.

docker container run -it --name myos1 -p 81:80 centos

Now, we are installing httpd package for web server from apache .

yum install httpd -y

After successfully installing httpd, we need to start the httpd service. We use systemctl command to manage a service but it does not work by default inside container and give following error,

So, we are trying another way. If we go to base OS (docker host) and check the status of httpd using systemctl (httpd is installed in my base OS), it will show which file it runs to start the httpd service (it is written in /usr/lib/systemd/system/httpd.service)

So, we use this as command /usr/sbin/httpd to start httpd web server inside our docker container,

Also we can check that httpd is listening on port number 80,

We are putting a file in the document root for testing ,

It is working and I can access the page deployed in the web server inside docker container (using dockerHostIP:81/file.html in browser)

So, we have successfully configured web server inside docker container.

Thank You!

Tech and Programming, MLOps, DevOps Assembly Lines, Hybrid Multi Cloud, Flutter and Ansible Automation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store