dsy1971 2018-03-13 10:47
浏览 930
已采纳

如何使用Docker连接php-apache和MySQL?

I have a Wordpress site on live server and I want to create a LAMP stack locally with Docker to test things.

I pull the images of php:7.0-apache and mysql:5.7, the same versions on live.

I create a MySQL container:

docker run -d --name achi-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

I create a php & apache container and link it with MySQL:

docker run -d --name achi-php-apache --link achi-mysql:mysql -p 8080:80 -v /home/achi/workspace/web/wordpress-template/:/var/www/html php:7.0-apache

I get the following error on localhost:8080:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php:1564 [...]

Do I link these two containers the wrong way?

  • 写回答

2条回答 默认 最新

  • douzhoubing2805 2018-03-13 19:50
    关注

    Your problem is not the connection between your containers. The problem is your PHP / Apache container which doesn't support mysqli (or PDO MySQL). WordPress can't find another function to connect with a MySQL database or your MySQL container. Instead WordPress is using a deprecated and removed (since PHP 7.0) mysql_ function per default. You need to install at least mysqli on your PHP container (explained below).

    I also recommend to use a docker-compose file to install and run all containers with one command.

    To create the containers you want, you can use the following docker-compose.yml file:

    version: "3"
    
    services:
      achi-php-apache:
        build:
          context: ./
        container_name: achi-php-apache
        ports:
          - "8080:80"
        volumes:
          - /home/achi/workspace/web/wordpress-template:/var/www/html:rw
        depends_on:
          - achi-mysql
        networks:
          - wp-net
      achi-mysql:
        image: mysql:5.7
        container_name: achi-mysql
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 123456
          MYSQL_DATABASE: wp-dbname
        volumes:
          - wp-mysql-data:/var/lib/mysql
        networks:
          - wp-net
    
    networks:
      wp-net:
        driver: bridge
    
    volumes:
      wp-mysql-data:
    

    You need the following Dockerfile on the same directory as the docker-compose.yml file:

    FROM php:7.0-apache
    
    RUN docker-php-ext-install -j$(nproc) mysqli
    

    This Dockerfile installs the missing mysqli extension so WordPress can use it.

    You can also use PDO MySQL instead of mysqli. In this case you can use the following Dockerfile:

    FROM php:7.0-apache
    
    RUN docker-php-ext-install -j$(nproc) pdo
    RUN docker-php-ext-install -j$(nproc) pdo_mysql
    

    Now you can execute the command docker-compose up inside the folder where the docker-compose.yml file is located. After creating the container and running you should be able to access the WordPress site (<ip-or-hostname>:8080).

    On the wp-config.php file you need to use the following constants:

    define('DB_NAME', 'wp-dbname');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '123456');
    define('DB_HOST', 'achi-mysql');
    

    You can also use the official WordPress image to install WordPress. In this case you can use the following docker-compose.yml file:

    version: "3"
    
    services:
      achi-php-apache:
        image: wordpress:4.9.4-php7.0-apache
        container_name: achi-php-apache
        ports:
          - "8080:80"
        environment:
          WORDPRESS_DB_HOST: achi-mysql
          WORDPRESS_DB_USER: root
          WORDPRESS_DB_PASSWORD: 123456
          WORDPRESS_DB_NAME: wp-dbname
        volumes:
          - /home/achi/workspace/web/wordpress-template:/var/www/html:rw
        depends_on:
          - achi-mysql
        networks:
          - wp-net
      achi-mysql:
        image: mysql:5.7
        container_name: achi-mysql
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 123456
          MYSQL_DATABASE: wp-dbname
        volumes:
          - wp-mysql-data:/var/lib/mysql
        networks:
          - wp-net
    
    networks:
      wp-net:
        driver: bridge
    
    volumes:
      wp-mysql-data:
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵