简体   繁体   English

如何在主机上运行Nginx的情况下在Docker容器中运行Rails应用程序?

[英]How to run rails app in docker container with nginx running on host machine?

I'm working on the scenario where I need to have: 1. nginx running on the host machine 2. rails app running inside docker container 我正在研究需要满足以下条件的场景:1.在主机上运行的nginx 2.在docker容器中运行的rails应用

My initial work included adding Dockerfile into rails app directory on host which looks like this: 我的最初工作包括将Dockerfile添加到主机上的rails应用程序目录中,如下所示:

ubuntu@ubuntu-xenial:~/rails_docker$ cat Dockerfile
FROM ruby:2.1-onbuild

ENV HOME /home/rails/webapp

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

WORKDIR $HOME

# Install gems
ADD Gemfile* $HOME/
RUN bundle install

# Add the app code
ADD . $HOME

EXPOSE 8080
EXPOSE 5432

Since I already have nginx on host, it is configured to look at directory where rails application exists on host machine: 由于我已经在主机上安装了nginx,因此将其配置为查看主机上存在Rails应用程序的目录:

ubuntu@ubuntu-xenial:~$ cat /etc/nginx/sites-available/www.testblog.io.conf
# MANAGED BY PUPPET
server {
  listen *:8080;
  server_name           www.testblog.io;

  index  index.html index.htm index.php;

  access_log            /var/log/nginx/www.testblog.io.access.log combined;
  error_log             /var/log/nginx/www.testblog.io.error.log;

  location / {
    root      /home/ubuntu/rails_docker/public;
    index     index.html index.htm index.php;
  }
  passenger_enabled on;
  passenger_ruby /usr/local/rvm/wrappers/default/ruby;
} 

I'm able to build docker image and my next idea is following: 1. Declare /home/ubuntu/rails_docker as a host volume when running "rails container" 2. Expose ports 8080:8080 and 5432:5432 我能够构建docker映像,我的下一个想法如下:1.运行“ rails container”时,将/ home / ubuntu / rails_docker声明为主机卷。2.暴露端口8080:8080和5432:5432

Command I use is following: 我使用的命令如下:

ubuntu@ubuntu-xenial:~/rails_docker$ docker run -d -p 8080:8080 --name examplerails -v /home/ubuntu/rails_docker:/home/rails/webapp railsapptest
82c8aa45b8c1a405e198a565feabf105d1afcbb1c37f8b7b11bf764395ed8c4e

When I check logs: 当我检查日志时:

ubuntu@ubuntu-xenial:~/rails_docker$ docker logs -f examplerails
Switch to inspect mode.

For some reason, it goes into irb mode. 由于某种原因,它进入了irb模式。 Any idea what I'm doing wrong? 知道我在做什么错吗?

Thanks in advance, Bakir 在此先感谢,Bakir

You need to have a CMD instruction in your Dockerfile. 您需要在Dockerfile中有一条CMD指令。 That is the command that is run when you start up your Docker container. 这是启动Docker容器时运行的命令。 If you don't specify a command, it will run whatever your FROM image uses, which you can see is 'irb' if you look at the Ruby docker image . 如果您不指定命令,它将运行您的FROM映像使用的任何命令,如果您查看Ruby docker映像 ,则可以看到它是“ irb”。

You can read more about the CMD instruction and everything else about Dockerfiles in the Docker documentation . 您可以在Docker文档中阅读有关CMD指令的更多信息,以及有关Dockerfile的所有其他信息。

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

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