I'm starting to use Docker and I'm making a webservice using Flask. In order to connect to databases I need to tell the service the credentials to connect. I don't want to put the variable directly on the Dockerfile or worst, on code. I thought of passing them through environment variables that are set on build time via a shell script called on the Dockerfile.
The thing here is that I've tried using CMD,RUN and ENTRYPOINT to achieve this without success.
Here's the last version of my Dockerfile:
FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["/app/con.sh"]
CMD ["-u","app.py"]
The script is named con.sh and has a structure like this:
export REDSHIFT_HOST="[Host of the DB]"
export REDSHIFT_USER="[DB user]"
export REDSHIFT_PASSWD="[DB password]"
export POSTGRES_DB="[DB name]"
export MODEL_IP="[endpoint of another service]"
export USER_SF="[user]"
export PASS_SF="[pass]"
export TOKEN="[Token]"
export INSTANCE_URL="[URL]"
What I get from this is:
Traceback (most recent call last):
File "app.py", line 4, in <module>
from clean_leads import *
File "/app/clean_leads.py", line 13, in <module>
HOST = os.environ['REDSHIFT_HOST']
File "/usr/local/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'REDSHIFT_HOST'
Which confirms that the environment variables didn't get set.