我有一个django项目的repo,想从它创建一个Docker镜像。我也不想在git中存储任何编译后的文件,所以我尝试在创建Docker镜像的过程中自动创建所有工件。如果我在我的Dockerfile中插入:RUN python manage.py compilemessages -l en
,我会得到(注意所有依赖项都安装在主机上):
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/src/playpilot/apps.py", line 21, in ready
for ct in ContentType.objects.all():
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
...
File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
字符串
我通过在构建脚本中运行docker-compose来解决这个问题(使用整个运行环境)
docker-compose up -d
docker-compose exec web ./manage.py compilemessages -l en
docker commit proj_web_1 image_name
docker-compose down
型
但这增加了构建时间,看起来是一个相当丑陋的解决方案。manage.py
不需要连接到数据库来执行这个特定的任务。有没有一种方法可以运行manage.py
,这样它就不会调用数据库后端?
Django版本:1.8
3条答案
按热度按时间qjp7pelc1#
使用
django-admin
代替manage.py
:字符串
4bbkushb2#
您的问题是,当您运行
collectstatic
时,postgres
容器已启动,但postgres
本身尚未启动。阅读https://docs.docker.com/compose/startup-order/了解更多信息你基本上需要做的是设置
ENTRYPOINT
,同时检查postgres
是否准备好接受连接,这里是我在项目中如何做的例子。字符串
np8igboo3#
尝试添加到dockerfile
运行apt-get update && apt-get install -y gettext libgettextpo-dev