skip to Main Content

I’m trying to dockerize my Rails application, but I have this error when I want to run it with docker-compose:

! Unable to load application: PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "postgres" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

my docker-compose file is:

version: '2'
services:
  web:
    build: .
    image: cda_app
    container_name: "cda_app_web"
    ports:
      - '127.0.0.1:${WEB_PORT}:3000'
    env_file: .env
    volumes:
      - gems:/gems
      - ./:/var/www/app
    logging:
      driver: 'json-file'
      options:
        max-size: '100m'
        max-file: '5'
    links:
      - redis
      - postgres
    entrypoint: 'bundle exec puma -C config/puma.rb'
  redis:
    image: 'redis'
    volumes:
      - redis:/data
      - redis_log:/var/log/redis
  postgres:
    image: 'postgres:12.4'
    volumes:
      - postgres:/var/lib/postgresql/data
      - postgres_log:/var/log/postgresql
volumes:
  gems:
  redis:
  redis_log:
  postgres:
  postgres_log:

Dockerfile:

ARG BASE_IMAGE=ruby:2.7.1
FROM $BASE_IMAGE

RUN apt-get update && 
  apt-get install -y 
    apt-utils 
    build-essential 
    cmake 
    curl 
    ghostscript 
    libmagic-dev 
    libpq-dev 
    openssh-client 
    rename 
  && rm -rf /var/lib/apt/lists/*

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash && apt-get install -y nodejs
RUN npm install -g yarn

WORKDIR /var/www/app

RUN mkdir /gems
ENV BUNDLE_PATH=/gems
RUN gem install bundler

ARG BUNDLE_WITHOUT=development:test
COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs $(nproc) --with BUNDLE_WITHOUT

COPY package.json yarn.lock ./
RUN yarn install --check-files --ignore-optional

COPY . .

RUN rename -f -v 's/.sample//' config/*sample.yml

EXPOSE 3000

CMD ./bin/puma -b tcp://0.0.0.0:3000

my database.yml:

default: &default
  pool: <%= ENV["DB_POOL"] %>
  template: 'template0'
  adapter: 'postgresql'
  database: <%= ENV["DB_NAME"] %>
  username: <%= ENV["DB_USER"] %>
  host: <%= ENV["DB_HOST"] %>
  port: <%= ENV["DB_PORT"] %>
  password: <%= ENV["DB_PASSWORD"] %>
  timeout: 5000
  encoding: 'utf8'
  min_messages: WARNING

development:
  <<: *default
  database: <%= ENV["DB_NAME"] %>_development

test:
  <<: *default
  database: <%= ENV["DB_NAME"] %>_test<%= ENV['TEST_ENV_NUMBER'] %>

production:
  <<: *default
  database: <%= ENV["DB_NAME"] %>_production

.env file:


DB_NAME=cda_database
DB_HOST=postgres
DB_USER=postgres
DB_PASSWORD=postgres
DB_PORT=5432
DB_POOL=5

I can’t understand what I’m doing wrong?!
It seems I did everything correctly, but I’m getting the error.
I’m using Digitalocean. Also, the database is working outside of docker, but it can’t be accessible in docker.

2

Answers


  1. You can try it in docker-compose file. I hope it working

    postgres:
      image: 'postgres:12.4'
      volumes:
        - postgres:/var/lib/postgresql/data
        - postgres_log:/var/log/postgresql
      environment:
        - POSTGRES_USER= postgres
        - POSTGRES_PASSWORD= postgres
    
    Login or Signup to reply.
  2. You can write the environment variables each one for postgresql in docker-compose.yml, example:

    version: '2'
    services:
      web:
        build: .
        image: cda_app
        container_name: "cda_app_web"
        ports:
          - '127.0.0.1:${WEB_PORT}:3000'
        env_file: .env
        volumes:
          - gems:/gems
          - ./:/var/www/app
        logging:
          driver: 'json-file'
          options:
            max-size: '100m'
            max-file: '5'
        links:
          - redis
          - postgres
        entrypoint: 'bundle exec puma -C config/puma.rb'
      redis:
        image: 'redis'
        volumes:
          - redis:/data
          - redis_log:/var/log/redis
      postgres:
        image: 'postgres:12.4'
        volumes:
          - postgres:/var/lib/postgresql/data
          - postgres_log:/var/log/postgresql
      environment:
          POSTGRES_PASSWORD: "${DB_PASSWORD}"
          POSTGRES_USER: "${DB_USER}"
          POSTGRES_DB: "${DB_NAME}"
    
    volumes:
      gems:
      redis:
      redis_log:
      postgres:
      postgres_log:
    
    

    Be aware that the file .env must be in the same directory than your docker-compose.yml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search