Docker Compose + Rails:迁移的最佳实践? [英] Docker Compose + Rails: best practice to migrate?

查看:124
本文介绍了Docker Compose + Rails:迁移的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在在Docker中运行Rails开发环境中关注了这篇文章。好文章,很棒。设置完所有内容后,我决定继续设置生产环境。

I just followed this article on Running a Rails Development Environment in Docker. Good article, works great. After setting everything up, I decided to go on and set up a production environment.

目标:

我要 rake db:create&&每次运行docker映像时,rake db:migrate

问题:

如果我移动数据库创建和迁移步骤...

If I move the database creation and migrations steps...

docker-compose run app rake db:create
docker-compose run app rake db:migrate

...到Dockerfile ...

...into the Dockerfile...

RUN rake db:create && rake db:migrate

...这将引发错误...

...that will throw an error...

could not translate host name "postgres" to address: Name or service not known

...因为我的 database.yml 中的主机 ...

...because the host in my database.yml...

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  username: postgres
  host: postgres
  port: 5432

development:
  <<: *default
  database: rails_five_development

...设置为 docker-compose.yml ...

...is set to the postgres service name specified in my docker-compose.yml...

version: "2"
services:
  postgres:
    image: postgres:9.5
    ports:
      - "5432"
  app:
    build: .
    command: rails server -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    links:
      - postgres

...因为这是推荐做法

...since that is the recommended practice as pointed by the article.

问题:

如何自动 rake db:create&&每次运行docker镜像时都耙ra db:migrate 吗?

我正在尝试实现与该问题相同的东西

推荐答案

来自 https://docs.docker.com/engine/reference/ builder /#cmd


如果您希望容器每次都运行相同的可执行文件,则应考虑使用ENTRYPOINT与CMD结合使用。请参阅ENTRYPOINT

If you would like your container to run the same executable every time, then you should consider using ENTRYPOINT in combination with CMD. See ENTRYPOINT

https://docs.docker.com/engine/reference/builder/#entrypoint

tl; dr

您可以在应用程序下定义入口点并在其中定义bash文件:

You could define an entrypoint under app and define a bash file there:

app:
  entrypoint: [bin/entry]
  ..

bin / entry文件示例:

bin/entry file example:

#!/bin/bash
set -e

rake db:create
rake db:migrate

exec "$@"

这篇关于Docker Compose + Rails:迁移的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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