使用docker-compose

时间:2017-11-22 13:10:24

标签: ruby-on-rails docker docker-compose dockerfile

您好我正在尝试将my-app改编为docker。

我的应用堆栈是

  • Ruby 2.4.0
  • Rails 5.0.1
  • Postgres 9.5.8

我创建了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

然后我创建了一个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

这是脚本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 "$@"

和我的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

我无法运行脚本

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

因为我是码头工人的新手,所以建议会好受欢迎

2 个答案:

答案 0 :(得分:2)

你是DATABASE_URL不能是localhost,它需要是服务的名称。它还需要用户名和密码:

postgres://postgres:postgres@postgres/postgres

它不需要发布,因为它使用默认值。以下是网址的示例:https://stackoverflow.com/a/20722229/749924

答案 1 :(得分:1)

尝试

---
- hosts: group1
sudo: yes
tasks:

  - name: Download and Extract apache
  unarchive:
   src: http://mirrors.up.pt/pub/apache/tomcat/tomcat-9/v9.0.1/bin/apache-tomcat-9.0.1.tar.gz
   dest: /opt/
   remote_src: yes

- name: Copy file to host
  copy: src=/etc/ansible/files/myfile.xml dest=/opt/apache-tomcat-9.0.1/conf/myfile.xml

确保您的入口点是可执行文件