用于 Rails 应用程序的 Docker 组合

问题描述 投票:0回答:1

我有下面的 docker compose 文件,我对此有几个问题,

version: '3.8'

services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile
    platform: linux/amd64
    command: bash -c "rm -f tmp/pids/server.pid && bin/rails s -p 3000 -b '0.0.0.0'"
    env_file:
      - .env.development
    volumes:
      - .:/usr/src/app
      - bundle:/usr/local/bundle
    ports:
      - "3000:3000"
    environment:
      REDIS_HOST: redis
      REDIS_URL: redis://redis-server:6379/0
      REDIS_PORT: 6379
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started

  db:
    image: postgres:16
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
    volumes:
      - pg_data:/var/lib/postgresql/data
    healthcheck:
      test: pg_isready -U postgres
      interval: 2s
      timeout: 5s
      retries: 30

  redis:
    image: redis:7.0-alpine
    restart: always
    volumes:
      - redis_data:/data
    ports:
      - 6379

  sidekiq:
    build: .
    command: bundle exec sidekiq
    depends_on:
      - redis

  rabbitmq:
    image: "rabbitmq:latest"
    ports:
      - "5672:5672"
  # sneakers:
  #   build: .
  #   command: "bundle exec rails sneakers:run"
  #   depends_on:
  #     - web
  #     - rabbitmq
  #     - redis

volumes:
  pg_data:
  redis_data:
  bundle:

Dockerfile

# syntax = docker/dockerfile:1

ARG RUBY_VERSION=3.2.2
FROM --platform=linux/amd64 registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
RUN export DOCKER_DEFAULT_PLATFORM=linux/amd64
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config \
    rabbitmq-server

# ENV RAILS_ENV production
# ENV RACK_ENV production

WORKDIR /rails

ENV BUNDLE_DEPLOYMENT="1" \
    BUNDLE_PATH="/usr/local/bundle" \
    BUNDLE_WITHOUT="development"

FROM base as build
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile
COPY . .
RUN bundle exec bootsnap precompile app/ lib/
FROM base
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
EXPOSE 3000
CMD ["./bin/rails", "server"]

这是database.yml文件

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  port: <%= ENV['DB_PORT'] %>

test:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  port: <%= ENV['DB_PORT'] %>

cd_dev:
  <<: *default
  database: <%= ENV['dbClusterIdentifier'] %>
  username: <%= ENV['username'] %>
  password: <%= ENV['password'] %>
  host: <%= ENV['host'] %>
  port: <%= ENV['port'] %>

cd_qa:
  <<: *default
  database: <%= ENV['dbClusterIdentifier'] %>
  username: <%= ENV['username'] %>
  password: <%= ENV['password'] %>
  host: <%= ENV['host'] %>
  port: <%= ENV['port'] %>

cd_staging:
  <<: *default
  database: <%= ENV['dbClusterIdentifier'] %>
  username: <%= ENV['username'] %>
  password: <%= ENV['password'] %>
  host: <%= ENV['host'] %>
  port: <%= ENV['port'] %>

cd_production:
  <<: *default
  database: <%= ENV['dbClusterIdentifier'] %>
  username: <%= ENV['username'] %>
  password: <%= ENV['password'] %>
  host: <%= ENV['host'] %>
  port: <%= ENV['port'] %>

我在日志中收到以下错误

cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:46.265 # Can't handle RDB format version 11
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:46.266 # Fatal error loading the DB: Invalid argument. Exiting.
cd-notification-common-service-redis-1     | 1:C 13 Feb 2024 21:29:48.167 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
cd-notification-common-service-redis-1     | 1:C 13 Feb 2024 21:29:48.167 # Redis version=7.0.15, bits=64, commit=00000000, modified=0, pid=1, just started
cd-notification-common-service-redis-1     | 1:C 13 Feb 2024 21:29:48.167 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:48.168 * monotonic clock: POSIX clock_gettime
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:48.174 * Running mode=standalone, port=6379.
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:48.174 # Server initialized
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:48.176 # Can't handle RDB format version 11
cd-notification-common-service-redis-1     | 1:M 13 Feb 2024 21:29:48.176 # Fatal error loading the DB: Invalid argument. Exiting.
cd-notification-common-service-redis-1 exited with code 1

我有几个问题

  1. 我正在使用 aws 秘密存储,当我使用单个 docker 文件时它工作正常,我是否需要在 docker-compose 中再次定义秘密?假设我在数据库 yml 中有很多 env。它是从秘密管理器加载的,我是否需要再次在 docker-compose 中定义它?

  2. Redis 位于不同的容器中,Rails 应用程序位于不同的容器中,我不断收到错误

    Cannot assign requested address - connect(2) for [::1]:6379 (Errno::EADDRNOTAVAIL) 
    ,如何在 Rails 应用程序中使用 Redis 容器?

ruby-on-rails docker docker-compose redis
1个回答
0
投票
  1. 尝试从 Rails 项目文件夹中删除
    dump.rdb
    文件(如果存在)
© www.soinside.com 2019 - 2024. All rights reserved.