Docker中有两个WordPress容器,但只工作一个

6yoyoihd  于 2023-08-03  发布在  WordPress
关注(0)|答案(1)|浏览(126)

我试图使2个不同的和独立的WordPress网站,在2个不同的docker容器。
我有2个Docker容器的WordPress在2个不同的文件夹,但事情是,第一个(容器1)工作正常,但第二个(容器2)不工作,它显示我Error establishing a database connection
容器1

services:
  db:
    platform: linux/x86_64
    image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password    
  wordpress:
    image: wordpress:latest
    ports:
      - 8084:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    volumes: 
      - ./html:/var/www/html 
volumes:
  db_data: {}

字符串
容器2

services:
  db:
    platform: linux/x86_64
    image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress2
      - MYSQL_USER=wordpress2
      - MYSQL_PASSWORD=wordpress2
    expose:
      - 3306
      - 33060
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8081:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password    
  wordpress:
    image: wordpress:latest
    ports:
      - 8085:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress2
      - WORDPRESS_DB_PASSWORD=wordpress2
      - WORDPRESS_DB_NAME=wordpress2
    volumes: 
      - ./html:/var/www/html 
volumes:
  db_data: {}


有什么想法或建议吗?

tzcvj98z

tzcvj98z1#

如果你一直想同时运行两个独立的WordPress网站,你可以使用一个docker-compose.yml文件同时运行它们。
如果您仍然希望将它们作为两个容器运行,您应该能够将这个docker-compose.yml拆分到两个单独的yml文件中,并且工作方式相同。
下面是一个经过测试的工作docker-compose.yml设置。

version: '3.7'

services:
  
  # site 1 database
  db_1:
    image: mysql:5.7
    networks:
      - wordpress_1
    volumes:
      # persist site 1 volume mapping for database
      - ./site-1/db:/var/lib/mysql:delegated
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  
  # site 1 wordpress
  wordpress_1:
    depends_on:
      - db_1
    image: wordpress:latest
    volumes:
      # persist site 1 volume mapping for files
      - ./site-1/themes/example:/var/www/html/wp-content/themes/example:delegated
      - ./site-1/mu-plugins:/var/www/html/wp-content/mu-plugins
      - ./site-1/plugins:/var/www/html/wp-content/plugins
      - ./site-1/uploads:/var/www/html/wp-content/uploads
      - ./site-1/logs:/var/www/html/wp-content/logs
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./error-logging.ini:/usr/local/etc/php/conf.d/error-logging.ini
    networks:
      - wordpress_1
    ports:
      - '8001:80'
    restart: always
    environment:
      # dev debug mode
      WORDPRESS_DEBUG: 1
      # docker wp config settings for site 1
      WORDPRESS_DB_HOST: db_1
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_AUTH_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_AUTH_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_CONFIG_EXTRA: |
        
        if(!defined('WP_HOME')) {
          /* force our home url for site 1 */
          define('WP_HOME', 'http://localhost:8001');
          define('WP_SITEURL', WP_HOME);
        }

  # site 2 database
  db_2:
    image: mysql:5.7
    networks:
      - wordpress_2
    volumes:
      # persist site 2 volume mapping for database
      - ./site-2/db:/var/lib/mysql:delegated
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  # site 2 wordpress
  wordpress_2:
    depends_on:
      - db_2
    image: wordpress:latest
    volumes:
      # persist site 2 volume mapping for files
      - ./site-2/themes/example:/var/www/html/wp-content/themes/example:delegated
      - ./site-2/mu-plugins:/var/www/html/wp-content/mu-plugins
      - ./site-2/plugins:/var/www/html/wp-content/plugins
      - ./site-2/uploads:/var/www/html/wp-content/uploads
      - ./site-2/logs:/var/www/html/wp-content/logs
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./error-logging.ini:/usr/local/etc/php/conf.d/error-logging.ini
    networks:
      - wordpress_2
    ports:
      - '8002:80'
    restart: always
    environment:
      # dev debug mode
      WORDPRESS_DEBUG: 1
      # docker wp config settings for site 2
      WORDPRESS_DB_HOST: db_2
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_AUTH_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_AUTH_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_CONFIG_EXTRA: |
        
        if(!defined('WP_HOME')) {
          /* force our home url for site 2 */
          define('WP_HOME', 'http://localhost:8002');
          define('WP_SITEURL', WP_HOME);
        }

networks:
  wordpress_1:
  wordpress_2:

字符串
你甚至可能不需要网络位,虽然不确定它将如何运行,我会让你测试。
您只需运行docker-compose up -d即可
这将创建两个独立的本地wordpress安装与持久卷Map到本地文件夹site-1site-2.


的数据
下面是它在docker desktop中运行时的样子。



然后你在这些端口访问wordpress网站。。

  1. http://localhost:8001/(研究中心1)
  2. http://localhost:8002/(站点2)
    在运行docker-compose up -d之前,您必须在docker-compose.yml文件所在的目录中创建两个.ini文件...

uploads.ini

file_uploads = On
memory_limit = 2000M
upload_max_filesize = 2000M
post_max_size = 2000M
max_execution_time = 600

error-logging.ini

error_reporting = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_RECOVERABLE_ERROR ^ E_DEPRECATED
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/www/html/wp-content/logs/debug.log
log_errors_max_len = 1024
ignore_repeated_errors = On
ignore_repeated_source = Off
html_errors = Off


现在您已经有了这三个项目文件,可以运行docker-compose up -d
每次安装docker-compose downdocker-compose up -d时,安装设置和数据都是相同的。

相关问题