简体   繁体   English

使用docker-compose对Rails应用程序进行Docker化

[英]Dockerizing a Rails application with docker-compose

Hi I am trying to adapt my-app to docker. 嗨,我正在尝试使我的应用程序适应docker。

My app stack is 我的应用程序堆栈是

  • Ruby 2.4.0 Ruby 2.4.0
  • Rails 5.0.1 Rails 5.0.1
  • Postgres 9.5.8 Postgres 9.5.8

I created a Dockerfile to create an image of my-app 我创建了一个Dockerfile来创建my-app的映像

FROM ruby:2.4

RUN apt-get update && apt-get install -y \
    software-properties-common \
    python-software-properties \
    build-essential \
    libpq-dev \
    libxml2-dev libxslt1-dev \
    libqt4-webkit libqt4-dev xvfb \
    nodejs \
    postgresql

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY Gemfile /usr/src/app
COPY Gemfile.lock /usr/src/app
RUN bundle install
ADD . /usr/src/app
COPY ./docker/docker-entrypoint.sh /usr/src/app/docker-entrypoint.sh
RUN chmod +x /usr/src/app/docker-entrypoint.sh
RUN bundle install

Then I created a docker-compose.yml 然后我创建了一个docker-compose.yml

version: '3'
services:

  app:
    image: app_rails
    entrypoint: ['/usr/src/app/docker-entrypoint.sh']
    ports:
      - "3000:3000"
    depends_on:
      - postgres
    links:
      - postgres
    volumes:
      - .:/usr/src/app
    environment:
      DATABASE_URL: postgres://postgres@postgres@postgres:5432/postgres?pool=5&encoding=utf-8

  postgres:
    image: postgres:9.5.8
    ports:
     - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres

And here is the script docker-entrypoint.sh 这是脚本docker-entrypoint.sh

#!/bin/sh
set -e

host="$1"
echo "host: ${host}"

if [ "$#" -gt 0 ]; then shift; fi

until psql -h "$host" -U "postgres" -c '\q'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

rails db:create
rails db:migrate RAILS_ENV=development

bundle exec rails server -b 0.0.0.0

exec "$@"

and my database.yml 和我的database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000

development:
  adapter: postgresql
  database: app_development
  pool: 5
  timeout: 5000
  url: postgres://postgres@postgres/postgres:5432

test:
  adapter: postgresql
  database: app_test
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: app_production
  pool: 5
  timeout: 5000

I am having trouble running the script 我在运行脚本时遇到问题

web_1       |   connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
web_1       | Postgres is unavailable - sleeping

Since I am new to docker an advise will be wellcome 由于我是Docker的新手,所以建议会很受欢迎

You're DATABASE_URL can't be localhost, it needs to be the name of service. 您是DATABASE_URL,不能是localhost,它必须是服务名称。 It also needs username and password in there: 还需要在其中输入用户名和密码:

postgres://postgres:postgres@postgres/postgres

It doesn't need post since it uses the default. 它使用默认值,因此无需发布。 Here's examples of URL's: https://stackoverflow.com/a/20722229/749924 以下是URL的示例: https : //stackoverflow.com/a/20722229/749924

Try 尝试

chmod +x /usr/src/my-app/docker-entrypoint.sh

to ensure your entry point is an executable 确保您的入口点是可执行文件

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

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