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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器