Running migrations with Rails in a Docker container with multiple container instances

前端 未结 2 1672
时光取名叫无心
时光取名叫无心 2021-01-31 09:01

I\'ve seen lots of examples of making Docker containers for Rails applications. Typically they run a rails server and have a CMD that runs migrations/setup then brings up the Ra

相关标签:
2条回答
  • 2021-01-31 09:43
    docker run <container name> rake db:migrate
    

    Starts you standard application container but don't run the CMD (rails server), but rake db:migrate

    UPDATE: Suggested by Roman, the command would now be:

    docker exec <container> rake db:migrate
    
    0 讨论(0)
  • 2021-01-31 09:53

    Especially with Rails I don't have any experience, but let's look from a docker and software engineering point of view.

    The Docker team advocates, sometimes quite aggressively, that containers are about shipping applications. In this really great statement, Jerome Petazzoni says that it is all about separation of concerns. I feel that this is exactly the point you already figured out.

    Running a rails container which starts a migration or setup might be good for initial deployment and probably often required during development. However, when going into production, you really should consider separating the concerns.

    Thus I would say have one image, which you use to run N rails container and add a tools/migration/setup whatever container, which you use to do administrative tasks. Have a look what the developers from the official rails image say about this:

    It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of.

    When you look at that image there is no setup or migration command. It is totally up to the user how to use it. So when you need to run several containers just go ahead.

    From my experience with mysql this works fine. You can run a data-only container to host the data, run a container with the mysql server and finally run a container for administrative tasks like backup and restore. For all three containers you can use the same image. Now you are free to access your database from let's say several Wordpress containers. This means clear separation of concerns. When you use docker-compose it is not that difficult to manage all those containers. Certainly there are already many third party containers and tools to also support you with setting up a complex application consisting of several containers.

    Finally, you should decide whether docker and the micro-service architecture is right for your problem. As outlined in this article there are some reasons against. One of the core problems being that it adds a whole new layer of complexity. However, that is the case with many solutions and I guess you are aware of this and willing to except it.

    0 讨论(0)
提交回复
热议问题