创建容器时执行python代码

时间:2019-11-12 04:38:53

标签: python-3.x docker dockerfile containers oracle-cloud-infrastructure

再次

在创建容器时执行python文件时遇到问题。我不知道是否可能,也许我错了。是为了我在这里寻求帮助的原因

这是我的Dockerfile

FROM juliovg/oracle-oci-19
ENV HOME_DIR=/ \
    CODE_DIR=/root/sample/code \
    BUCKET_NAME=Code \
    LC_ALL=en_US.utf-8 \
    LANG=en_US.utf-8 \
    KEY_NAME="key.tar.gz" \
    KEY_URL="https://objectstorage/key.tar.gz" \
    PYTHON_ENV="https://objectstorage/test_1.py" \
    SCRIPT_SH="https://objectstorage/script.sh"
WORKDIR $HOME_DIR
RUN rm -rf $HOME_DIR/.oci
RUN wget $KEY_URL
RUN tar -xvf $KEY_NAME && rm -rf $KEY_NAME
RUN mkdir -p $CODE_DIR

WORKDIR $CODE_DIR
RUN wget $PYTHON_ENV
RUN wget $SCRIPT_SH

#RUN python3 /root/sample/code/test_1.py
#CMD ["python3 test_1.py"]
CMD [ "python3", "./test_1.py" ]
#ENTRYPOINT [ "python3", "test_1.py" ]
#RUN chmod +x script.sh
#CMD ["sh", "./script.sh"]

这是我的python文件

import oci
import sys
import os
import time
from base64 import b64encode, b64decode

config = oci.config.from_file()

compartment_code = config["tenancy"]
user_code = config["user"]
fingerprint_code = config["fingerprint"]



def write_env(environment_name):

    with open(os.path.expanduser("~/.bashrc"), "a") as outfile:

        if environment_name is "COMP_ID":
            outfile.write("export COMP_ID={} \n".format(compartment_code)),
        elif environment_name is "USER_ID":
            outfile.write("export USER_ID={} \n".format(user_code)),
        elif environment_name is "FINGER_CODE":
            outfile.write("export FINGER_CODE={} \n".format(fingerprint_code))
        else:
            print("Variable unknow")
    outfile.closed


def set_environment():
    MANDATORY_ENV_VARS = ["COMP_ID", "USER_ID", "FINGER_CODE"]

    for var in MANDATORY_ENV_VARS:
        if var not in os.environ:
            print("Set environment variable {}.".format(var))
            write_env(var)
        else:
            print("estan")

    refresh_env = "exec -l $SHELL"
    os.system(refresh_env)


set_environment()

我的script.sh是:

#!/bin/bash -x

python3 /root/sample/code/test_1.py

一切正常,我没看到错误

enter image description here

但是当我进入图像时,未设置环境,.bashrc文件不包含新的3导出

有人可以帮助我吗?

致以问候

0 个答案:

没有答案