如果数据库位于另一个容器中,如何迁移到dockerfile? [英] How to migrate to dockerfile if the database is in another container?

查看:165
本文介绍了如果数据库位于另一个容器中,如何迁移到dockerfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Such situation: There is a container with a dB and Django container of the application which through-link connects to a dB and starts itself:

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

不好的是,每次我启动时,您都必须手动运行python vfnd / manage.py迁移

The bad thing is that you have to run python vfnd/manage.py migrate manually every time I launch the containers.

尝试以下代码:

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

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

However, when you try to build the image, you receive an error on this command

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

我如何实现我的想法

推荐答案

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

Put those commands in a script and run it as entrypoint in dockerfile.


  • 使用内容创建 init.sh 脚本

  • Create init.sh script with contents
#!/bin/bash

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

exec "$@"




  • 将您的 dockerfile 更改为

    • Change your dockerfile to
    • FROM deb_base
      COPY vfnd /
      COPY init.sh /init.sh
      ENTRYPOINT ["/init.sh"]
      

      希望这会有所帮助。

      这篇关于如果数据库位于另一个容器中,如何迁移到dockerfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆