Docker Series - Running a Webserver and a Cool App

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. 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.

Docker Series - 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 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 and a very cool note taking app using Docker.

Why Docker?

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

As a network engineer, I don't have a lot of experience with Linux or web development. Traditionally, I might spin up a VM or use tools like Vagrant, then set up a web server on it. But this can be complex and time-consuming, and we don’t always have that kind of time.

This is where Docker comes in. With Docker, setting up a web server takes just a few seconds and two commands. No need for deep Linux knowledge or lengthy 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
docker.io/library/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
0389cee7f80186f4d69ffee549496821c091c8079cebb252628b8a0f4011121b

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 host 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
root@0389cee7f801:/usr/local/apache2#

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
exit
PS C:\Users\vsurr>

Installing a Cool Note Taking App

So, we saw how to install a simple web server, but that probably doesn’t excite most of you. Let’s try something a bit more interesting, installing a very cool note-taking app called memos. I covered this app in detail in another blog post, so feel free to check that out if you want to know why I like it so much. To install memos, all you need to do is run this single command.

Memos - Amazing Open Source, Self-hosted Notes App
That being said, I recently stumbled upon another great self-hosted note-taking app called ‘Memos’ I just couldn’t believe that I didn’t know about this until very recently.
docker run -d \
  --init \
  --name memos \
  --publish 5230:5230 \
  --volume ~/.memos/:/var/opt/memos \
  neosmemo/memos:stable
  • docker run -d - Starts the container in detached mode, so it runs in the background.
  • --init - This flag adds a lightweight init system to the container that handles process management and signal handling.
  • --name memos - Names the container "memos", so it's easier to manage later.
  • --publish 5230:5230 - Maps port 5230 on your machine to port 5230 inside the container.
  • --volume ~/.memos/:/var/opt/memos - Mounts a directory from your machine into the container so memos can save data outside the container itself. We’ll cover volumes properly later in the series.
  • neosmemo/memos:stable - This is the image name, which is the actual application.

Once it’s up and running, you can access memos by opening http://<your-ip-address>:5230 in your browser. And that’s it - you now have a very nice note-taking app installed with just one command.

We will go into Docker Networking in more detail later, but for now, as a client, you just need to connect to the host's IP and the port we exposed. The host will then forward the traffic to the container, which has its own internal IP. With Docker's default networking mode (bridge), you cannot directly reach the container's real IP from outside the host. We’ll explain why this happens and how to work around it in a future post.

Closing Up

With just a couple of commands, we pulled the httpd image and ran a web server in a Docker container. We also installed memos, a cool note-taking app, with a single command. This shows how Docker makes it quick and easy to deploy both simple and useful applications without needing deep Linux or development knowledge. Stay tuned for more as we continue to explore Docker in the upcoming posts.

Docker Series - Dockerfile
A Dockerfile is a text document that contains all the instructions a user could tell Docker to assemble an image. Using a Dockerfile, you can automate the