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 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格