• docker容器交付配置管理


    参考 https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file

    Docker容器配置管理

    Set environment variables (-e, --env, --env-file)
    
    $ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash
    
    Use the -e, --env, and --env-file flags to set simple (non-array) environment variables in the container you’re running, or overwrite variables that are defined in the Dockerfile of the image you’re running.
    
    You can define the variable and its value when running the container:
    
    $ docker run --env VAR1=value1 --env VAR2=value2 ubuntu env | grep VAR
    
    VAR1=value1
    
    VAR2=value2
    
    You can also use variables that you’ve exported to your local environment:
    
    export VAR1=value1
    
    export VAR2=value2
    
     
    
    $ docker run --env VAR1 --env VAR2 ubuntu env | grep VAR
    
    VAR1=value1
    
    VAR2=value2
    
    When running the command, the Docker CLI client checks the value the variable has in your local environment and passes it to the container. If no = is provided and that variable is not exported in your local environment, the variable won’t be set in the container.
    
    You can also load the environment variables from a file. This file should use the syntax <variable>=value (which sets the variable to the given value) or <variable> (which takes the value from the local environment), and # for comments.
    
    $ cat env.list
    
    # This is a comment
    
    VAR1=value1
    
    VAR2=value2
    
    USER
    
     
    
    $ docker run --env-file env.list ubuntu env | grep VAR
    
    VAR1=value1
    
    VAR2=value2
    
    USER=denis

    通过env查看抓取环境变量

    测试案例

    root@playbook-master:~/test# cat Dockerfile

    1 FROM ubuntu:16.04
    2 
    3 RUN apt update && apt install -y python-pip lrzsz
    4 
    5 ADD . /root
    6 
    7 WORKDIR /root
    8 
    9 CMD ["python","env_docker.py"]

    root@playbook-master:~/test# cat redis.conf

    {{var1}}

    {{var2}}

    {{password_redis}}

     

    root@playbook-master:~/test# cat var.txt

    var1=docker

    var2=redis

    password_redis=123456

    __config_location=/root/redis.conf

    root@playbook-master:~/test# docker run --name env1 -d --env-file ./var.txt env:test

    eb6eb002f1d03d783d49fd17b872abf96370323d50f00fafba1fed4e23e91141

    root@playbook-master:~/test# docker ps

    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

    eb6eb002f1d0        env:test            "python env_docker.py"   2 seconds ago       Up 1 second                             env1

    root@playbook-master:~/test# docker exec -it env1 /bin/bash

    root@eb6eb002f1d0:~# cat redis.conf

    docker

    redis

    123456

    python 处理脚本Env_docker.py

     1 # -*- coding:utf-8 -*-
     2 
     3 #!/usr/bin/python
     4 
     5 import os
     6 
     7 import re
     8 
     9 import time
    10 
    11 def alter(file):
    12 
    13     dict = os.environ
    14 
    15    
    16 
    17     file_data = ""
    18 
    19     with open(file,"r") as f:
    20 
    21         for line in f:
    22 
    23             for key in dict.keys():
    24 
    25                 if key in dict.keys():
    26 
    27                     keya = "{{"+key+"}}"
    28 
    29                     line = line.replace(keya,dict[key])
    30 
    31             file_data += line
    32 
    33     with open(file,"w") as f:
    34 
    35         f.write(file_data)
    36 
    37  
    38 
    39 dict=os.environ
    40 
    41 filenames = dict['__config_location']
    42 
    43 filenames = filenames.split(',')
    44 
    45 for filename in filenames:
    46 
    47     print(filename)
    48 
    49     alter(filename)
    50 
    51 time.sleep(60)  #测试添加入env_docker.py脚本

    shell处理脚本(ubuntu /bin/sh不支持数组功能,要用/bin/bash)

     1 #!/bin/bash
     2 
     3 deal(){
     4 
     5    key=($(env|awk -F= '{print $1}'))
     6 
     7    value=($(env|awk -F= '{print $2}'))
     8 
     9    num=$(env|wc -l)
    10 
    11    for ((i=0;i<$num;i++))
    12 
    13    do
    14 
    15          key1=${key[$i]}
    16 
    17          keya="{{"$key1"}}"
    18 
    19          value1=${value[$i]}
    20 
    21          grep $keya $file >/dev/null
    22 
    23          if [ $? -eq 0 ];
    24 
    25          then
    26 
    27              sed -i "s/$keya/$value1/g" $file
    28 
    29          fi
    30 
    31    done
    32 
    33 }
    34 
    35 for file in `echo ${__config_location}|awk -F, '{for(i=1;i<=NF;i++){print $i;}}'`
    36 
    37 do
    38 
    39    deal
    40 
    41 done
  • 相关阅读:
    【Amadeus原创】win10重装如何跳过微软账号直接设置本地帐户
    【Amadeus原创】升贴水的概念
    【Amadeus原创】含税价不含税价和税金的计算方法
    企业数字化转型白皮书2022
    大数据生态技术体系
    Dinky实时计算平台
    IPD、CMMI、敏捷
    Parallels 升级后提示虚拟机网络初始化失败
    暗区突围
    C#捕获未处理异常
  • 原文地址:https://www.cnblogs.com/Honeycomb/p/10114433.html
Copyright © 2020-2023  润新知