将WP-CLI容器添加到LEMP堆栈

时间:2019-01-19 14:50:47

标签: docker-compose dockerfile

Docker version 18.09.1, build 4c52b90
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

我有一个工作正常的LEMP堆栈,其中包含以下docker-compose文件。

version: "3.7"

services:
  php:
    build: 
      context: './config/php/'
    networks:
      - dev
    volumes:
      - /WORK/:/var/www/html/

    working_dir: /var/www/html/
    container_name: php

  nginx:
    image: nginx:1.14-alpine
    depends_on:
      - php
      - mysql
    networks:
      - dev      
    ports:
      - "80:80"
    volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - /WORK/:/var/www/html/
    container_name: nginx

  mysql:
    image: mysql:5.7
    restart: always
    ports:
      - "3306:3306"
    volumes:
      - /MYSQL/:/var/lib/mysql
    networks:
      - dev      
    environment:
      MYSQL_ROOT_PASSWORD: "db_root"
      MYSQL_DATABASE: "test_db"
      MYSQL_USER: "test_db_user"
      MYSQL_PASSWORD: "test_db_pass"
    container_name: mysql

  wpcli:
    image: wordpress:cli
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql 
      WORDPRESS_DB_NAME: test_db
      WORDPRESS_DB_USER: test_db_user
      WORDPRESS_DB_PASSWORD: test_db_pass
    networks:
      - dev
    command: '--path=`/var/www/html/WP-Site/`'      
    volumes:
      - /WORK/:/var/www/html/
      - /MYSQL/:/var/lib/mysql
    working_dir: /var/www/html/
    container_name: wpcli

networks:
  dev:

PHP映像的Dockerfile包含此文件。

FROM php:7.2-fpm-alpine

# docker-entrypoint.sh dependencies
RUN apk add --no-cache \
# in theory, docker-entrypoint.sh is POSIX-compliant, but priority is a working, consistent image
        bash \
# BusyBox sed is not sufficient for some of our sed expressions
        sed

# https://github.com/docker-library/wordpress/blob/master/php7.3/fpm-alpine/Dockerfile
# install the PHP extensions we need
RUN set -ex; \
    \
    apk add --no-cache --virtual .build-deps \
        libjpeg-turbo-dev \
        libpng-dev \
        libzip-dev \
    ; \
    \
    docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
    docker-php-ext-install gd mysqli opcache zip; \
    \
    runDeps="$( \
        scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
            | tr ',' '\n' \
            | sort -u \
            | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )"; \
    apk add --virtual .wordpress-phpexts-rundeps $runDeps; \
    apk del .build-deps

NGINX conf是这个。

upstream php {  
  server php:9000;
}

server {

        listen                  80;       
        server_name             localhost;

        ## Your only path reference.
        root /var/www/html;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php index.html index.htm;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # https://nginxlibrary.com/enable-directory-listing/
                autoindex on;

                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

除WP-CLI之外的所有服务均有效。我的文件夹结构是这样,以便在主WORK文件夹下有子文件夹,每个子文件夹中都有一个项目。

此类项目可以是WordPress,但也可以使用其他CMS或只是小型静态站点。

http://localhost/下,我看到WORK文件夹及其所有子文件夹的概述。点击任何项目文件夹,例如WP-Site,将引导我进入http://localhost/WP-Site/。在那里,我有一个可以正常工作的WordPress安装程序,可以很好地连接到PHP和MYSQL服务。

但是当尝试使用WP-CLI时,它要么告诉我它找不到有效的WordPress安装,然后当我在服务中传递命令并将其提供给我其中一个WordPress项目的路径时,它就会退出而不会错误消息。

我是否真的需要WordPress图像才能使用WP-CLI图像。在WP-CLI服务中,我还提供了与MYSQL服务相同的环境变量,以便WP-CLI可以知道连接到哪里。

如果有任何方法可以运行WP-CLI容器并将其放在主WORK文件夹下,那么对于我可能进行的所有项目,那都很好。

我考虑将所有内容都安装到一个映像中,但是这违背了Docker的宗旨,即能够将事物分离和互换。因此,我希望能够制作一个确实能够连接到我的PHP和MYSQL容器的WP-CLI容器。

0 个答案:

没有答案