Search code examples
pythonmysqldockerpyramid

I am getting error while running image for my project. Here i am trying to dockerize my application


db_url = os.environ['OPENSHIFT_MYSQL_DB_URL']
  File "/usr/local/lib/python2.7/UserDict.py", line 40, in __getitem__
  raise KeyError(key)
  KeyError: 'OPENSHIFT_MYSQL_DB_URL'

My Dockerfile look like:
 FROM python:2.7.13
 RUN apt-get update && apt-get install -y libsasl2-dev python-dev 
 libldap2-dev
 RUN apt-get install -y gcc libc-dev unixodbc-dev libffi-dev
 RUN apt-get install -y apt-utils
 RUN apt-get install -y libmysqlclient-dev libssl-dev netcat
 RUN pip install --upgrade pip
 COPY . /LAE-python
 WORKDIR /LAE-python
 RUN pwd
 #RUN pip install -r requirements.txt
 RUN python setup.py develop
 ENTRYPOINT ["pserve"]
 CMD ["development.ini","--reload"]


My Code :
import os
import sys
import transaction
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import Column,Integer, String,ForeignKey

from mtools.settings import mysql_user,mysql_passowrd,database_name

db_session = 
scoped_session(sessionmaker(autocommit=False,autoflush=False))
db_url = os.environ['OPENSHIFT_MYSQL_DB_URL']
engine_string = db_url+database_name

I am getting error for "db_url = os.environ['OPENSHIFT_MYSQL_DB_URL']" line, My problem is how to set all these env variable in one dockerfile so that i can build image and run it. Eariler code in LAE server now we are moving to some containerize based server .


Solution

  • Your problem is related to a ENV configuration setting, so, I've removed everything else to show you where the problem is:

    This is the dockerfile with the ENV setting

    FROM python:2.7.13
    RUN apt-get update && apt-get install -y libsasl2-dev python-dev libldap2-dev
    RUN apt-get install -y gcc libc-dev unixodbc-dev libffi-dev
    RUN apt-get install -y apt-utils
    RUN apt-get install -y libmysqlclient-dev libssl-dev netcat
    RUN pip install --upgrade pip
    
    ENV OPENSHIFT_MYSQL_DB_URL "your URL"
    
    COPY . /LAE-python
    WORKDIR /LAE-python
    
    CMD ["python","-u", "setup.py"]
    

    This is setup.py:

    import os
    import sys
    
    db_url = os.environ['OPENSHIFT_MYSQL_DB_URL']
    print db_url
    

    Now you can access your os.environ variable.

    Reference:

    https://vsupalov.com/docker-arg-env-variable-guide/