Skip to content

Commit 8129098

Browse files
committed
.
1 parent ab90428 commit 8129098

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

docker.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ If any of the local files are changed, there is no need to rebuild the python pa
7070
7171
7272
73-
Environment Variable
73+
Input Variables
7474
*********************
7575

7676
To pass environment variables from ``docker run`` to the python code, we can use two methods.

flask.rst

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ Note that the latter argument must be at least 1.
317317
logger.addHandler(handler)
318318
319319
320-
321320
Docker
322321
------
323322

@@ -338,6 +337,43 @@ If we run ``docker ps``, under PORTS, we should be able to see
338337
that the Docker host IP 0.0.0.0 and port 5000, is accessible to the container at port 5000.
339338

340339

340+
Accessing Environment Variables
341+
--------------------------------
342+
343+
We can and should set environment variables; i.e., variables stored in the OS,
344+
especially for passwords and keys, rather than in python scripts. This helps with
345+
version control as you don't want to upload them to the github, though now it prevents keys from uploading.
346+
Presently I guess, it still prevents the need to copy/paste the keys into the script everytime you launch the app.
347+
348+
To do this, in Mac/Linux, we can store the env variable in a .bash_profile.
349+
350+
.. code:: bash
351+
352+
# open/create bash_profile
353+
nano ~/.bash_profile
354+
# add new environment variable
355+
export SECRET_KEY="key"
356+
# restart bash_profile
357+
source .bash_profile
358+
359+
360+
In the flask script, we can then obtain the variable by using the os package.
361+
362+
.. code:: python
363+
364+
import os
365+
366+
SECRET_KEY = os.environ.get("SECRET_KEY")
367+
368+
369+
For flask apps in docker containers, we can add an -e to include the environment variable into the
370+
container.
371+
372+
.. code:: bash
373+
374+
sudo docker run -e SECRET_KEY=$SECRET_KEY -p 5000:5000 comply
375+
376+
341377
OpenAPI
342378
---------
343379

0 commit comments

Comments
 (0)