This tutorial aims to host a simple web server on a Raspberry Pi. This produces a very lightweight web server and works well to host a microservice or to test a website without deploying a full web server on the cloud. We will use Docker on Raspbian OS and spin up an Apache 2.4 container from Docker Hub.
The only prerequisite to following this guide is that you have SSH connection enabled, Raspberry Pi 1, 2, 3, 4 or Pi Zero W with a running Raspbian OS.
Installing Docker to the Raspberry Pi
Step 1: Update and Upgrade
Before installing Docker we need to make sure that the Raspberry Pi is running the latest software.
sudo apt-get update && sudo apt-get upgrade
Step 2: Download the Script to Install Docker on Raspberry Pi
downloading and running the script is very easy just copy and paste the command in the terminal:
curl -fsSL https://get.docker.com -o get-docker.sh
To execute the installation script enter this command:
sudo sh get-docker.sh
Now you have to wait for the script will install all the required packages in Raspberry Pi.
Step 3: Add a Non-Root User to the Docker Group
By default, only root users can run the docker containers. If you are not logged in as the root you will need to use the sudo prefix every time and it’s not recommended. We can easily skip by adding the non-root user to the Docker group here is how to do that:
sudo usermod -aG docker [user_name]
To add the Pi user (the default user in Raspberry Pi OS), use the command:
sudo usermod -aG docker pi
Setting up Apache on Raspberry Pi
One of the best things about the Docker ecosystem is that there are tens of standard docker containers that you can easily download and use.
In this article, we will instantiate an Apache 2.4 container named raspberry-pi-web-server, detached from the current terminal. We will use an image called httpd:2.4 from Docker Hub.
Our plan is to have requests made to raspberry pi’s local IP address on port 8080 be redirected to port 80 on the container. Also, instead of serving content from the container itself, we will serve a simple web page from /home/user/website.
We will do this by mapping /home/user/website/ on the /usr/local/apache2/htdocs/ on the container. Note that you will need to use sudo or login as root to proceed, and do not omit the forward slashes at the end of each directory.
# sudo docker run -dit --name raspberry-pi-web-server -p 8080:80 -v /home/user/website/:/usr/local/apache2/htdocs/ httpd:2.4
At this point, our Apache container should be up and running.
$ sudo docker ps
Now let’s create a simple web page named raspberry-pi-web-server.html inside the /home/user/website directory.
# vi /home/user/website/raspberry-pi-web-server.html
Add the following sample HTML content to the file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Raspberry Pi Web Server</title> </head> <body> <h1>Learn Docker</h1> </body> </html>
Next, point your browser to Server-IP:8080/raspberry-pi-web-server.html (where Server-IP is your Raspberry Pi IP address).
If you wish, you can now stop the container.
$ sudo docker stop raspberry-pi-web-server
and remove it:
$ sudo docker rm raspberry-pi-web-server
To finish cleaning up, you may want to delete the image that was used in the container (omit this step if you’re planning on creating other Apache 2.4 containers soon).
$ sudo docker image remove httpd:2.4
In this article, we explained how to Set Up a Raspberry Pi Web Server using docker. You can learn more about raspberry pi with our other articles: