新手问题 Docker 跑一个 rails 项目,缺少 nodejs runtime,一般怎么弄

ad583255925 · 2018年03月23日 · 最后由 ad583255925 回复于 2018年03月25日 · 1234 次阅读

我现在是想这样 项目下的Dockerfile

FROM ruby:2.3

WORKDIR /app

COPY Gemfile Gemfile.lock ./

COPY . .

RUN bundle install

项目下的docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: rails s
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    links:
      - node
  node:
    image: node

但是这样好像不起作用

共收到 13 条回复

要在你项目的 Dockerfile 里安装 node 环境。而不是在 docker-compose 里定义来启动一个 node 环境的 container。

FROM ruby:2.3

WORKDIR /app

COPY Gemfile Gemfile.lock ./

COPY . .

RUN apt-get install nodejs

RUN bundle install
w7938940 回复

这样啊,那数据库呢,装在一起,还是新建一个容器link

现在又碰到一个问题,我link了一个redis,但是我的rails默认找的是127.0.0.1:6379,而redis在另一个容器里面,rails容器得用redis:6379来访问,这个怎么处理比较妥一点

version: '3'
services:
  bkfix:
    build: .
    command: rails s -p 4000
    ports:
      - "3000:4000"
    volumes:
      - .:/app
    links:
      - mysql
      - redis
  mysql:
    image: mysql
    restart: always
    command: --character-set-server=utf8 --collation-server=utf8_general_ci
    ports:
      - '3306:3306'
    environment:
      - MYSQL_ROOT_PASSWORD=ad66544970123
      - MYSQL_DATABASE=bkfix_dev
    volumes:
      - ./data/mysql:/var/lib/mysql
  redis:
    image: redis:alpine
    command: redis-server
    volumes:
      - ./data/redis:/data

node 不适合 docker, node_modules 这么多, 在 Mac / Windows 会非常慢...

应该把编译好的js文件预先放在docker里

我还想问下各位大佬,如果想用docker来部署production,正确的流程应该是怎么样的,本地Build完,传上去,到服务器Pull下来跑,还是到服务器上build,感觉比起一句mina deploy来说,docker部署步骤还挺多的

w7938940 回复

应该把 apt-get 移到前面以利用 docker 构建缓存。

@ad583255925 容器部署应该依赖容器调度工具,例如 kubernetes,现在云服务商已经开始提供 kubernetes service。

如果运行时并不需要 js runtime,那么构建完后 node.js 就变得多余了,增加了镜像体积。

可以用 multi-stage 分步构建镜像: https://docs.docker.com/develop/develop-images/multistage-build/

dokku你值得拥有。。。

web:
  image: nginx
  links:
    - bkfix
  volumes:
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
  command: nginx -c /etc/nginx/nginx.conf
  ports:
    - "80:80"

我想让我的nginx.conf生效,但是我nginx只要加上command: nginx -c /etc/nginx/nginx.conf跑就会直接退出,不知道怎么回事,

现在nginx也生效了,我想试着绑在3002端口下看下效果,但是并没有反应,80端口是好的

nginx

upstream bkfix {
    server bkfix:7000 fail_timeout=0;
  }

  server {
    listen 3002 default_server;

    root /var/www/bkfix/public;
    location ^~ /assets/ {
      gzip_static on;
      expires max;
      add_header Cache-Control public;
    }

docker-compose

version: '3'
services:
  bkfix:
    build: .
    command: bundle exec puma -C config/puma.rb -e production
    environment:
      - RAILS_ENV=production
    ports:
      - "7000:7000"
    volumes:
      - .:/app
    links:
      - mysql
      - redis
  mysql:
    image: mysql
    restart: always
    command: --character-set-server=utf8 --collation-server=utf8_general_ci
    environment:
      - MYSQL_ROOT_PASSWORD=ad66544970123
      - MYSQL_DATABASE=bkfix_dev
    volumes:
      - ./data/mysql:/var/lib/mysql
  redis:
    image: redis:alpine
    command: redis-server
    volumes:
      - ./data/redis:/data
  web:
    image: nginx
    links:
      - bkfix
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    ports:
      - "80:80"

puma.rb

app_root = "/app"
pidfile "#{app_root}/tmp/pids/puma.pid"
state_path "#{app_root}/tmp/pids/puma.state"
bind "unix://#{app_root}/tmp/sockets/puma.sock"
port 7000
activate_control_app "unix://#{app_root}/tmp/sockets/pumactl.sock"
# daemonize true
workers 2
threads 8, 16
preload_app!


stdout_redirect "#{app_root}/log/puma_access.log", "#{app_root}/log/puma_error.log", true

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

before_fork do
  ActiveRecord::Base.connection_pool.disconnect!
end

是端口没映射

ports:
  - "80:80"
  - "3002:3002"

现在就差最后一步了,结果出来这么个报错NoMethodError (private methodwarn' called for nil:NilClass):`

I, [2018-03-25T08:26:14.073013 #22]  INFO -- : [5083dc64-fb14-4d47-b359-6c7ad6d836f7] Completed 500 Internal Server Error in 43ms
F, [2018-03-25T08:26:14.078556 #22] FATAL -- : [5083dc64-fb14-4d47-b359-6c7ad6d836f7]   
F, [2018-03-25T08:26:14.080239 #22] FATAL -- : [5083dc64-fb14-4d47-b359-6c7ad6d836f7] NoMethodError (private method `warn' called for nil:NilClass):
F, [2018-03-25T08:26:14.080579 #22] FATAL -- : [5083dc64-fb14-4d47-b359-6c7ad6d836f7]   
F, [2018-03-25T08:26:14.080940 #22] FATAL -- : [5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notifier.rb:109:in `rescue in fire_notification'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notifier.rb:103:in `fire_notification'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notifier.rb:48:in `block in notify_exception'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notifier.rb:47:in `each'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notifier.rb:47:in `notify_exception'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notification/rack.rb:42:in `rescue in call'
[5083dc64-fb14-4d47-b359-6c7ad6d836f7] exception_notification (4.2.1) lib/exception_notification/rack.rb:31:in `call'

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册