Не удалось открыть TCP-соединение с lvh: [порт] Соединение отклонено с HTTParty.get на DOCKER RAILS NGINX - PullRequest
0 голосов
/ 06 июня 2018

Я пытаюсь настроить среду докера, используя docker-compose с изображениями rails (работает puma), nginx, mysql ,asticsearch.

Но когда я пытаюсь вызвать его, используя HTTParty.get('http://lvh.me:8888')

это не удалось, и я получил сообщение об ошибке

Errno :: ECONNREFUSED (Не удалось открыть TCP-соединение с lvh.me:8888 (Соединение отклонено - connect (2) для "lvh.me")"порт 8888))

Мой файл Docker-Compose:

version: '3.3'
services:
    beecomredis:
        image: redis:4.0.8
        ports:
            - "6379:6379"
    beecomdb:
        image: mysql:5.7.21
        volumes:
            - ./mysql_data/mysql:/var/lib/mysql
        ports:
            - "6603:3306"
    beecomec:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.2.2
        #container_name: elasticsearch
        environment:
          - http.cors.enabled=true
          - http.cors.allow-origin="*"
          - node.master=true
          - cluster.name=docker-cluster
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - xpack.security.enabled=false
        ulimits:
          memlock:
            soft: -1
            hard: -1
        volumes:
            - ./elastic_data/elasticsearch/data:/usr/share/elasticsearch/data
            - ./elastic_data/elasticsearch/logs:/usr/share/elasticsearch/logs
        ports:
            - '9200:9200'
            - '9300:9300'
    beecomnginx:
        build:
          context: .
          dockerfile: Dockerfile-nginx
        ports:
          - "8888:80"
    beecom:
        build: .
        command: foreman start
        volumes:
            - .:/beecom
         expose:
            - "3000"
        depends_on:
            - beecomdb
            - beecomredis
            - beecomec
            - beecomnginx

Мой файл nginx.conf:

upstream rails_app {
   server beecom:3000;
}

server {
   # define your domain
   server_name www.example.com;

# define the public application root
   root   $RAILS_ROOT/public;
   index  index.html;

# define where Nginx should write its logs
   access_log $RAILS_ROOT/log/nginx.access.log;
   error_log $RAILS_ROOT/log/nginx.error.log;

   # deny requests for files that should never be accessed
   location ~ /\. {
      deny all;
   }

   location ~* ^.+\.(rb|log)$ {
      deny all;
   }

   # serve static (compiled) assets directly if they exist (for rails production)
   location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/   {
      try_files $uri @rails;
      access_log off;
      gzip_static on;

      # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;

      add_header Last-Modified "";
      add_header ETag "";
      break;
   }

   # send non-static file requests to the app server
   location / {
      try_files $uri @rails;
   }

   location @rails {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://rails_app;
   }
}

Мой файл DockerFile:

FROM ruby:2.5.0-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
 build-essential \
 default-libmysqlclient-dev \
 mysql-client \
 libmagickwand-dev \
 imagemagick \
 curl \
 git \
 gnupg2 \
/sources.list.d/yarn.list \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - && apt-get install -y --no-install-recommends nodejs

RUN gem update --system

RUN mkdir -p /beecom
WORKDIR /beecom

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
COPY package.json package.json

ENV RAILS_ENV development
ENV RACK_ENV development

RUN bundle install

RUN set :environment, 'development'

COPY config/puma.rb config/puma.rb
COPY . /beecom

EXPOSE 3000

CMD [ "foreman", "start" ]

И, наконец, мой Dockerfile-nginx:

# Base image:
FROM nginx

# Install dependencies
RUN apt-get update -qq && apt-get -y install apache2-utils

# establish where Nginx should look for files
ENV RAILS_ROOT /beecom

# Set our working directory inside the image
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT

# create log directory
RUN mkdir log

# copy over static assets
COPY ./public public/

# Copy Nginx config template
COPY ./config/nginx.conf /tmp/docker.nginx

# substitute variable references in the Nginx config template for real values from the environment
# put the final config in its place
 RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
RUN rm -rf /etc/nginx/sites-available/default
ADD config/nginx.conf /etc/nginx/sites-enabled/nginx.conf

EXPOSE 80

# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e. `docker stop`)
CMD [ "nginx", "-g", "daemon off;" ]

О, и я забыл опубликовать свой конфиг Puma:

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum, this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port        ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 3 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted this block will be run, if you are using `preload_app!`
# option you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, Ruby
# cannot share connections between processes.
#
# on_worker_boot do
#   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

1 Ответ

0 голосов
/ 07 июня 2018

Наконец я обнаружил, что добавление привязки к моему config/puma.rb файлу.

bind 'tcp://0.0.0.0:8888'

Решило мою проблему.

ОБНОВЛЕНО: Наконец, лучший способ найти это исправить - это изменитьмой nginx.config вот так:

upstream MyAppUpstream_website {
   server website:3000;
}

server {
  listen 443 ssl;
  listen      80;
  server_name lvh.me
  keepalive_timeout 900;

  ssl_certificate      /[key];
  ssl_certificate_key  /[key];

# define the public application root
   root   $RAILS_ROOT/public;
   index  index.html;

# define where Nginx should write its logs
   access_log $RAILS_ROOT/log/nginx.access.log;
   error_log $RAILS_ROOT/log/nginx.error.log;

   # deny requests for files that should never be accessed
   location ~ /\. {
      deny all;
   }

   location ~* ^.+\.(rb|log)$ {
      deny all;
   }

   # serve static (compiled) assets directly if they exist (for rails production)
   location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/   {
      try_files $uri @rails;
      access_log off;
      gzip_static on;

      # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;

      add_header Last-Modified "";
      add_header ETag "";
      break;
   }

   # send non-static file requests to the app server
   location / {
      try_files $uri @rails;
   }

   location @rails {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://MyAppUpstream_website;

   }
}
...