Here is my dockerfile and here is my docker-compose file. When I ran docker-compose run web rake db:migrate I got this err 
rake aborted!
failed to execute:
pg_dump -s -x -O -f /usr/src/app/db/structure.sql --schema=public --schema=partitioning docker_rails_dev
Please check the output above for any errors and make sure that `pg_dump` is installed in your PATH and has proper permissions.
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/postgresql_database_tasks.rb:90:in `run_cmd'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/postgresql_database_tasks.rb:55:in `structure_dump'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/database_tasks.rb:183:in `structure_dump'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:279:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:53:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:45:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)
so I entered into postgres container and ran pg_dump and got this err
pg_dump: [archiver (db)] connection to database "root" failed: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
but running pg_dump --version gives me the version number of postgres.
any idea why this is happening?
Using run command is not the best way since you need postgres server up and running. Try with exec, have in mind that using -f as argument in pg_dump will create a file inside the running instance so you might want to write file using > to redirect output and create the dump outside the container. In my case I do something like:
docker-compose -f $PROJECT_PATH/docker-compose-$ENV.yml exec postgres pg_dump -U $DBUSER -Z 9 $DOCKERSERVICE > backups/db/postgres-`date +"%m%d%y%H%M%S".sql.gz`
Where $DOCKERSERVICE is usually postgres
For you case it might be something like:
docker-compose exec web rake db:migrate
And a better reference for docker-compose exec
I knew that question is old, but I stuck with the same problem:
pg_dump: [archiver (db)] connection to database "root" failed: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
However, I have no setup for root role. So the fix is export PGUSER=postgres for me. 
It is even possible to set PGUSER through compose file. Smth like:
services:
  some_exact_service:
    ...
    environment:
      PGUSER: postgres
Hope you understand that you may replace postgres role name with your own ;).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With