码头工人:git clone结果不在本地卷中,args不扩展

时间:2018-10-19 21:38:48

标签: git docker docker-compose

我正在尝试通过Docker站起一个PHP application。我可以手动使用它,但是我正在尝试按照最佳做法(例如:单责任形象等)对其进行改进(使其在CI / CD环境中更加可用)

TLDR:

  1. 如何可靠地将git repo克隆到本地共享卷中?
  2. 如何从.env文件转到特定的git标签?

问题:

  1. 将git复制到映射到本地卷的路径中导致本地路径中没有数据/文件
    • 我发现有文章提到在构建过程中较早映射空卷将导致路径为空(但我认为这并不直接适用于此)。
  2. docker-compose配置将显示TAO_CODE_TAG的正确值,但在结帐期间失败(视为空)
    • 我知道,根据在dockerfile中的调用方式,可能不会发生变量的shell扩展。但是,我看到了它与RUN echo $var一起工作的工作示例?

./。env

PHP_VERSION=7.0
MYSQL_VERSION=5.7
APACHE_VERSION=2.4.32
TAO_CODE_TAG=v3.2-rc02
DB_ROOT_PWD=password ;-)

./ docker-compose.yml

version: "3.2"

services:
  git:
    build:
      context: './git/'
      args:
        TAO_CODE_TAG: ${TAO_CODE_TAG:?err}
    volumes:
      - ./tao-src/:/build
    container_name: git

  composer:
    image: composer/composer:php7  #  Would be nice to use tag from .env for php version
    command: install
    depends_on:
      - git
    volumes:
      - ./tao-src/:/app
    container_name: composer

  php:
    build:
      context: './php/'
      args:
        PHP_VERSION: ${PHP_VERSION:?err}
    depends_on:
      - composer
    networks:
      - backend
    ports:
      - 9000:9000
    volumes:
      - ./tao-src/:/var/www/html/
    container_name: php

  apache:
    build:
      context: './apache/'
      args:
        APACHE_VERSION: ${APACHE_VERSION:?err}
    depends_on:
      - git
      - php
      - mysql
    networks:
      - frontend
      - backend
    ports:
      - 80:80
    volumes:
      - ./tao-src/:/var/www/html/
    container_name: apache

  mysql:
    image: mysql:${MYSQL_VERSION:-latest}
    networks:
      - backend
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PWD:?err}
    container_name: mysql

networks:
  frontend:
  backend:

./ apache / Dockerfile

ARG APACHE_VERSION=""
FROM httpd:${APACHE_VERSION:+${APACHE_VERSION}-}alpine

# Copy apache vhost file to proxy php requests to php-fpm container
COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
    >> /usr/local/apache2/conf/httpd.conf

./ apache / demo.apache.conf

ServerName localhost

LoadModule rewrite_module /usr/local/apache2/modules/mod_rewrite.so
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so

<VirtualHost *:80>
    # Proxy .php requests to port 9000 of the php-fpm container
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
    DocumentRoot /var/www/html/
    <Directory /var/www/html/>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Send apache logs to stdout and stderr
    CustomLog /proc/self/fd/1 common
    ErrorLog /proc/self/fd/2
</VirtualHost>

.// git / Dockerfile

ARG TAO_CODE_TAG
FROM alpine/git:1.0.4
RUN apk add git \
    && mkdir /build \
    && git clone https://github.com/oat-sa/package-tao /build \
    && cd /build \
    && git checkout tags/v3.2-rc02 # Should use val from .env!
VOLUME ["/build"] # Last in file so as to not "disappear" during build

./ php / Dockerfile

ARG PHP_VERSION=""
FROM php:${PHP_VERSION:+${PHP_VERSION}-}fpm-alpine

# One run/layer as needed/able per recommendation in docs
# https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run
RUN apk update \
    # TAO requires zip support
    && apk add libzip-dev zip \
    && docker-php-ext-configure zip --with-libzip \
    && docker-php-ext-install zip \
    # TAO requires pdo_mysql support
    && docker-php-ext-install pdo_mysql \
    # TAO recommends opcache
    && docker-php-ext-install opcache \
    && docker-php-ext-configure opcache --enable-opcache \
    # Ensure a php.ini is present
    && mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini;

PS:最终,诸如使用秘密服务器获取安全凭据等之类的事情以及将自动install script.env文件一起使用的事情将实现为好吧。

1 个答案:

答案 0 :(得分:1)

我现在可以进入Web安装页面。所以,对于任何有兴趣的人...

#1-源/克隆问题

我意识到我所需要的只是compose.jsonindex.php文件,我可以通过<?php // start $stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'"); $stmt->execute(); if($stmt->rowCount() > 0) { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); //end ?> <form method="get" action="btnchange.php"> <input type="hidden" name="prod_id" value="<?php echo $row['cartno']; ?>"> <tbody> <tr> <td><?php echo $row['cartno']; ?></td> <td><?php echo $row['cartname']; ?></td> <td><?php echo $row['cartprice']; ?></td> <td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td> <td><?php echo $row['cartsub']; ?></td> <td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td> <td><button type="submit" name="submit">Submit Quantity</button></b> <td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td> </tr> </tbody> </form> jsdeliver中的dockerfile statements来抓取,然后递归chown / chmod来获取php和apache ADD文件夹中的新文件。

注意:这并不能真正解决通常从github获取源代码的问题,但足以满足我对特定代码库/项目的实际/即刻需求。

#2-标签问题

我通过...解决了标签问题

docker-compose.yml

/var/www/html

php / Dockerfile

version: "3.2"

services:
  php:
    build:
      context: './php/'
      args:
        PHP_VERSION: ${PHP_VERSION:?err}
        TAO_CODE_TAG: ${TAO_CODE_TAG:?err} # <-- IMPORTANT

我目前正在通过在php和apache派生的图像容器中复制一个多阶段构建来解决代码/ composer-install的问题,如以上各行所示,然后...

php / Dockerfile

FROM composer/composer as Composed
ARG TAO_CODE_TAG # <-- IMPORTANT
RUN mkdir /build && cd /build
WORKDIR /build
ADD https://cdn.jsdelivr.net/gh/oat-sa/package-tao@${TAO_CODE_TAG}/composer.json /build/composer.json # <-- IMPORTANT
RUN composer install --ignore-platform-reqs --no-scripts --no-suggest --no-dev

# ...continues

不幸的是,这对于php和apache实例都是重复的(要求将代码都放入这两个实例中。)我仍在研究一种创建共享的仅源资源的方法,该资源可以通过共享来访问和映射(大多数可能)。