温馨提示:本文翻译自stackoverflow.com,查看原文请点击:django - How to migrate to dockerfile if the database is in another container?

django - 如果数据库位于另一个容器中,如何迁移到dockerfile?

发布于 2020-03-27 11:43:45

这样的情况:有一个带有dB的容器,而应用程序的Django容器则通过链接连接到dB并自行启动:

FROM deb_base
COPY vfnd vfnd
CMD ["python", "./vfnd/manage.py", "runserver", "0.0.0.0:8001"]

不好的是,每次我启动容器时,都必须手动运行python vfnd / manage.py migration。

尝试了以下代码:

FROM deb_base
COPY vfnd vfnd
RUN ["python", "./vfnd/manage.py", "migrate"]
CMD ["python", "./vfnd/manage.py", "runserver", "0.0.0.0:8001"]

但是,当您尝试构建映像时,此命令会收到错误消息

Step 3/4 : RUN ["python","./vfnd/manage.py","migrate"]
 ---> Running in 5791de6fc147
Traceback (most recent call last):
  File "/usr/share/Python-3.7.3/lib/python3.7/site-packages/django/db/backends/b
ase/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/share/Python-3.7.3/lib/python3.7/site-packages/django/db/backends/b
ase/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/share/Python-3.7.3/lib/python3.7/site-packages/django/db/backends/p
ostgresql/base.py", line 168, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/share/Python-3.7.3/lib/python3.7/site-packages/psycopg2/__init__.py
", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "pg_1" to address: Name
 or service not known

我如何实现我的想法

查看更多

查看更多

提问者
mr. Di
被浏览
76
mchawre 2019-07-04 01:52

将这些命令放在脚本中,然后将其作为入口点运行dockerfile

  • 创建init.sh包含内容的脚本
#!/bin/bash

python /vfnd/manage.py migrate
python /vfnd/manage.py runserver 0.0.0.0:8001

exec "$@"
  • 更改dockerfile
FROM deb_base
COPY vfnd /
COPY init.sh /init.sh
ENTRYPOINT ["/init.sh"]

希望这可以帮助。

发布
问题

分享
好友

手机
浏览

扫码手机浏览