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

我在实时服务器上有一个Wordpress站点,我想在本地用Docker创建一个LAMP堆栈来测试。< / p>

我将 php:7.0-apache </ code>和 mysql:5.7 </ code>的图片拉到了相同的版本上。</ p> \ n

我创建一个MySQL容器:</ p>

  docker run -d --name achi-mysql -e MYSQL_ROOT_PASSWORD = 123456 mysql:5.7 
</ code> </ pre>

我创建了一个php&amp; apache容器并将其与MySQL链接:</ p>

  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 
</ code> </ pre>

我在localhost上收到以下错误:8080 :</ p>

致命错误:未捕获错误:调用/var/www/html/wp-includes/wp-db.php:1564中的未定义函数mysql_connect()[.. 。] </ code> </ p>

我是否以错误的方式链接这两个容器?<​​/ p>
</ div>

展开原文

原文

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?

dtoaillwk759656786
dtoaillwk759656786 还有一个可以尝试的wordpress图像。以下是可能有用的指南:docs.docker.com/compose/wordpress/
2 年多之前 回复
douzi9430
douzi9430 wordpressv4.9.4
2 年多之前 回复
dsdsds12222
dsdsds12222 自PHP7.0起,不推荐使用mysql_*函数。wordpress版本?
2 年多之前 回复

2个回答

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:
dongmo9996
dongmo9996 谢谢,我使用了你描述的第一种方法(不是wordpress图像)。
2 年多之前 回复



最简单的方法是使用docker-compose将所有docker实例链接在一起,而不是通过docker命令链接。 下面是一个示例docker-compose.yml文件,可以执行您想要的操作:</ p>

  version:'2'
services:
achi-php-apache:
image :php:7.0-apache
ports:
  • “8080:80” 卷:
  • / home / achi / workspace / web / wordpress-template /:/ var / www / html links:\ n - achi-mysql achi-mysql: image:mysql:5.7 卷:
  • / var / lib / mysql ports:
  • “3306:3306” environment: MYSQL_ROOT_PASSWORD :123456 MYSQL_USER:someuser MYSQL_PASSWORD:somepassword MYSQL_DATABASE:somedefaultdatabase </ code> </ pre> </ div>

展开原文

原文

The simplest way is to use docker-compose to link all your docker instances together rather than linking through the docker command. Here is a sample docker-compose.yml file that should do what you want:

version: '2'
services:
  achi-php-apache:
    image: php:7.0-apache
    ports:
     - "8080:80"
    volumes:
     - /home/achi/workspace/web/wordpress-template/:/var/www/html
    links:
     - achi-mysql
  achi-mysql:
    image: mysql:5.7
    volumes:
     - /var/lib/mysql
    ports:
     - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: someuser
      MYSQL_PASSWORD: somepassword
      MYSQL_DATABASE: somedefaultdatabase

duanbeng6709
duanbeng6709 是的,我认为您可能需要更新PHP设置以允许mysql_函数,但是如果您可以将wordpress更新为使用mysqli_ functions而不是更安全。
2 年多之前 回复
dpq755012465
dpq755012465 谢谢,这个解决方案很干净,但我仍然遇到同样的错误。 是不是我的PHP图像缺少一些功能? 我虽然使用官方7.0。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐