WordPress /wp-admin redirect to wrong port in docker

Question

While installing an existing WordPress installation in a docker container, I’ve been unable to access the wp-admin pages. The site and pages load perfectly but when browsing to https://example.com/wp-admin that url is redirected to http://example.com:8080/wp-admin and is unable to load since nothing is hosted on that port (note the http/https).

The WordPress site is copied into the docker image that is then executed on google cloud run. Cloud run redirects port 80 to 443. I’ve already tried setting the WP_SITEURL and WP_HOME in the database and wp-config. FORCE_SSL_ADMIN is set to true in wp-config.

The container’s apache webserver runs on port 8080. That’s probably where the wrong port number is taken from somehow. Cloud run proxies 443 -> 8080. I’m using the latest https://hub.docker.com/_/wordpress image with my own wordpress copied in and the following extra modification that made things work so far:

wp-config (only relevant things, the rest is the db connection):

## WordPress container HTTPS fixes for google cloud
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
    $_SERVER['HTTPS']='on';

define('DOMAIN_CURRENT_SITE', $_SERVER['HTTP_HOST']);
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
define('FORCE_SSL_ADMIN', true);

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');

/var/www/html/.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Dockerfile ($PORT is port 8080. $PORT is a default env var set by google cloud, the entrypoint does db setup and then starts apache2):

FROM wordpress:latest
#copy the website and configuration
COPY ./wordpress/ /var/www/html
COPY ./dockerbuild-deps/cloud-run-entrypoint.sh /usr/local/bin/
COPY ./dockerbuild-deps/.htaccess ./dockerbuild-deps/wp-config.php /var/www/html/
COPY ./dockerbuild-deps/.htaccess /var/www/html/wp-admin/
# Use the PORT environment variable in Apache configuration files and make the entrypoint works
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && chmod +x /usr/local/bin/cloud-run-entrypoint.sh

ENTRYPOINT ["cloud-run-entrypoint.sh","docker-entrypoint.sh"]
CMD ["apache2-foreground"]

What changes do I need to make to access wp-admin on https://example.com/wp-admin without a redirect back to port 8080 and http?

0
Gijs Peerlings 2 months 0 Answers 12 views 0

Leave an answer