Docker

Run container with environment variable

Naranjito 2021. 11. 2. 19:34
  • Set environment variables.

1. One way, build the image(name:tag) with environment file(2021.11.02 - [Anaconda] - Environment Configuration).

$ docker build -t myenv:1.0 ./
[+] Building 18.6s (11/11) FINISHED            

...

 => => naming to docker.io/library/myenv:1.0                                                         0.0s

And then, apply .env file to the image, run the Python code(2021.11.02 - [Anaconda] - Environment Configuration).

$ docker run --env-file .env myenv:1.0 env_practice.py

...

  TW_ATT_IP_SEARCH_DATA ACCD_CHARGER_ID  ... DRULE_DESC TW_DMG_IP_SEARCH_DATA
0                  None      baenak0717  ...        NaN                   NaN
1                  None         infotel  ...        NaN                   NaN
2                  None         taemink  ...        NaN                   NaN
3                  None         taemink  ...        NaN                   NaN
4                  None         kar0825  ...        NaN                   NaN

[5 rows x 132 columns]

 

2. Another way, define the variable and its value when running the container. 

$ docker run --env ELASTIC_HOST=@@@.@@@.@@.@@@ --env ELASTIC_PORT==@@@@ --env ELASTIC_USER=@@@ --env ELASTIC_KEY='@@@' myenv:1.0 env_practice.py

>>>
@@@.@@@.@@.@@@
@@@@
@@@
@@@

 

Then, check the environment I set inside the container. 

# <First> I created new container with my environment file. I used ubuntu image.

$ docker run -i -t --env-file .env ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest

# <Second> Inside the container, I am going to check my environment.
root@385c9c8d3a90:/# env | grep ELASTIC

>>>
ELASTIC_HOST=@@@.@@@.@@.@@@ #I can see the environment I set
ELASTIC_PORT=@@@@
ELASTIC_USER=@@@
ELASTIC_KEY=@@@

- | : The symbol ‘|’ denotes a pipe. It helps to mash-up two or more commands at the same time and run them consecutively.(https://www.guru99.com/linux-pipe-grep.html)

 

- grep : It will scan the document for the desired information and present the result in a format you want.(https://www.guru99.com/linux-pipe-grep.html)

 

  • My local global environment.
$ env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash

...

CONDA_DEFAULT_ENV=practice_pandas
SECURITYSESSIONID=186a8
_=/usr/bin/env

 

3. Other way, define and get environment variables in Python you can just use the os module.

import pandas as pd
import ssl
from elasticsearch.connection import create_ssl_context
from elasticsearch import Elasticsearch
import urllib3
from decouple import config

ssl_context = create_ssl_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

import os

ELASTIC_HOST=os.getenv('ELASTIC_HOST')
if ELASTIC_HOST == None:
    ELASTIC_HOST='http://localhost'
ELASTIC_PORT=os.getenv('ELASTIC_PORT')
if ELASTIC_PORT == None:
    ELASTIC_PORT=9200
else:
    ELASTIC_PORT=int(ELASTIC_PORT)
ELASTIC_USER=os.getenv('ELASTIC_USER')
if ELASTIC_USER == None:
    ELASTIC_USER='user'
ELASTIC_KEY=os.environ.get('ELASTIC_KEY')
if ELASTIC_KEY == None:
    ELASTIC_KEY='123'
    
es = Elasticsearch(hosts=[{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}], scheme="http",verify_certs=False, timeout=300, ssl_context=ssl_context, http_auth=(ELASTIC_USER, ELASTIC_KEY))
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

######## 2020, 1 year ########
######## There are no MTM data in 2018, 2019 ########

body = {
         "size" : 100,
         "query": {
                 "range":{
                    "TW_COLLECT_DT":{
                        "gte":"2020-01-01T00:00:00.625+09:00",
                        "lte":"2020-12-31T00:00:00.625+09:00" ################
                    }
                }
                 },
    "sort":[{
        "_id":"asc"
    }]
}
        
res = es.search(index = 'd-2k-r-sago-temp', body=body)
data = res['hits']['hits']
# nxt=res["hit"]["hit"][-1]["sort"][0]
total = res['hits']['total']

# print(total)

accident = []
for da in data:
    att_type = da['_source']
    # att_type["POL_NM"]=att_type["SCEN_INFOS"][0]["POL_NM"]
    accident.append(att_type)

# df = pd.DataFrame(accident,dtype=str)
df_10000 = pd.DataFrame(accident)

print(df_10000.head())

 

Then, build docker file(2021.10.22 - [Docker] - How to run my python project on the docker).

$ docker build -t myenv:1.0 .
[+] Building 3.1s (12/12) FINISHED                                              

...

 => => naming to docker.io/library/myenv:1.0                               0.0s

 

And, run with environment file(2021.11.02 - [Anaconda] - Environment Configuration).

$ docker run --env-file .env myenv:1.0 env_practice.py

>>>
@@@.@@@.@@.@@@
@@@@
@@@
@@@
  TW_ATT_IP_SEARCH_DATA ACCD_CHARGER_ID  ... DRULE_DESC TW_DMG_IP_SEARCH_DATA
...

 

Run without environment file.

$ docker run myenv:1.0 env_practice.py

>>>

http://localhost
9200
user
123

...

elasticsearch.exceptions.ConnectionError: 

...