Docker Series #2: Running a Webserver (httpd)

Docker Series #2: Running a Webserver (httpd)
In: Docker, NetDevOps

Welcome back to the second part of the Docker series. If you're new to Docker or need a quick refresher on what it is and how it works, I recommend checking out the first part of the series below.

Docker Series #1: Hello Docker
As always, my goal here is to explain what Docker is using plain language and relatable examples, I hope to give you a clear understanding of what Docker is.

In our previous blog post, we ran an Ubuntu image as a Docker container. We went ahead and connected to the container's shell, executing a few Linux commands.

In this post, we're going to take things a step further. To give you a practical, real-world example, we will set up a web server using Docker and Apache httpd.

Why Docker?

Imagine a situation where you need to quickly set up a web server for testing. Perhaps you want to verify a NAT policy by initiating traffic from the internet or examine HTTP traffic through packet captures, or maybe you have another reason entirely.

As a network engineer, I don't have a lot of experience with Linux or web development. Traditionally, I might consider spinning up a VM or using tools like Vagrant, and then setting up a web server on it. But this can be a complex and time-consuming task, and we don't always have that kind of time on our hands.

This is where Docker comes in. With Docker, setting up a web server can be done in a matter of seconds, with just two commands. No need for extensive Linux know-how, and no need for time-consuming setup. Docker makes it simple and fast to get a web server up and running. And in this post, we'll show you exactly how to do that.

HTTPD and Docker

HTTPD, or the Apache HTTP Server, is one of the most popular web servers in the world. It's free, open-source, and has a huge community of users and developers behind it. It's used to serve anything from small personal blogs to large-scale enterprise systems.

The httpd Docker image is a lightweight, pre-configured version of the Apache HTTP Server. This image contains everything you need to get a web server up and running in no time. Docker has packed the necessary components of HTTP into an easy-to-use package. All you need to do is pull this image, run a container from it, and voila - you have a working web server.

Pulling the HTTPD Docker Image

PS C:\Users\vsurr> docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
648e0aadf75a: Pull complete
c76ba39af630: Pull complete
b9819ffb14ec: Pull complete
37baa60548e6: Pull complete
6dbce5de7542: Pull complete
Digest: sha256:d7262c0f29a26349d6af45199b2770d499c74d45cee5c47995a1ebb336093088
Status: Downloaded newer image for httpd:latest

This command tells Docker to download (or "pull") the httpd image from Docker's online repository, Docker Hub. Think of Docker Hub as an online library filled with different Docker images that you can freely use. In this case, you're borrowing the httpd image.

Running a Container from the HTTPD Image

PS C:\Users\vsurr> docker run -d -p 8080:80 --name web-server httpd

This command tells Docker to run a new container from the httpd image we just pulled. Here's what each part of this command does.

  • docker run tells Docker to create and start a new container.
  • -d is an option that tells Docker to run the container in the background (detached mode). This way, you can continue to use your terminal while the web server is running.
  • -p 8080:80 is another option that tells Docker to map port 8080 on your machine to port 80 inside the container. This way, when you go to http://localhost:8080 in your web browser, you're actually accessing the web server running on port 80 inside the Docker container.
  • --name web-server is an option to give the container a friendly name. In this case, we're calling our container web-server.
  • httpd at the end is the name of the Docker image (the one we just downloaded) to use when creating this container.

After running this command, you'll have a fully functional Apache HTTP Server running in a Docker container, accessible at http://localhost:8080.

Without Docker

Certainly, running a web server without Docker is indeed possible and there are several ways to do it.

  1. Manually installing the web server: This involves downloading the web server software (such as Apache HTTP Server, Nginx, etc.) directly on your machine, be it Windows, macOS, or Linux. You'd have to go through the installation process which can sometimes be complex, involving command-line interfaces and dealing with configuration files.
  2. Using a virtual machine: You could create a virtual machine (VM), install an operating system on it, and then install the web server on that operating system. This can be resource-intensive as each VM runs a complete operating system.

Docker abstracts away the complexities and differences of operating systems, providing a consistent environment. Whether you're on Windows, macOS, or Linux, the Docker commands to run a web server remain the same.

Accessing the Terminal of a Running Docker Container

If you want to access the terminal of a running Docker container, you can use the docker exec command, which allows you to execute a command inside a running container.

PS C:\Users\vsurr> docker exec -it web-server bash

Here, web-server is the name of our container (as specified when we started it), and bash is the command we want to run - it opens a bash shell inside the container. The -it flag is used to make the session interactive so that you can input commands.

Once you're inside the container, you can use any Linux command you like such as echo or ls. If you want to exit from the docker shell, just type exit

root@0389cee7f801:/usr/local/apache2# exit
PS C:\Users\vsurr>

Closing Thoughts

There you have it, with just a couple of commands, we managed to pull the httpd image and run a web server in a Docker container. We've demonstrated how Docker makes it remarkably simple and quick to deploy a web server without any need for extensive web development or Linux expertise. Stay tuned for more as we continue to explore the world of Docker in upcoming posts!

Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
More from Packetswitch
Table of Contents
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.