我现在是想这样 项目下的 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
但是这样好像不起作用
要在你项目的 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
现在又碰到一个问题,我 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
我还想问下各位大佬,如果想用 docker 来部署 production,正确的流程应该是怎么样的,本地 Build 完,传上去,到服务器 Pull 下来跑,还是到服务器上 build,感觉比起一句mina deploy
来说,docker 部署步骤还挺多的
应该把 apt-get
移到前面以利用 docker 构建缓存。
@ad583255925 容器部署应该依赖容器调度工具,例如 kubernetes,现在云服务商已经开始提供 kubernetes service。
如果运行时并不需要 js runtime,那么构建完后 node.js 就变得多余了,增加了镜像体积。
可以用 multi-stage 分步构建镜像: https://docs.docker.com/develop/develop-images/multistage-build/
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 method
warn' 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'