2

When I use the google cloud run service my docker container will return the error:

PG::ConnectionBad: 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"?

I've enabled the cloud sql admin api on the relevant project. I ssh'ed into the instance that I was running with GCP services available in the google cloud shell, and checked /var/run/postgresql/.s.PGSQL.5432. There was nothing available. Google cloud run docks say to set the designation for the socket to under /cloudsql/, but no socket appears to exist there either.

Nothing in cloud sql/run open issues or the issue tracker suggests that this should be an issue.

Deploy command uses the --add-cloudsql-instances flag without error, so I believe there should be no issue there.

Relevant database.yml section:

staging:
  adapter: postgresql
  encoding: utf8
  pool: 5
  timeout: 5000
  database: project_staging
  username: project_staging
  password: <%= Rails.application.credentials[:db_password] %>
  socket: "/cloudsql/my-project-name:asia-northeast1:project-database-name/"

Dockerfile to set up the container -

FROM ruby:2.6.2

ARG environment
// Bunch of env code

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /usr/src/app
RUN gem install bundler
COPY Gemfile Gemfile.lock ./
ENV BUNDLE_FROZEN=true
RUN bundle install
COPY . .

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

ENV RAILS_LOG_TO_STDOUT=true
Do I need to install more than just postgresql-client here?

Almost certainly irrelevant, but the start script:

cd /usr/src/app

bundle exec rake db:create

bundle exec rake db:migrate

# Do some protective cleanup
> log/${RAILS_ENV}.log
rm -f tmp/pids/server.pid

bundle exec rails server -e ${RAILS_ENV} -b 0.0.0.0 -p $PORT

I'm honestly baffled here. Is it a configuration issue? A cloud run issue? Am I missing some kind of package? I expected it to just connect to the socket without issue on boot.

JapanRob
  • 354
  • 3
  • 16

2 Answers2

2

I have followed this Medium guide(parts 1, 2, 3 and 4) to create a Cloud Run with Ruby and connect it to a Cloud SQL instance with no problem at all, can you try to comparing it to your deploy or even try to follow the steps to see if what you did differs on what they explain there?

Also, in case that helps, there is a similar case I've found in another post where they were facing the same issue even though it's not deployed in Cloud Run, might be helpful. Another Medium post addresses this same issue too and gives a set of solutions.

bhito
  • 2,083
  • 7
  • 13
0

This error message shows that your app tries to connect to the "default localhost" socket file (/var/run/postgresql/.s.PGSQL.5432) but not the /cloudsql/my-project-name:asia-northeast1:project-database-name/ socket file.

That means the HOST value in your Rail app database config is "empty" or "localhost". You shoud try to dump the database config from your Rail app and correct this value.

CK.Nguyen
  • 1,258
  • 17
  • 16