httpd-container using docker

Run simple httpd container using docker | Complete tutorial with example [2022]

This is the second part of our Learn docker by example series. In this blog, we will be deploying an httpd container using docker. We will also understand the docker port mapping. So without wasting any time, let’s get started.

Run an httpd docker container

To get started with this tutorial, please make sure the docker is running in your system. Please follow this link to install docker in your system.

Type the below command to verify if the docker installation is OK.

docker run hello-world               

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

If you are getting the above output, means your docker installation is OK. You can follow my blog on Hello world, Docker container, to learn more about the hello-world image.

Introduction to httpd container

The Apache HTTP Server, known as Apache, is a Web server application. The docker community maintains this image, and it is different from the official httpd image. More information about the httpd image can be found in the dockerhub http.

Let’s pull the httpd image from the dockerhub using the docker pull command:

docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
69692152171a: Already exists
7284b4e0cc7b: Already exists
3678b2d55ccd: Already exists
aeb67982a725: Pull complete
06954f8169fd: Pull complete
Digest: sha256:48bae0ac5d0d75168f1c1282c0eb21b43302cb1b5c5dc9fa3b4a758ccfb36fe9
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

Verify if the images get successfully pulled using the docker images command

docker images
REPOSITORY                     TAG              IMAGE ID       CREATED         SIZE
httpd                          latest           39c2d1c93266   3 weeks ago     138MB

Now let’s run the below command to run the httpd image.

docker run -d --name my-httpd -p 8080:80 httpd
1b4b8273712b39efc3b02b0890efcf433cea36991af7887b86abaf8ce2a00041

Explanation about the httpd container run command

  • -d flag is used to run the container in detached mode. It means your docker container will be running in the background.
  • –name flag is used to specify the container name. It is always a good practice to give a name to a docker container. If the –name flag is not set, docker will generate any random name for your containers.
  • -p flag is used to publish a container’s port(s) to the host. By default, when you run a docker container, it will not publish any port to the outside world. To access the application outside the docker container, we need to expose the container port to any port on your local system.
  • In the above command, 80 is the port on the docker container where the httpd service is running, and 8080 is the port we have exposed in our local system.
  • To access the httpd service, we need to access it via port 8080
  • 8080 port is user-defined; you can change this port based on availability. If there is any other service running on this port, you can easily specify any different port to access the service.

Run the below command to check if the httpd docker container is running:

docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                  NAMES
1b4b8273712b   httpd     "httpd-foreground"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->80/tcp   my-httpd

Let’s go to Url localhost:8080 to access the HTTPS web UI.

httpd container using docker

If you are getting the above message, it means your httpd docker container is working fine.

Suppose you wish to see what is happening under the hood. You can check the logs by typing the docker logs command followed by container-name.

docker logs my-httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sat Jun 19 16:17:05.681223 2021] [mpm_event:notice] [pid 1:tid 140531868439680] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Sat Jun 19 16:17:05.698883 2021] [core:notice] [pid 1:tid 140531868439680] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [19/Jun/2021:16:17:13 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:18 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:18 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:20 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:20 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:21 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:21 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:23 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:23 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:47 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:47 +0000] "GET /favicon.ico HTTP/1.1" 404 196
172.17.0.1 - - [19/Jun/2021:16:17:57 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [19/Jun/2021:16:17:57 +0000] "GET /favicon.ico HTTP/1.1" 404 196

You can quickly get those details by typing the docker inspect command to get details aboutall properties and parameters are set for your docker container.

docker inspect my-httpd
[
    {
        "Id": "1b4b8273712b39efc3b02b0890efcf433cea36991af7887b86abaf8ce2a00041",
        "Created": "2021-06-19T16:17:04.5708039Z",
.............<supressed o/p>.............. 
.............<supressed o/p>..............      
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
.............<supressed o/p>.............. 
.............<supressed o/p>.............. 
]

In inspect command output, you will get information about the container PortBindings, volume, and other details that we will be coving in another session.

Httpd dockerfile example

So far, we have used the httpd container, which is readily available in the dockerhub. If the official httpd image does not fulfill your need, you can write your dockerfile to start an httpd service.

Create a dockerfile and paste the below content into it

touch dockerfile
FROM centos
RUN yum install httpd -y
COPY index.html /var/www/html/
CMD ["/usr/sbin/httpd","-D", "FOREGROUND"]
EXPOSE 80

Now create a file name index.html and paste the below content

&lt;!DOCTYPE html>
&lt;html>
&lt;body>
&lt;h1>Dockerfile tutorial&lt;/h1>
&lt;p>Custom Httpd Image&lt;/p>
&lt;/body>
&lt;/html>

Now build and run the docker image by typing the below command

docker build -t httpd-dockerfile .
docker run -d -p 8080:80 httpd-dockerfile

Goto localhost:8080 to access the httpd web UI.

You can follow my dockerfile tutorial httpd example to get more information about writing a custom httpd dockerfile.

Conclusion

I hope you liked this basic httpd docker tutorial. This is an excellent example of understanding the docker port mapping and accessing any web application running inside a docker container. Please let me know in the comment box if you have any queries.

More to read?

Hello-world docker image

Docker layer

Docker vs Podman

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top