15

I've been following this guide of how to set up a Rails Development Environment using Docker: Setting Up A Rails Development Environment Using Docker.

I've hit a few snags along the way, but I've managed to get through most of them up until the step of running Rails migration. Running the command docker-compose run web rake db:migrate yields the following result:

rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

docker-compose.yml:

version: '2'
services:
  db:
    image: postgres
    volumes:
      - ./pgdata:/pgdata
    environment:
      POSTGRES_DB: myapp_development
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD:
      PGDATA: /pgdata
  web:
    build: .
    command: bundle exec rails server --port 5000 --binding 0.0.0.0
    volumes_from:
      - container:myapp-web-sync:rw
      - container:myapp-bundle-sync:rw
    volumes:
      - ./keys:/root/.ssh/
    ports:
      - "5000:5000"
    environment:
      REDIS_URL: redis://redis:6379
      GEM_HOME: /bundle
    links:
      - db
      - redis
volumes:
  myapp-web-sync:
    external: true
  myapp-bundle-sync:
    external: true

1 Answer 1

23

You try to connect to localhost:5432 in other words you try to connect to the web container, but your point is db. Just specify Database host in your application like db

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

3 Comments

Thanks for the response. Are you saying it should be db:5432 in my Rails database.yml file (the file which specifies what database to connect to)?
Yes, exactly. please try this solution and let me know what happens.
I just tried this hint on my own Dockerized Rails application and it indeed worked. When connecting within Docker, the host in database.yml should be the container name that contains the db, not localhost. Of course, that depends on your configuration.

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.