Docker 组合 php-fpm、nginx、MySQL 并在 nginx 上使用 wp-cli

问题描述 投票:0回答:1

我将 3 个独立的容器链接在一起:

我的目标是在将源代码复制到 nginx 容器之前运行 wp-cli 安装。

问题:

  1. 尝试从 Dockerfile (my-nginx 映像)运行 wp-cli 安装脚本工作正常,但是当我尝试运行任何 wp-cli 命令时,它会返回错误:
    env: can't execute 'php': No such file or directory
  2. wp-cli 安装脚本(具有正确的依赖项)也适用于 dockerfile.php-fpm (my-php 映像),它也会返回错误:
    Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress install exists under.
    并让我使用标志 --allow 运行此命令-root,在这种情况下这很糟糕,因为我想使用 wp 用户权限。或以其他用户身份运行此命令。

#核心配置

docker-compose.yml:

version: '2'
services:

my-nginx:
    build: .
    volumes:
      - .:/var/www/html
    ports:
      - "8080:80"
    links:
      - my-php
      - my-mysql
  my-php:
    build:
      context: .
      dockerfile: Dockerfile.php-fpm
    volumes:
      - .:/var/www/html
    ports:
      - "9000:9000"
  my-mysql:
    image: mariadb:5.5
    volumes:
      - /var/lib/mysql
 

nginx.conf:

server {
  server_name _;
  listen 80 default_server;

  root   /var/www/html;
  index  index.php index.html;

  access_log /dev/stdout;
  error_log /dev/stdout info;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ .php$ {
    include fastcgi_params;
    fastcgi_pass my-php:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

wp-cli 集成克隆自:https://github.com/conetix/docker-wordpress-wp-cli

#选项1
Dokerfile:

FROM nginx:1.10-alpine

# add root dir
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
ENV WP_ROOT /var/www/html

# Install requirements for wp-cli support
RUN apk update \
  && apk add less mysql-client sudo curl


# copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
   
    
# Add WP-CLI 
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
COPY wp-su.sh /bin/wp
RUN chmod +x /bin/wp-cli.phar \
    && sudo mv /bin/wp-cli.phar /usr/local/bin/wp

RUN  wp core download --allow-root
   
# copy all files for current dir (should be theme or plugin folder)
COPY . ./

Dockergile.php-fpm

FROM php:7.0.6-fpm-alpine  

VOLUME /var/www/html
RUN docker-php-ext-install -j $(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1)  iconv gd mbstring fileinfo curl xmlreader xmlwriter mysqli

#选项2
Dokerfile:

FROM nginx:1.10-alpine

# add root dir
RUN mkdir -p /var/www/html
WORKDIR /var/www/html

RUN apk update \
  && apk add less mysql-client sudo


# copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
   
   
# copy all files for current dir (should be theme or plugin folder)
COPY . ./

Dockergile.php-fpm

FROM php:7.0.6-fpm-alpine  

VOLUME /var/www/html
RUN docker-php-ext-install -j $(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1)  iconv gd mbstring fileinfo curl xmlreader xmlwriter mysqli

# Install requirements for wp-cli support
RUN apk update \
    && apk add curl sudo less
ENV WP_ROOT /var/www/html

# Add WP-CLI 
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
COPY wp-su.sh /bin/wp
RUN chmod +x /bin/wp-cli.phar \
    && sudo mv /bin/wp-cli.phar /usr/local/bin/wp

RUN  wp core download

一般问题:

  1. docker 内用户权限的最佳实践是什么?
  2. 来自
  3. https://github.com/conetix/docker-wordpress-wp-cli/blob/master/wp-su.sh 的 wp-cli 集成使用 www-data 用户 - 我应该将此用户添加到我的 docker 中吗?如果是的话 - 给哪一个? ngnix 或 php-fpm 以及为什么。
  4. 从 php-fpm 容器运行 wp download core 看起来好像下载了文件,但是当我在运行时尝试执行时, /var/www/html 中没有显示这些文件。有没有某种方法可以命令图像文件安装?
  5. 如果我音量大,为什么“php”无法从 my-nginx 获得命令
  6. my-php 图像?我还必须在这张图片上安装 php 吗?
wordpress docker docker-compose
1个回答
8
投票
PHP 未安装在您的 nginx 容器中。我认为它也不应该安装在那里。使用 php fpm 容器运行 WP CLI。以下是我如何使用类似的设置运行 WP CLI:

docker-compose.yml:

version: '2' services: mysql: image: mysql:latest volumes: - "../.data/db:/var/lib/mysql" restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: my_database MYSQL_USER: my_user MYSQL_PASSWORD: password phpfpm: depends_on: - mysql image: my/phpfpm:latest build: ./docker/php-fpm volumes: - ".:/var/www/html" - "./docker/php-fpm/php.ini:/usr/local/etc/php/php.ini" - "./docker/php-fpm/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini" links: - mysql restart: always extra_hosts: - "mysite.dev:172.18.0.1" # Use the gateway address for your docker network for the ip address. This is so that PHP FPM can find nginx for the postback to do things like cron jobs with WordPress nginx: depends_on: - phpfpm ports: - "80:80" image: nginx:latest volumes: - ".:/var/www/html" - "./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf" links: - phpfpm restart: always

docker/php-fpm/Dockerfile:

FROM php:5.5-fpm ARG INSTALL_XDEBUG=true ENV INSTALL_XDEBUG ${INSTALL_XDEBUG} RUN if [ ${INSTALL_XDEBUG} = true ]; then \ pecl install xdebug && \ docker-php-ext-enable xdebug \ ;fi RUN apt-get update && apt-get install -y libz-dev libmemcached-dev libjpeg-dev libpng-dev \ && pecl install memcached \ && docker-php-ext-enable memcached \ && docker-php-ext-install -j$(nproc) pdo pdo_mysql mysqli gd \ && docker-php-ext-enable pdo pdo_mysql mysqli gd RUN docker-php-ext-install zip \ && docker-php-ext-enable zip RUN curl https://getcomposer.org/download/1.2.0/composer.phar > /tmp/composer.phar \ && chmod +x /tmp/composer.phar \ && mv /tmp/composer.phar /usr/local/bin/composer \ && apt-get update && apt-get install -y less \ && curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > /tmp/wp-cli.phar \ && chmod +x /tmp/wp-cli.phar \ && mv /tmp/wp-cli.phar /usr/local/bin/wp CMD ["php-fpm"] EXPOSE 9000

我还使用 memcached 容器,因此 dockerfile 也安装了 memcached 扩展。重要的部分是安装 WP CLI 并将其移动到位。您还可以通过在该 dockerfile 中进行更改来修改已安装的 PHP 版本。一旦您的集群启动并运行并在 PHP 上安装了 WP CLI,您就可以运行以下命令在容器内运行 wp cli 命令:

docker exec -u www-data <container_name> wp ... # whatever command after that

我有已经具有容器名称的不同项目的别名:

alias mywp="docker exec -u www-data mysite_phpfpm_1 wp"

这让我可以像这样运行 wp 命令:

mywp core update-db --network mywp post meta list 2119
    
© www.soinside.com 2019 - 2024. All rights reserved.