Issues with Two WordPress Installations and One Database in Docker

I am attempting to set up a Docker environment with two WordPress instances, each connecting to a shared MySQL database. The configuration I want has one WordPress running on port 8000 and the other on port 8001.

Here is the docker-compose setup I’m trying:

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: yourpassword
       MYSQL_DATABASE: wordpress_db
       MYSQL_USER: wordpress_user
       MYSQL_PASSWORD: wordpress_pass
   wordpress-first:
     depends_on:
       - db
     image: wordpress:latest
     container_name: first-blog
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress_user
       WORDPRESS_DB_PASSWORD: wordpress_pass
       WORDPRESS_TABLE_PREFIX: first_
   wordpress-second:
     depends_on:
       - db
     image: wordpress:latest
     container_name: second-blog
     ports:
       - "8001:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress_user
       WORDPRESS_DB_PASSWORD: wordpress_pass
       WORDPRESS_TABLE_PREFIX: second_
volumes:
    db_data:

After executing docker-compose up, everything seems to work well, and the following services are visible:

STATUS              PORTS                  NAMES
Up 10 minutes       0.0.0.0:8000->80/tcp   first-blog
Up 10 minutes       0.0.0.0:8001->80/tcp   second-blog
Up 10 minutes       3306/tcp               wordpress_db

However, when I try to access localhost:8001, it redirects to localhost:8000, giving me an HTTP 301 status. I suspect this might be a WordPress issue, but I’m uncertain. Can anyone help me understand why this is happening?

sounds like the site url in ur db is set to the first instance. try update the siteurl and home options for the second wp instalation to match its port, or just use different dbs.

Had the exact same issue when migrating a client’s staging setup. This happens because WordPress’s URL canonicalization gets confused when both sites share the same database server without proper isolation. Your docker-compose is mostly right, but you’re missing WORDPRESS_DB_NAME for both services. Without it, both instances try using default database names and conflict with each other. Also check for plugins or themes forcing redirects based on domain detection - they’ll mess you up every time. Add unique WORDPRESS_DB_NAME values for each service, or better yet, create separate databases entirely. Saved me hours of debugging redirect loops in my multi-tenant setup.

This happens because WordPress stores the site URL in the database, and both your installations are writing to the same database. You’ve got different table prefixes, but you’re missing WORDPRESS_DB_NAME from your environment variables - so both instances default to the same ‘wordpress’ database name. I ran into this exact issue setting up a similar dev environment. Just add WORDPRESS_DB_NAME: wordpress_db to both WordPress services in your docker-compose file. Without it, WordPress creates its own database structure and you get URL conflicts. Better yet, create separate databases for each installation. That gives you proper isolation and prevents settings from mixing together. Works great in production when you need complete separation.