Docker

Ways to Run Apache Web server in Docker Container

Run Apache Web Server in Docker

In order to go through the complete process, first of all we need the image for the Apache which is in this case named as “httpd“. To run Apache web server in docker, a docker based Apache’s image is required.

From the picture below, run the command used in order to pull or download the image to your system’s local repository.

By using the command below, we can confirm that the image is now on our system.

Different ways to run Apache web server in a docker container

  1. No port mapping
  2. With Dynamic or Random port mapping
  3. With Fixed Port mapping

Before going further, if in any case you are testing the docker software in your virtual machine with the help of VirtualBox or VMware software, then make you sure you have settings like shown in picture below.

bridge mode in virtual box on windows 10
bridge mode in virtual box on windows 10

Note: The IP addresses used or shown above may vary in your case, so do not panic, just go with the procedure and replace the IP addresses mentioned in this post with yours.

Again before going further, go along with the picture shown below, this picture is the visual representation of what we are going to do next.

running docker in virtual box on windows 10

No port mapping

Create a new container of “httpd” image, which is going to run in the detached mode.

run docker container in detached mode

sudo docker container run -d –name apache-webserver httpd

  • sudo = run the command with root level permissions
  • docker = command name
  • container = context given to the docker command
  • run = telling docker to run some container from specified image
  • -d = run the docker container in detached mode
  • apache-webserver = name of the container
  • httpd = name of the image

Question: What is docker running in detached mode? or what is detached mode in docker?

Answer: when we want to run a docker container in background without having any interaction with it, then detached mode is our solution. Basically to run a container in standalone manner.

Cross check whether the container is correctly running not.

listing docker containers

Note: Notice that the “port” exposed is “80” and this port is going to transport tcp traffic only.

Question: How to find the IP address of any docker container?

Answer: Run the command shown below, in order to view the IP address of any docker docker container.

inspecting details of a docker container

sudo docker inspect apache-webserver | grep -i address

Now, install the command line browser “elinks” in order to check whether our Apache web server is running and delivering any kind of content or not.

install yum on centos

sudo yum install elinks

From below, it can be seen that on accessing the IP address of the running container, it is shown “It works!” which is being delivered by Apache web server.

using elinks with dump

elinks –dump 172.17.0.2

Extra

Just for more depth reference, we are creating one more container based on “alpine Linux” and in that, we are going to install “curl browser”. After that from it, we will try to access the web server running in another container.

run alpine based docker container

Question: What if we are running docker containers in a virtual machine and we want to access the machine or the content running in it outside the virtual machine like on our host machine?

Answer: The only way to do that is with the port mapping, there are two type of port mapping, follow below to learn more.

With Dynamic port mapping

Create a new container based on “Apache image” but in this we are going to specify a new option “-P“, it is nothing but telling docker to assign some port number to it from host’s available port number and map it to the port number “80” of the container.

run docker container with random or dynamic port mapping

sudo docker container run -d –name apache-dynamic-port -P httpd

Question: Why port mapping is required?

Answer: Because we want to route the traffic from and into the container to the outside world or to our host machine. So that if any request to the host machine is coming then it is going to be forwarded to the container running under it.

After creating the container, with the help of “sudo container ls” you can view all the running containers and from the picture below, we can see that container with “ID = 50e4382ffc89” and “name = apache-dynamic-port” is having a port mapping like => “0.0.0.0:32768->80/tcp“.

  • 0.0.0.0:32768->80/tcp = It means that the request coming from any IPv4 Address on port 32768 is going to be forwarded to the port 80 of the running container.

In order to confirm whether our Apache web server is delivering any content or not in this case, first we need to check the IP Address of the machine on which docker is installed. After that, on the same network you can go to any device and in the browser and access the IP Address of that machine with specified port, as shown below.

check IP address in linux
access an web server with specified port number

Note: The IP Address on your machine may vary.

With Fixed port mapping

Question: What if we want to access the web server with port 80 and not on any other random port?

Answer: In that case, we have a “fixed port mapping” option in docker which can be used just using a flag “-p” and specifying the port number.

run a docker container with fixed port mapping

sudo docker container run -d –name apache-fixed-port -p 80:80 httpd

  • -p = this is to tell docker to map some docker port to the host machine’s port and vice-versa.
  • 80:80 = map port 80 of docker to the port 80 of host machine.

To confirm, simply go to any device under that network and in the browser type the IP Address of the machine on which docker is running without specifying any port number, can be seen below.

Other ways for port mapping

  • -p 8000:80 = map port 80 of docker container to the port 8000 of host machine
  • -p IP:8080:80 = map port 80 of docker container to the port 8000 of specified IP
  • -p 8000:80/tcp = map tcp port 80 of docker container to port 8000 of host
  • -p 8000:80/tcp -p 8000:80/udp = map port tcp and udp port 80 of container to port 8000 of host machine.

Comment here