简体   繁体   English

如何使用Docker构建Sidekiq和Rails镜像?

[英]How to build sidekiq and rails image with docker?

I am trying another way to build a rails application into a docker image. 我正在尝试另一种将Rails应用程序构建到Docker映像中的方法。

The structure of my services: 我的服务结构:

  • redis -- from official docker hub registry Redis-来自官方Docker Hub注册表
  • fluentd -- from official docker hub registry 流利-来自官方Docker Hub注册表
  • mysql -- from official docker hub registry mysql-来自官方Docker Hub注册表
  • sidekiq -- build myself(maybe there isn't a official image for this) sidekiq-建立自己(也许没有官方图片)
  • web -- build myself 网络-建立自己

I created two Dockerfiles like: 我创建了两个Dockerfile,例如:

  • Dockerfile.sidekiq Dockerfile.sidekiq
  • Dockerfile.web Dockerfile.web

Dockerfile.sidekiq Dockerfile.sidekiq

FROM ruby:2.2.2

ENV APP_HOME /myapp
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ADD Gemfile $APP_HOME/Gemfile
ADD Gemfile.lock $APP_HOME/Gemfile.lock
ADD config/sidekiq.yml $APP_HOME/config/sidekiq.yml
ADD init_sidekiq.sh $APP_HOME/

RUN export LANG=C.UTF-8 && bundle install

ADD . $APP_HOME

CMD ["sh", "init_sidekiq.sh"]

init_sidekiq.sh init_sidekiq.sh

#!/bin/sh
bundle exec sidekiq -C config/sidekiq.yml

Dockerfile.web Dockerfile.web

FROM rails:4.2.1

ENV APP_HOME /myapp
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ADD Gemfile $APP_HOME/Gemfile
ADD Gemfile.lock $APP_HOME/Gemfile.lock
ADD init_web.sh $APP_HOME/

RUN export LANG=C.UTF-8 && bundle install

ADD . $APP_HOME

CMD ["sh", "init_web.sh"]

init_web.sh init_web.sh

#!/bin/sh
bundle exec rake db:create db:migrate
bundle exec rails server -b 0.0.0.0

Use them I built two images: 使用它们,我构建了两个图像:

  • myapp_web myapp_web
  • myapp_sidekiq myapp_sidekiq

Then run these containers: 然后运行以下容器:

$ docker run --name redis -d redis
$ docker run --name fluentd -d -p 24224:24224 fluent/fluentd
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=my_password -d mysql

Make env.list 制作环境清单

RAILS_ENV=production
DATABASE_URL=mysql2://root:my_password@172.17.0.4/myapp?checkout_timeout=20000

Go on run these containers: 继续运行以下容器:

$ docker run --name web -d -p 3000:3000 --link mysql:mysql --env-file ./env.list myapp_web
$ docker run --name sidekiq -d --link mysql:mysql --env-file ./env.list myapp_sidekiq

The result: 结果:

  • redis -- success Redis-成功
  • fluentd -- success 流利-成功
  • mysql -- success mysql-成功
  • web -- success 网络-成功
  • sidekiq -- failure sidekiq-失败

The sidekiq log: sidekiq日志:

$ docker logs sidekiq
Unknown database 'myapp'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `rescue in mysql2_connection'

I used the same method both web and sidekiq to connect mysql. 我使用相同的方法通过web和sidekiq连接mysql。 I believe that in the mysql container there exists a myapp database. 我相信在mysql容器中存在一个myapp数据库。 But why it can't find it? 但是为什么找不到它呢?

Is it a wrong way to make them been two containers? 使它们成为两个容器是错误的方法吗? How to run sidekiq correctly? 如何正确运行sidekiq?

I think the problem there is how are you connecting to redis? 我认为问题在于您如何连接到Redis? The message appears to come from sidekiq and somehow it can't connect to your redis server. 该消息似乎来自sidekiq,并且某种程度上无法连接到您的Redis服务器。 (and I think trying to connect to some bogus db server/database) (而且我认为尝试连接到一些虚假的数据库服务器/数据库)

So I think you need to link your sidekiq container to both your db container and also your redis container. 因此,我认为您需要将sidekiq容器链接到db容器和redis容器。

docker run --name sidekiq -d --link mysql:mysql --link redis:redis --env-file ./env.list myapp_sidekiq

Also, would be nice if you can share your env.list 另外,如果您可以共享您的env.list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM