Я прочитал много ответов здесь, я все еще не понимаю, что происходит с моими экземплярами
docker -compose.yml
version: '3'
services:
api:
build: ./puller-api/
ports:
- "8000:8000"
#restart: "always"
env_file:
- ./env/env-dev.env
depends_on:
- mongo
mongo:
image: mongo:4.2
restart: always
env_file:
- ./env/env-dev.env
dockerfile:
FROM python:3.8
WORKDIR /api
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
#
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
&& apt-get -y install git iproute2 procps lsb-release \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
RUN cd src \
RUN pip install -e . \
ENTRYPOINT ["uvicorn", "puller.main:app"]
. / Env / env-dev.env
MONGO_DB=puller
MONGO_COL_JOBS=jobs
MONGO_COL_USERS=users
MONGO_CON=mongodb://....
тогда, когда я запускаю эту команду:
docker run -it --entrypoint /bin/bash puller_api -c env
HOSTNAME=f9e7cd5d2059
PYTHON_VERSION=3.8.2
PWD=/api
HOME=/root
LANG=C.UTF-8
GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568
TERM=xterm
SHLVL=0
PYTHON_PIP_VERSION=20.0.2
PYTHON_GET_PIP_SHA256=421ac1d44c0cf9730a088e337867d974b91bdce4ea2636099275071878cc189e
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/d59197a3c169cef378a22428a3fa99d33e080a5d/get-pip.py
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
, как вы видите, не MON GO env vars устанавливаются?
docker -составить -d
затем я присоединяюсь к нему с помощью: docker run -it --entrypoint /bin/bash puller_api
и запускаю вручную:
root@2f5356b25861:/api/src# uvicorn puller.main:app
Вы можете видеть, что переменные MONGO_ не установлены:
User's Environment variable:
{'GPG_KEY': 'E3FF2839C048B25C084DEBE9B26995E310250568',
'HOME': '/root',
'HOSTNAME': '2f5356b25861',
'LANG': 'C.UTF-8',
'OLDPWD': '/api',
'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
'PWD': '/api/src',
'PYTHON_GET_PIP_SHA256': '421ac1d44c0cf9730a088e337867d974b91bdce4ea2636099275071878cc189e',
'PYTHON_GET_PIP_URL': 'https://github.com/pypa/get-pip/raw/d59197a3c169cef378a22428a3fa99d33e080a5d/get-pip.py',
'PYTHON_PIP_VERSION': '20.0.2',
'PYTHON_VERSION': '3.8.2',
'SHLVL': '1',
'TERM': 'xterm',
'_': '/usr/local/bin/uvicorn'}
Traceback (most recent call last):
File "/usr/local/bin/uvicorn", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 331, in main
run(**kwargs)
File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 354, in run
server.run()
File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 389, in serve
config.load()
File "/usr/local/lib/python3.8/site-packages/uvicorn/config.py", line 288, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./puller/main.py", line 13, in <module>
from puller.db import dbjobs, dbusers
File "./puller/db.py", line 17, in <module>
db = client[os.getenv('MONGO_DB')]
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1566, in __getitem__
return database.Database(self, name)
File "/usr/local/lib/python3.8/site-packages/pymongo/database.py", line 107, in __init__
raise TypeError("name must be an instance "
TypeError: name must be an instance of str
db.py
import pymongo
import os
import pprint
# Get the list of user's
# environment variables
env_var = os.environ
# Print the list of user's
# environment variables
print("User's Environment variable:")
pprint.pprint(dict(env_var), width = 1)
client = pymongo.MongoClient(os.getenv('MONGO_CON'))
db = client[os.getenv('MONGO_DB')]
dbjobs = db[os.getenv('MONGO_COL_JOBS')]
dbusers = db[os.getenv('MONGO_COL_USERS')]