如何使用多个动态数据库在 Docker 中创建同一应用程序的多个映像?

How can I create multiple images of the same application in Docker with Multiple dynamic Database?

提问人:Rakesh Bakoriya 提问时间:10/6/2023 最后编辑:Rakesh Bakoriya 更新时间:10/6/2023 访问量:61

问:

我正在使用 rails 5.1.0

我想创建同一应用程序的多个映像, 每个图像的域和数据库会有所不同,

image_1示例

  • 域 example.app.com
  • 数据库 - example_production

image_2示例

  • 域 example1.app.com
  • 数据库 - example1_production

image_3示例

  • 域 example3.app.com
  • 数据库 - example3_production

#application_controller.rb

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: "#{request.base_url}_production", reading: "#{request.base_url}_production" }
end

语法 = docker/dockerfile:1.2

FROM ruby:2.3

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

# Set an environment variable where the Rails app is installed to inside of Docker image:
ENV RAILS_ROOT /var/www
RUN mkdir -p $RAILS_ROOT/app
RUN mkdir -p $RAILS_ROOT/shared
RUN mkdir -p $RAILS_ROOT/shared/tmp/sockets
RUN mkdir -p $RAILS_ROOT/shared/tmp/pids

# Set working directory, where the commands will be ran:
WORKDIR $RAILS_ROOT/app

# Setting env up
ENV RAILS_ENV='production'
ENV RACK_ENV='production'

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5

# Adding project files
COPY . .
RUN ["chmod", "+x", "./docker-entrypoint.sh"]

EXPOSE 9292

ENTRYPOINT ["./docker-entrypoint.sh"]
Ruby-on-Rails Ruby PostgreSQL Docker ActiveRecord

评论

1赞 Hans Kilian 10/6/2023
听起来您可以使用单个映像来做到这一点,您可以使用环境变量为不同的域和数据库配置它。
0赞 Rakesh Bakoriya 10/6/2023
可以,在相同的镜像和相同的环境下,根据多个域管理多个数据库。
0赞 kwerle 10/7/2023
这真的是一个docker问题:stackoverflow.com/questions/30494050/......
0赞 Hans Kilian 10/7/2023
@kwerle我不同意。解决方案是 Docker 解决方案。问题不是。
0赞 tadman 10/7/2023
也许是Docker Compose?

答: 暂无答案