Docker Compose Wordpress+mysql - Alternative ports

Solution 1:

On the bridge network that docker-compose creates, you use the container ports directly. You only use the mapped ports when you connect to a container from the host computer.

Since WordPress and MySQL are on the same network, you should use port 3306. You only need to map the MySQL ports to host ports if you intend to connect to the database from the host.

Also, WordPress doesn't connect to MySQL using port 33060, so that's why there's no parameter for it on the WordPress side.

Here's a docker-compose file that should work

version: "3.9"

services:
  db:
    image: mysql:latest
    volumes:
      - wordpress_db_data:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "80:80"
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  wordpress_db_data: {}
  wordpress_data: {}

Note that I've removed the port mappings on the database, since they're not needed for WordPress to work. If you need to connect to the database from the host, you can just add them back. Usually port 3306 is enough. 33060 is rarely used, AFAIK.