问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/41510010/pg-dump-issue-in-docker