简体   繁体   中英

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

My initial work included adding Dockerfile into rails app directory on host which looks like this:

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:

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

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. Any idea what I'm doing wrong?

Thanks in advance, Bakir

You need to have a CMD instruction in your Dockerfile. That is the command that is run when you start up your Docker container. 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 .

You can read more about the CMD instruction and everything else about Dockerfiles in the Docker documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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