在为我的项目运行图像时出现错误。在这里我正在尝试对我的应用程序进行泊坞窗

时间:2019-01-29 11:09:03

标签: python mysql docker pyramid

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

我在“ db_url = os.environ ['OPENSHIFT_MYSQL_DB_URL']”行中遇到错误,我的问题是如何在一个dockerfile中设置所有这些env变量,以便我可以构建映像并运行它。现在,我们将LAE服务器中的Eariler代码转移到基于容器化的服务器上。

1 个答案:

答案 0 :(得分:0)

您的问题与ENV配置设置有关,因此,我删除了所有其他内容以向您显示问题所在:

这是具有ENV设置的dockerfile

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"]

这是setup.py:

import os
import sys

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

现在,您可以访问os.environ变量。

参考:

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