Docker环境变量问题
黄舟
黄舟 2017-04-24 09:10:12
0
2
824

Docker环境变量问题,比如,在Docker Hub上下载了一个nginx镜像,现在要把我自己的配置文件用上去,看到说可以有三种方式:
1、用环境变量传递配置。
2、用数据卷,把配置文件放到数据卷中。
3、修改镜像,保存为新的镜像。

现在,我想采用第1种方法,文档中关于环境变量的内容如下:

可是怎么配置,仍然有点不清楚。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

membalas semua(2)
Ty80

Siapa memberitahu anda bahawa pembolehubah persekitaran boleh digunakan untuk memindahkan fail konfigurasi...
Keupayaan untuk menggunakan pembolehubah persekitaran bermakna sesetengah perisian boleh mengenali kedua-dua konfigurasi yang disediakan dalam pembolehubah persekitaran dan konfigurasi dalam bentuk fail, Malangnya, Nginx tidak disertakan.

Mengambil wget sebagai contoh, apabila menetapkan proksi HTTP, anda boleh memilih mana-mana kaedah berikut:

  • Tambah baris dalam fail konfigurasihttp_proxy = http://example.com/xxx

  • Tetapkan pembolehubah persekitaran sistem $http_proxy kepada http://example.com/xxx

Dua kaedah mempunyai kesan yang sama.

Sudah tentu, anda boleh menulis skrip Shell dalam bekas Docker untuk mengubah suai kandungan fail konfigurasi berdasarkan pembolehubah persekitaran, tetapi ini topik lain. Terdapat banyak alat di Github yang melaksanakan fungsi yang serupa, seperti Tiller.

Maklumat tambahan: Bagaimana Saya Harus Mendapatkan Konfigurasi Aplikasi ke dalam Bekas Docker saya?

Peter_Zhu

Oh, anda perlu membawa parameter semasa memulakan bekas, seperti:
./docker run --detach --name wordpress --net wordnet --publish 80:80 --env WORDPRESS_DB_HOST=mysql -- env WORDPRESS_DB_PASSWORD =xxxxxxxx wordpress:4.4

Sudah tentu, parameter yang dibawa masuk mesti diproses dalam arahan permulaan kontena:

cat /entrypoint.sh

#!/bin/bash
set -e

if [[ "" == apache2* ]] || [ "" == php-fpm ]; then
    if [ -n "$MYSQL_PORT_3306_TCP" ]; then
        if [ -z "$WORDPRESS_DB_HOST" ]; then
            WORDPRESS_DB_HOST='mysql'
        else
            echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found'
            echo >&2 "  Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)"
            echo >&2 '  instead of the linked mysql container'
        fi
    fi

    if [ -z "$WORDPRESS_DB_HOST" ]; then
        echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables'
        echo >&2 '  Did you forget to --link some_mysql_container:mysql or set an external db'
        echo >&2 '  with -e WORDPRESS_DB_HOST=hostname:port?'
        exit 1
    fi

    # if we're linked to MySQL and thus have credentials already, let's use them
    : ${WORDPRESS_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}}
    if [ "$WORDPRESS_DB_USER" = 'root' ]; then
        : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
    fi
    : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD}
    : ${WORDPRESS_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-wordpress}}

    if [ -z "$WORDPRESS_DB_PASSWORD" ]; then
        echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable'
        echo >&2 '  Did you forget to -e WORDPRESS_DB_PASSWORD=... ?'
        echo >&2
        echo >&2 '  (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)'
        exit 1
    fi

    if ! [ -e index.php -a -e wp-includes/version.php ]; then
        echo >&2 "WordPress not found in $(pwd) - copying now..."
        if [ "$(ls -A)" ]; then
            echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
            ( set -x; ls -A; sleep 10 )
        fi
        tar cf - --one-file-system -C /usr/src/wordpress . | tar xf -
        echo >&2 "Complete! WordPress has been successfully copied to $(pwd)"
        if [ ! -e .htaccess ]; then
            # NOTE: The "Indexes" option is disabled in the php:apache base image
            cat > .htaccess <<-'EOF'
                # BEGIN WordPress
                <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteBase /
                RewriteRule ^index\.php$ - [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
                </IfModule>
                # END WordPress
            EOF
            chown www-data:www-data .htaccess
        fi
    fi

    # TODO handle WordPress upgrades magically in the same way, but only if wp-includes/version.php's $wp_version is less than /usr/src/wordpress/wp-includes/version.php's $wp_version

    # version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
    # https://github.com/docker-library/wordpress/issues/116
    # https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
    sed -ri 's/\r\n|\r/\n/g' wp-config*

    if [ ! -e wp-config.php ]; then
        awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    $_SERVER['HTTPS'] = 'on';
}

EOPHP
        chown www-data:www-data wp-config.php
    fi

    # see http://stackoverflow.com/a/2705678/433558
    sed_escape_lhs() {
        echo "$@" | sed 's/[]\/$*.^|[]/\&/g'
    }
    sed_escape_rhs() {
        echo "$@" | sed 's/[\/&]/\&/g'
    }
    php_escape() {
        php -r 'var_export(('') $argv[1]);' ""
    }
    set_config() {
        key=""
        value=""
        var_type="${3:-string}"
        start="(['\"])$(sed_escape_lhs "$key")\s*,"
        end="\);"
        if [ "${key:0:1}" = '$' ]; then
            start="^(\s*)$(sed_escape_lhs "$key")\s*="
            end=";"
        fi
        sed -ri "s/($start\s*).*($end)$/$(sed_escape_rhs "$(php_escape "$value" "$var_type")")/" wp-config.php
    }

    set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
    set_config 'DB_USER' "$WORDPRESS_DB_USER"
    set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
    set_config 'DB_NAME' "$WORDPRESS_DB_NAME"

    # allow any of these "Authentication Unique Keys and Salts." to be specified via
    # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
    UNIQUES=(
        AUTH_KEY
        SECURE_AUTH_KEY
        LOGGED_IN_KEY
        NONCE_KEY
        AUTH_SALT
        SECURE_AUTH_SALT
        LOGGED_IN_SALT
        NONCE_SALT
    )
    for unique in "${UNIQUES[@]}"; do
        eval unique_value=$WORDPRESS_$unique
        if [ "$unique_value" ]; then
            set_config "$unique" "$unique_value"
        else
            # if not specified, let's generate a random value
            current_set="$(sed -rn "s/define\((([\'\"])$unique\s*,\s*)(['\"])(.*)\);//p" wp-config.php)"
            if [ "$current_set" = 'put your unique phrase here' ]; then
                set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
            fi
        fi
    done

    if [ "$WORDPRESS_TABLE_PREFIX" ]; then
        set_config '$table_prefix' "$WORDPRESS_TABLE_PREFIX"
    fi

    if [ "$WORDPRESS_DEBUG" ]; then
        set_config 'WP_DEBUG' 1 boolean
    fi

    TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
<?php
// database might not exist, so let's try creating it (just to be safe)

$stderr = fopen('php://stderr', 'w');

list($host, $port) = explode(':', $argv[1], 2);

$maxTries = 10;
do {
    $mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
    if ($mysql->connect_error) {
        fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
        --$maxTries;
        if ($maxTries <= 0) {
            exit(1);
        }
        sleep(3);
    }
} while ($mysql->connect_error);

if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) {
    fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");
    $mysql->close();
    exit(1);
}

$mysql->close();
EOPHP
fi

exec "$@"
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan