WordPress Docker container automatically redirects to production website instead of localhost

I’m working on setting up a local WordPress development environment using Docker on my Ubuntu machine. I copied my production database and imported it into the MySQL container that runs on port 8080 through phpMyAdmin. Even though I used a plugin to update the home and site URLs in the database, I’m still having issues.

Whenever I try to access my local site at localhost:8000, it keeps redirecting me back to the live production website instead of showing my local development version. I can’t figure out what’s causing this redirect problem.

Here’s my docker-compose configuration:

version: '3'

services:
  database:
    image: mysql:5.7
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: myRootPass
      MYSQL_DATABASE: local_wordpress
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: wp123pass

  webapp:
    depends_on:
      - database
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_NAME: local_wordpress
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp123pass
      WORDPRESS_TABLE_PREFIX: "dev_"
    working_dir: /var/www/html
    volumes:
      - /home/user/projects/my_site/wp-content:/var/www/html/wp-content

  dbadmin:
    image: phpmyadmin/phpmyadmin
    container_name: db_manager
    environment:
      MYSQL_USERNAME: root
      MYSQL_ROOT_PASSWORD: myRootPass
    restart: always
    ports:
      - 8080:80
    links:
      - database

volumes:
    mysql_data:

What could be causing this redirect issue? Any ideas on how to fix it?

check your browser cache first - chrome and firefox cache 301 redirects like crazy. try opening localhost:8000 in incognito or clear your cache completely. same thing happened to me and it was just the browser remembering the old redirect from the production site.

This redirect problem is usually from cached redirects or .htaccess rules that didn’t get updated during migration. First, check if there’s a .htaccess file in your wp-content volume with redirect rules pointing to your production domain. WordPress and plugins write redirect rules there that stick around even after database updates. Some caching plugins also store redirect mappings in the database under weird option names that standard URL replacement tools don’t catch. Try renaming your .htaccess file to .htaccess-backup and test again. WordPress Customizer settings are another common culprit - they can have hardcoded URLs that most migration tools miss. You could also add WP_HOME and WP_SITEURL constants directly to your wp-config.php file pointing to localhost:8000. This forces WordPress to ignore whatever URL settings are in the database.

I’ve encountered the same issue when moving a production site to a local Docker environment. WordPress often hardcodes URLs in various locations, not just the main site URL settings. Even after updating those in the database, you might still find old production URLs embedded in serialized data, widgets, and other option fields. The plugin you used likely only addressed basic entries in the wp_options table, missing the more complex serialized data. To fix this, consider using WP-CLI’s search-replace command to comprehensively update URLs. Run wp search-replace 'yourproductiondomain.com' 'localhost:8000' --dry-run inside your container to review the changes it would make. Additionally, check your wp-config.php for any hardcoded constants like WP_HOME or WP_SITEURL; these can completely override your database settings and may need adjusting for local development.