POSTS
Wordpress in Docker: What caused the incorrect port forwarding
Did you ever come across a scenario when you wanted to get something done desperately, but, you felt like being on the verge of giving up, however, you never really gave up?
Since last night, I had been going crazy trying to access my local installation of WordPress site from my Android device, but after umpteenth effort, all I landed on was either an error page or an empty page. Something was going terribly wrong with the WordPress URL that I was trying to access from my Android app.
Out of several issues that I faced, I would like to discuss a very specific issue here in this blog post. The issue surfaced when I performed following steps:
The Problem
Rough Attempt-1:
- Pulled a XAMPP Docker image
Started the docker container, with container port 80 mapped to host port 5000
$ docker run -it -p 5000:80 <image-name>
Moved my local WordPress installation files into the running container under the directory named as
www
Accessed the WordPress link successfully via URL
http://127.0.0.1:5000/www
Configured MySQL database for WordPress by following the installation steps displayed on browser screen
Once MySQL setup was completed, and after WordPress admin user was created, I could access the WordPress blogs and admin pages successfully in my laptop
When I tried accessing the WordPress site on my Android phone via URL
http://<HOST-IP>:5000/www
, the page did not load for unknown reasons.
Rough Attempt-2:
Took a backup of MySQL database from the running container
$ mysqldump -u [username] -p [databaseName] > mysqldump_backup.sql
Closed all the running instances of Docker containers
$ docker stop `docker ps -aq` $ docker rm `docker ps -aq`
Started the docker container, with container port 80 mapped to host port 80
$ docker run -it -p 80:80 <image-name>
Moved my local WordPress installation folder into the running container
Configured MySQL database using the saved file containg MySQL dump from previous installation
$ mysql -u root < mysqldump_backup.sql
Accessed the WordPress link successfully via URL
http://<HOST-IP>/www
However, when I tried accessing the WordPress site through container IP address
http://172.17.0.2/www
, I got redirected tohttp://172.17.0.2:5000/www
The more I tried different scenarios, the more it got confusing.
And, yes, I was still receiving error while trying to access the WordPress site from my Android device
The Solution
Initially, I thought that it must be browser caching issue. But, clearing the browser cache did not help in resolving the mysterious port forwarding issue.
I thought maybe there’s some hidden cache I am unable to reach, so, I closed all my windows and restarted my laptop. The issue persisted even after the fresh reboot. I was still being redirected to an unknown port whenever I tried accessing the WordPress site running inside the Docker container.
Somehow, I became sure that it cannot be a browser caching issue, and that something must be getting stored somewhere else, maybe in the container itself.
After Googling for solutions, I came across a very hopeful blog post which wasn’t directly addressing my issue, but it seemed like guiding me into the right direction:
Connection refused? Docker networking and how it impacts your image
I changed my Google search keywords, and, stumbled upon another link where one of the comments said something about changing values in wp_options
table:
I recalled viewing table names starting with wp_
in the MySQL database. In past 24 hours, this must have been the most liberating moment for me. I was right on track. I saw the culprit sitting in the wp_options
table
I updated the URL values stored in siteurl
and home
variables, and restarted XAMPP.
This time, the URL http://<HOST-IP>/www
served the WordPress website correctly on both, my laptop and my Android device.
Thank You Note
Thanks to all the smart brains whom I gave a distress call at odd times of day and night… :D Thank you for being patient with me and for giving me your sincerest feedback. Your inputs and suggestions motivated me to keep moving and pushed me to try a little harder even when I felt drained and exhausted.
It seems obvious once you know it… But to know the obvious needs tons of patience and lots of friends (to rant your frustration)! :)