-1

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

FROM firesh/nginx-lua

COPY ./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 os

secret_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.

10
  • 1
    How does the interactive shell session you show relate to the script? Can you provide the Dockerfile and other instructions to run the container? How would you normally expect to set the environment variable? Commented Mar 7, 2021 at 16:25
  • @DavidMaze I ran a docker, defined a new env var and calling it from python. How will the Dockerfile contribute? anyway this is the image: hub.docker.com/r/firesh/nginx-lua Commented Mar 7, 2021 at 16:31
  • If you're just using a prebuilt image, I'm a little confused on how the script gets into the container. I'd also normally expect that image to run Nginx and not an interactive shell. Can you edit the question to provide the exact commands you're running, especially the docker run command? Commented Mar 7, 2021 at 16:37
  • I ran docker, defined a new env var and called it from python. And it worked: pastebin.com/aByMyGZB Please add more details how you do it. Commented Mar 7, 2021 at 16:37
  • 1
    If you docker run the image you've provided, it runs Nginx, not an interactive shell. Nothing you've shown indicates how you're running the Python script at all. Based only on what you've shown, I'd recommend starting your image FROM python:3.9 instead of an unrelated Nginx image, and setting ENV in the Dockerfile; it would help if you clarified how these three parts came together and why ENV isn't a good answer. Commented Mar 7, 2021 at 18:57

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.