Env var is defined on docker but returns None

2024/11/16 1:35:22

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine):

FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx
COPY ./handler.lua /etc/nginx/
COPY ./env_var_echo.py /etc/nginx/RUN apk update
RUN apk add python3
RUN nginx -s reload

I run the image and then get in the docker:

docker run -it -d -p 8080:80 --name my-ngx-lua nginx-lua
docker exec -it my-ngx-lua sh

Then I define a new environment variable from inside the docker:

/etc/nginx # export SECRET=thisIsMySecret
/etc/nginx # echo $SECRET
thisIsMySecret
/etc/nginx #

EDIT: After defining the new env var, I exit the container and then get into it again and it is not there anymore:

/etc/nginx # exit
iy@MacBook-Pro ~ % docker exec -it my-ngx-lua sh
/etc/nginx # echo $SECRET/etc/nginx #

I run the python script and I expect to receive "thisIsMySecret", which is the value I defined.

import ossecret_key = os.environ.get('SECRET')
print(secret_key + '\n')

But I get None instead.

Only if I call any env var that already came with the docker (PATH for example), python will return the value of it. But if it is an env var that I just defined, it will return None.

BTW, I tried the same with lua and received nil. hence I am pretty sure the issue is from Alpine.

I am not looking for a solution like defining the env var from docker build.

Thanks.

Answer

This

After defining the new env var, I exit the container

is the cause. Exported variable only exists while the main process does and it is only visible to the process it was declared in.

What you need is to use the -e option when you start the container:

docker run -e SECRET=mysecret ...

With this docker will add the environment variable to the main process (NGINX in this case) and to docker exec commands as well.

https://en.xdnf.cn/q/118985.html

Related Q&A

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …

Binomial distribution CDF using scipy.stats.binom.cdf [duplicate]

This question already has answers here:Alternatives for returning multiple values from a Python function [closed](14 answers)Closed 2 years ago.I wrote below code to use binomial distribution CDF (by u…

How to get Cartesian product of two iterables when one of them is infinite

Lets say I have two iterables, one finite and one infinite:import itertoolsteams = [A, B, C] steps = itertools.count(0, 100)I was wondering if I can avoid the nested for loop and use one of the infinit…

Function to create nested dictionary from lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Pygame not using specified font

So I am having a problem in pygame where I specify the font and size to use, but when my program is run, the font and size are the default.Here is where I define the textdef font(self):*****FATAL - THI…

port management in python/flask application

I am writing a REST API using the micro framework Flask with python programming language. In the debug mode the application detect any change in source code and restart itself using the same host and p…

using def with tkinter to make simple wikipedia app in python

I am beginner in python. I am trying to make a python wiki app that gives you a summary of anything that you search for. My code is below:import wikipediaquestion = input("Question: ")wikiped…

Only length-1 arrays can be converted to Python scalars with log

from numpy import * from pylab import * from scipy import * from scipy.signal import * from scipy.stats import * testimg = imread(path) hist = hist(testimg.flatten(), 256, range=[0.0,1.0])[0] hist…