处理克隆容器上的不同配置文件

时间:2019-03-08 18:51:19

标签: docker docker-compose

我需要根据相同的源代码但使用不同的配置文件来生成2个容器。

第一个前提条件是两个实例必须修改其配置文件(来自docker-entrypoint.sh ATM),但不得相互影响或影响主机。

第二个先决条件(也是我受困的地方)是,我还希望将主机上源代码的更改反映到容器上,以免每次进行更改时都重新构建它们。 我以为可以创建一个tmpfs卷来允许容器轻松管理自己的配置,而不保留它,但是Windows上似乎无法使用此功能,并且我团队中的一些成员正在使用该OS。

有人有想法进行这项工作吗?如果有什么用,目标是使开发人员可以轻松地在此项目上进行工作,而无需麻烦地构建相应的体系结构(2个实例通过仅允许单向流使用RMQ的网络相互通信)。 / p>

  

docker-compose.yml

version: "2.4"

networks:
    gateway_network:
        name: "gateway_network"

volumes:
    gateway_data:
        name: "gateway_data"

services:
    rabbitmq:
        container_name: "rmq"
        image: "rabbitmq:3.7"
        expose:
            - "5672"

    php_internal:
        container_name: "php-int"
        build: "."
        networks:
          - "gateway_network"
        environment:
            - "APP_ENV=dev"
            - "GW_TYPE=internal"
        volumes:
            - "gateway_data:/data/ssl"
            - ".:/www"
        expose:
            - "9000"
        depends_on:
            - rabbitmq

    php_external:
        container_name: "php-ext"
        build: "."
        networks:
            - "gateway_network"
        environment:
            - "APP_ENV=dev"
            - "GW_TYPE=external"
        volumes:
            - "gateway_data:/data/ssl"
            - ".:/www"
        expose:
            - "9000"
        depends_on:
            - php_internal
        restart: always

    web_internal:
        container_name: "gw-int"
        image: "nginx:latest"
        networks:
            - "gateway_network"
        ports:
            - "8000:443"
        volumes:
            - ".:/www"
            - "./docker/nginx/internal.conf:/etc/nginx/conf.d/default.conf"
            - "gateway_data:/data/ssl"
        depends_on:
            - php_internal
        restart: always

    web_external:
        container_name: "gw-ext"
        image: "nginx:latest"
        networks:
            - "gateway_network"
        ports:
            - "8001:443"
        volumes:
            - ".:/www"
            - "./docker/nginx/external.conf:/etc/nginx/conf.d/default.conf"
            - "gateway_data:/data/ssl"
        depends_on:
            - php_external
        restart: always
  

Dockerfile

# See https://hub.docker.com/_/php/#how-to-use-this-image
# See https://github.com/TrafeX/docker-php-nginx/

ARG PHP_VERSION=7.1

FROM php:${PHP_VERSION}-fpm-alpine
# Use "latest" to get the most recent version of composer

RUN set -eux; apk --no-cache --update add \
    acl curl libgd freetype-dev libjpeg-turbo-dev libpng-dev bzip2-dev libxml2-dev curl-dev icu-dev \
    openldap-dev imap-dev sqlite-dev git \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd intl bcmath bz2 ldap pdo_mysql

RUN set -eux; CFLAGS="-I/usr/src/php" docker-php-ext-install xmlreader
RUN set -eux; docker-php-ext-configure imap --with-imap --with-imap-ssl \
    && docker-php-ext-install imap pcntl sockets

COPY --from=composer:1.8.4 /usr/bin/composer /usr/bin/composer

COPY ./docker/fpm/fpm-pool.conf /etc/php7/php-fpm.d/www.conf
COPY ./docker/php/custom_php.ini /etc/php7/conf.d/custom_php.ini

VOLUME [ "/www/var", "/data/ssl" ]
COPY ./app/config/parameters.yml.dist /www/app/config/parameters.yml

WORKDIR /www

COPY ./docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]

EXPOSE 9000

  

docker-entrypoint.sh

#!/usr/bin/env sh
set -e

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
    set -- php-fpm "$@"
fi

if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
    rm -rf var/cache var/log
    mkdir -p var/cache var/log
    setfacl -R -m u:www-data:rwX -m u:"$(whoami)":rwX var
    setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX var

    setfacl -R -m u:www-data:rwX -m u:"$(whoami)":rwX /var
    setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX /var

    if [ "$GW_TYPE" = 'internal' ]; then
        # Generate cert for the internal gateway to authenticate itself
        openssl genpkey -algorithm RSA -out /data/ssl/private_key.pem -pkeyopt rsa_keygen_bits:2048
        openssl rsa -pubout -in /data/ssl/private_key.pem -out /data/ssl/public_key.pem

        # Generate self-signed HTTPS cert for nginx instances
        openssl genrsa -out /data/ssl/webserver_ca.key 4096
        openssl req -new -x509 -days 3650 -key /data/ssl/webserver_ca.key -out /data/ssl/webserver_ca.crt -subj "some_subject"

        openssl genrsa -out /data/ssl/webserver.key 4096
        openssl req -new -key /data/ssl/webserver.key -out /data/ssl/webserver.csr -subj "some_subject"
        openssl x509 -req -days 3650 -in /data/ssl/webserver.csr -CA /data/ssl/webserver_ca.crt -CAkey /data/ssl/webserver_ca.key -set_serial 01 -out /data/ssl/webserver.crt


        openssl genrsa -out /data/ssl/client.key 4096
        openssl req -new -key /data/ssl/client.key -out /data/ssl/client.csr -subj "some_subject"
        openssl x509 -req -days 3650 -in /data/ssl/client.csr -CA /data/ssl/webserver_ca.crt -CAkey /data/ssl/webserver_ca.key -set_serial 01 -out /data/ssl/client.crt
    fi

    if [ "$APP_ENV" != 'prod' ]; then
        composer install --prefer-dist --no-progress --no-suggest --no-interaction
    fi

    echo "Starting..."
fi

exec docker-php-entrypoint "$@"

1 个答案:

答案 0 :(得分:3)

使用Docker Compose,您可以在命令提示符或脚本中的一些命令中轻松创建和销毁隔离的环境,例如以下命令:

docker-compose -f docker-compose.yml -f docker-compose-test.override.yml up -d

您应该使用多个撰写文件。这样,您可以根据环境创建多个配置变体。

覆盖基本的docker-compose文件

一个典型的用例是当您定义多个撰写文件时,您可以针对多个环境,例如生产,登台,CI或开发。为了支持这些差异,您可以将Compose配置分为多个文件,例如docker-compose.ymldocker-compose.prod.ymldocker-compose.staging.yml

在基本docker-compose.yml文件中,您只想设置跨环境通用的信息。然后,在docker-compose.override.yml或用于生产或登台的类似文件中,应放置特定于每种环境的配置。

请参见以下图片: enter image description here

来源: