Recently, I've been running rails with Docker, and there are some pits, and here's a note.
First build Project:
docker-compose build
And then you start to get an error:
Psql: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"?
The error here is that the/var/run/postgresql/.s.pgsql.5432 file could not be found, but my local operation is no problem. So find Docker's example of Docker rails from getting started to practicing this book, here's the address: I found my database.yml
file wrong. Mine is this:
default: &default adapter: postgresql encoding: unicode`请输入代码` # For details on connection pooling,www.97yingyuan.org see Rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
the database target that Rails reads by default is localhost, and we need to specify the db of the capacitance manually. Similarly, the user name needs to be modified to coincide with the Postgres image reservation. Open the newly generated database.yml file. Replace with the following:
default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: postgres host: db
So run again docker-compose build
, success build! And then run up again docker-compose up
, the tragic error again:
Activerecord::nodatabaseerror (fatal:database "heroku_app_development" does not exist
Tip No database is created heroku_app_development
, OK, then create one:
sudo docker ps
#列出所有容器
sudo docker exec -it [container ID] /bin/bash
#进入postgres
sudo su - postgres
#切换到postgres用户, because this user has Createdb permissions
ceratedb heroku_app_development
#heroku_app_development Creation Complete!
Again run docker-compose up
, and error:
Migrations is pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
Railser should be familiar with the database migration, into the Web container:
sudo docker exec -it [container ID] /bin/bash
#进入web容器
rails db:migrate
#迁移成功!
Docker Pit note psql:could not connect to server