0

I'm currently having an issue with my docker-compose that have these services. Rails app and Postgres. These are my configurations:

docker-compose.yml

version: '3'
services:
 db:
  image: postgres:alpine
  restart: always
  volumes:
   - ./tmp/db:/var/lib/postgresql/data
  ports:
    - "5432:5432"
  environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  
 app:
  build: .
  restart: always
  command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  volumes:
   - .:/myapp
   - bundle_path:/bundle
  ports:
   - "3000:3000"
  depends_on:
   - db

volumes:
  bundle_path:

Dockerfile

FROM ruby:2.5.3-slim

# install rails dependencies
RUN apt-get update -qq \
  && apt-get install -y \
  # Needed for certain gems
  build-essential \
  # Needed for postgres gem
  libpq-dev \
  # Others
  nodejs \
  vim-tiny \   
  # The following are used to trim down the size of the image by removing unneeded data
  && apt-get clean autoclean \
  && apt-get autoremove -y \
  && rm -rf \
  /var/lib/apt \
  /var/lib/dpkg \
  /var/lib/cache \
  /var/lib/log

# Changes localtime to Singapore
RUN cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime

# create a folder /myapp in the docker container and go into that folder
RUN mkdir /myapp

WORKDIR /myapp

COPY Gemfile /myapp/Gemfile

COPY Gemfile.lock /myapp/Gemfile.lock

# Run bundle install to install gems inside the gemfile
RUN bundle install

ADD . /myapp

CMD bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"

database.yml

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

development:
  <<: *default
  database: myapp_development
  host: db
  username: postgres
  password: postgres
  port: 5432
  

I can build the app using docker-compose build but whenever I docker-compose up the service db exited but my rails app is running.

This is the logs I'm getting when I run docker-compose up

db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
db_1   | If you want to create a new database system, either remove or empty
db_1   | the directory "/var/lib/postgresql/data" or run initdb
db_1   | with an argument other than "/var/lib/postgresql/data".

The error I'm getting when I access http://localhost:3000 is

could not connect to server: No route to host Is the server running on host "db" (172.18.0.2) and accepting TCP/IP connections on port 5432?

1 Answer 1

2

I think you should use volume for Postgres too.

services:
  db:
    image: postgres:alpine
    restart: always
    volumes:
     - postgres_volume:/var/lib/postgresql/data
volumes:
  postgres_volume:

I had similar issue and fixed it with that. Try also to restart Docker.

Sign up to request clarification or add additional context in comments.

2 Comments

It's working now. Wondering for my other rails app that uses docker-compose I don't need to mount the volume of postgres and it's working. Not sure for this one it's needed.
I honestly don't know but I spent some time with volume issues, especially with Postgres. Maybe it's a bad configuration from my side but I never figured out why/how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.