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 overleaf中论文编辑,报错`pages' is a missing field, not a string, for entry 4
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么