How I Resolved the Issue of self-hosting Websites Going Offline After a Raspberry Pi Reboot

Recently, I encountered an issue with my Raspberry Pi-hosted websites that would go offline every time the Pi was rebooted. After several troubleshooting steps and some trial and error, I was able to bring my websites back online and set up Docker to automatically restart the containers after each reboot. Here’s the debugging process and solution.

1. Initial Problem: Websites Were Offline

After rebooting my Raspberry Pi, my websites stopped working. I tried accessing them, but all I got was error messages. Upon checking the logs, I saw this line in the NGINX error logs:

pgsqlCopyEditconnect() failed (111: Connection refused) while connecting to upstream

This indicated that the NGINX server couldn’t connect to the upstream service—likely the WordPress container running on port 8081.

2. Checking Docker Containers

Using the docker ps -a command, I realized that the WordPress containers (wordpress1, wordpress2) had stopped running after the reboot. They were in an “Exited” state.

docker ps -a

The output showed:

nginxCopyEditCONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS                     PORTS                  NAMES
b80590e36948   wordpress:latest       "docker-entrypoint.s…"   7 weeks ago    Exited (255) 18 hours ago   0.0.0.0:8081->80/tcp   wordpress1
8152b44a1be7   wordpress:latest       "docker-entrypoint.s…"   7 weeks ago    Exited (255) 18 hours ago   0.0.0.0:8082->80/tcp   wordpress2

The Exited (255) status code usually means the containers crashed or failed to start properly, which was causing the websites to be offline.

3. Reviewing Docker Logs

To understand why the containers exited, I checked the logs using:

docker logs wordpress1
docker logs wordpress2

That is too much output, I should only output the last section of the logs, but in the end, the logs take so long to load, and I cancelled it.

4. Restarting the Docker Containers

To get the websites back online quickly, I restarted the Docker containers, with the help of ChatGPT:

docker start wordpress1
docker start wordpress2

docker start mysql1
docker start mysql2

This temporarily fixed the issue, but I wanted a more permanent solution to ensure the containers would automatically restart after every reboot, without requiring manual intervention.

5. Setting Up Automatic Restart

After the containers were back up and running, I decided to configure them to restart automatically after each reboot. Docker offers a built-in feature called restart policies, which control how containers behave when they exit or when Docker itself restarts.

To apply a restart policy for the existing containers, I ran the following commands:

docker update --restart unless-stopped wordpress1
docker update --restart unless-stopped wordpress2

docker update --restart unless-stopped mysql1
docker update --restart unless-stopped mysql2

This sets the restart policy to unless-stopped, which means the containers will restart automatically unless they were manually stopped.

6. Enabling Docker to Start on Boot

In addition to setting the restart policy for my containers, I also made sure that Docker itself starts automatically when the Raspberry Pi reboots. To ensure Docker starts on boot, I ran:

bashCopyEditsudo systemctl enable docker

This command ensures that the Docker service will start automatically after a reboot.

7. Testing the Setup

After applying the restart policy and ensuring Docker starts on boot, I decided to test the setup by rebooting the Raspberry Pi:

sudo reboot

After the reboot, I checked the status of my containers using docker ps and confirmed that both WordPress containers (wordpress1, wordpress2) were running again.

8. Final Result: Websites Are Now Online Automatically

With these steps in place, my websites are now set to automatically come back online whenever the Raspberry Pi reboots. The Docker containers restart automatically, and the NGINX server successfully proxies the requests to the containers.


Conclusion

By following the debugging process and using Docker’s restart policies, I was able to resolve the issue of my websites going offline after a reboot. This approach allows the websites to be automatically online after each reboot without requiring manual intervention.

Leave a comment

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