Why is dockerized Rails app raising “PG::ConnectionBad”?
I am trying to "dockerize" an existing Rails development app. This is my first time experimenting with Docker.
After searching and reading a lot of information online, I finally managed to get a container up and running with several services: app, postgres, redis, sidekiq and guard.
But I am having trouble connecting to the database.
Starting postgres ... done
Starting redis ... done
rails aborted!
PG::ConnectionBad: 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?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
No doubt there is a simple configuration that I've overlooked, but I've been unable to figure out what or find a suitable guide.
I did notice that the IP addresses appear not to match.
$ docker-compose ps
Name State Ports
------------------------------------------------------
postgres Up 0.0.0.0:5432->5432/tcp
The top error suggests postgres should be at 127.0.0.1. Could this be the reason?
Can anyone point me in the right direction?
Dockerfile
# Dockerfile
FROM ruby:2.4-alpine
# Install runtime dependencies
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline-dev yarn git
RUN apk add --virtual build-deps build-base postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev git
# Copy the app's code into the container
ENV APP_HOME /app
COPY . $APP_HOME
WORKDIR $APP_HOME
# Budnle gems
RUN bundle install --jobs 4
# Install Yarn packages
RUN yarn install
RUN yarn upgrade
# Configure production environment variables
ENV RAILS_ENV=production
RACK_ENV=production
# Expose port 3000 from the container
EXPOSE 3000
# Run puma server
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres
ports:
- "5432:5432"
volumes:
- $HOME/postgres-data:/var/lib/postgresql
image: postgres:9.6.9
redis:
container_name: redis
ports:
- "6379:6379"
links:
- postgres
image: redis:5.0-rc
web:
container_name: web
build: .
ports:
- "3000:3000"
command: /bin/sh -c "rails s -b 0.0.0.0 -p 3000"
env_file:
- .env
links:
- postgres
- redis
guard:
build: .
env_file:
- .env
command: bundle exec guard --no-bundler-warning --no-interactions
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
links:
- postgres
- redis
env_file:
- .env
volumes:
redis:
postgres:
sidekiq:
guard:
ruby-on-rails docker
|
show 1 more comment
I am trying to "dockerize" an existing Rails development app. This is my first time experimenting with Docker.
After searching and reading a lot of information online, I finally managed to get a container up and running with several services: app, postgres, redis, sidekiq and guard.
But I am having trouble connecting to the database.
Starting postgres ... done
Starting redis ... done
rails aborted!
PG::ConnectionBad: 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?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
No doubt there is a simple configuration that I've overlooked, but I've been unable to figure out what or find a suitable guide.
I did notice that the IP addresses appear not to match.
$ docker-compose ps
Name State Ports
------------------------------------------------------
postgres Up 0.0.0.0:5432->5432/tcp
The top error suggests postgres should be at 127.0.0.1. Could this be the reason?
Can anyone point me in the right direction?
Dockerfile
# Dockerfile
FROM ruby:2.4-alpine
# Install runtime dependencies
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline-dev yarn git
RUN apk add --virtual build-deps build-base postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev git
# Copy the app's code into the container
ENV APP_HOME /app
COPY . $APP_HOME
WORKDIR $APP_HOME
# Budnle gems
RUN bundle install --jobs 4
# Install Yarn packages
RUN yarn install
RUN yarn upgrade
# Configure production environment variables
ENV RAILS_ENV=production
RACK_ENV=production
# Expose port 3000 from the container
EXPOSE 3000
# Run puma server
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres
ports:
- "5432:5432"
volumes:
- $HOME/postgres-data:/var/lib/postgresql
image: postgres:9.6.9
redis:
container_name: redis
ports:
- "6379:6379"
links:
- postgres
image: redis:5.0-rc
web:
container_name: web
build: .
ports:
- "3000:3000"
command: /bin/sh -c "rails s -b 0.0.0.0 -p 3000"
env_file:
- .env
links:
- postgres
- redis
guard:
build: .
env_file:
- .env
command: bundle exec guard --no-bundler-warning --no-interactions
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
links:
- postgres
- redis
env_file:
- .env
volumes:
redis:
postgres:
sidekiq:
guard:
ruby-on-rails docker
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
1
From the point of view of your application, the database will be visible at the host namepostgres
(the name of the block in thedocker-compose.yml
file). You can remove all of thecontainer_name:
andlinks:
sections, they don't really have a practical effect.
– David Maze
Jan 3 at 1:55
Thanks @DavidMaze, a couple questions: 1) my database.yml containshost: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought thatlinks:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!
– Andy Harvey
Jan 3 at 2:24
1
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on
– Jay Dorsey
Jan 3 at 2:43
1
...anddepends_on:
is a fairly weak ordering constraint (if youdocker-compose up web
thendepends_on:
will cause it to startpostgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).
– David Maze
Jan 3 at 4:49
|
show 1 more comment
I am trying to "dockerize" an existing Rails development app. This is my first time experimenting with Docker.
After searching and reading a lot of information online, I finally managed to get a container up and running with several services: app, postgres, redis, sidekiq and guard.
But I am having trouble connecting to the database.
Starting postgres ... done
Starting redis ... done
rails aborted!
PG::ConnectionBad: 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?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
No doubt there is a simple configuration that I've overlooked, but I've been unable to figure out what or find a suitable guide.
I did notice that the IP addresses appear not to match.
$ docker-compose ps
Name State Ports
------------------------------------------------------
postgres Up 0.0.0.0:5432->5432/tcp
The top error suggests postgres should be at 127.0.0.1. Could this be the reason?
Can anyone point me in the right direction?
Dockerfile
# Dockerfile
FROM ruby:2.4-alpine
# Install runtime dependencies
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline-dev yarn git
RUN apk add --virtual build-deps build-base postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev git
# Copy the app's code into the container
ENV APP_HOME /app
COPY . $APP_HOME
WORKDIR $APP_HOME
# Budnle gems
RUN bundle install --jobs 4
# Install Yarn packages
RUN yarn install
RUN yarn upgrade
# Configure production environment variables
ENV RAILS_ENV=production
RACK_ENV=production
# Expose port 3000 from the container
EXPOSE 3000
# Run puma server
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres
ports:
- "5432:5432"
volumes:
- $HOME/postgres-data:/var/lib/postgresql
image: postgres:9.6.9
redis:
container_name: redis
ports:
- "6379:6379"
links:
- postgres
image: redis:5.0-rc
web:
container_name: web
build: .
ports:
- "3000:3000"
command: /bin/sh -c "rails s -b 0.0.0.0 -p 3000"
env_file:
- .env
links:
- postgres
- redis
guard:
build: .
env_file:
- .env
command: bundle exec guard --no-bundler-warning --no-interactions
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
links:
- postgres
- redis
env_file:
- .env
volumes:
redis:
postgres:
sidekiq:
guard:
ruby-on-rails docker
I am trying to "dockerize" an existing Rails development app. This is my first time experimenting with Docker.
After searching and reading a lot of information online, I finally managed to get a container up and running with several services: app, postgres, redis, sidekiq and guard.
But I am having trouble connecting to the database.
Starting postgres ... done
Starting redis ... done
rails aborted!
PG::ConnectionBad: 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?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
No doubt there is a simple configuration that I've overlooked, but I've been unable to figure out what or find a suitable guide.
I did notice that the IP addresses appear not to match.
$ docker-compose ps
Name State Ports
------------------------------------------------------
postgres Up 0.0.0.0:5432->5432/tcp
The top error suggests postgres should be at 127.0.0.1. Could this be the reason?
Can anyone point me in the right direction?
Dockerfile
# Dockerfile
FROM ruby:2.4-alpine
# Install runtime dependencies
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline-dev yarn git
RUN apk add --virtual build-deps build-base postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev git
# Copy the app's code into the container
ENV APP_HOME /app
COPY . $APP_HOME
WORKDIR $APP_HOME
# Budnle gems
RUN bundle install --jobs 4
# Install Yarn packages
RUN yarn install
RUN yarn upgrade
# Configure production environment variables
ENV RAILS_ENV=production
RACK_ENV=production
# Expose port 3000 from the container
EXPOSE 3000
# Run puma server
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres
ports:
- "5432:5432"
volumes:
- $HOME/postgres-data:/var/lib/postgresql
image: postgres:9.6.9
redis:
container_name: redis
ports:
- "6379:6379"
links:
- postgres
image: redis:5.0-rc
web:
container_name: web
build: .
ports:
- "3000:3000"
command: /bin/sh -c "rails s -b 0.0.0.0 -p 3000"
env_file:
- .env
links:
- postgres
- redis
guard:
build: .
env_file:
- .env
command: bundle exec guard --no-bundler-warning --no-interactions
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
links:
- postgres
- redis
env_file:
- .env
volumes:
redis:
postgres:
sidekiq:
guard:
ruby-on-rails docker
ruby-on-rails docker
asked Jan 3 at 1:20
Andy HarveyAndy Harvey
5,4151158136
5,4151158136
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
1
From the point of view of your application, the database will be visible at the host namepostgres
(the name of the block in thedocker-compose.yml
file). You can remove all of thecontainer_name:
andlinks:
sections, they don't really have a practical effect.
– David Maze
Jan 3 at 1:55
Thanks @DavidMaze, a couple questions: 1) my database.yml containshost: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought thatlinks:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!
– Andy Harvey
Jan 3 at 2:24
1
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on
– Jay Dorsey
Jan 3 at 2:43
1
...anddepends_on:
is a fairly weak ordering constraint (if youdocker-compose up web
thendepends_on:
will cause it to startpostgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).
– David Maze
Jan 3 at 4:49
|
show 1 more comment
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
1
From the point of view of your application, the database will be visible at the host namepostgres
(the name of the block in thedocker-compose.yml
file). You can remove all of thecontainer_name:
andlinks:
sections, they don't really have a practical effect.
– David Maze
Jan 3 at 1:55
Thanks @DavidMaze, a couple questions: 1) my database.yml containshost: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought thatlinks:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!
– Andy Harvey
Jan 3 at 2:24
1
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on
– Jay Dorsey
Jan 3 at 2:43
1
...anddepends_on:
is a fairly weak ordering constraint (if youdocker-compose up web
thendepends_on:
will cause it to startpostgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).
– David Maze
Jan 3 at 4:49
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
1
1
From the point of view of your application, the database will be visible at the host name
postgres
(the name of the block in the docker-compose.yml
file). You can remove all of the container_name:
and links:
sections, they don't really have a practical effect.– David Maze
Jan 3 at 1:55
From the point of view of your application, the database will be visible at the host name
postgres
(the name of the block in the docker-compose.yml
file). You can remove all of the container_name:
and links:
sections, they don't really have a practical effect.– David Maze
Jan 3 at 1:55
Thanks @DavidMaze, a couple questions: 1) my database.yml contains
host: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought that links:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!– Andy Harvey
Jan 3 at 2:24
Thanks @DavidMaze, a couple questions: 1) my database.yml contains
host: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought that links:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!– Andy Harvey
Jan 3 at 2:24
1
1
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on– Jay Dorsey
Jan 3 at 2:43
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on– Jay Dorsey
Jan 3 at 2:43
1
1
...and
depends_on:
is a fairly weak ordering constraint (if you docker-compose up web
then depends_on:
will cause it to start postgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).– David Maze
Jan 3 at 4:49
...and
depends_on:
is a fairly weak ordering constraint (if you docker-compose up web
then depends_on:
will cause it to start postgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).– David Maze
Jan 3 at 4:49
|
show 1 more comment
1 Answer
1
active
oldest
votes
Since you're using the official postgres docker container, the official documentation has some related information:
The PostgreSQL image sets up trust authentication locally so you may
notice a password is not required when connecting from localhost
(inside the same container). However, a password will be required
if connecting from a different host/container.
Since you're not inside the same container, it looks like you may need to use a POSTGRES_USER
and POSTGRES_PASSWORD
. I tested this with a sample app locally, using your docker-compose.yml
and Dockerfile
and I needed to:
- Add
POSTGRES_USER
andPOSTGRES_PASSWORD
to both theweb
andpostgres
sections in the respectiveenv
/environment
(you'll add these to the.env
file you're including) - Add
username
andpassword
keys to thedatabase.yml
in the relevant sections to pull in the environment variables I set above
database.yml
:
default: &default
adapter: postgresql
encoding: unicode
host: postgres
pool: 5
timeout: 5000
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
With those changes I was able to connect to the postgres container/database from the web container.
My docker-compose ps
shows similar to yours:
Name Command State Ports
----------------------------------------------------------------------------------------
hello_app_sidekiq_1 bundle exec sidekiq -C con ... Exit 127
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
web /bin/sh -c bundle exec rai ... Up 0.0.0.0:3000->3000/tcp
I'm able to run docker-compose run --rm web /bin/ash
and drop into a rails console
, run rails db:migrate
, etc. without issue with the changes above.
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've usedwait_for_it.sh
in the past successfully
– Jay Dorsey
Jan 3 at 19:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015204%2fwhy-is-dockerized-rails-app-raising-pgconnectionbad%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you're using the official postgres docker container, the official documentation has some related information:
The PostgreSQL image sets up trust authentication locally so you may
notice a password is not required when connecting from localhost
(inside the same container). However, a password will be required
if connecting from a different host/container.
Since you're not inside the same container, it looks like you may need to use a POSTGRES_USER
and POSTGRES_PASSWORD
. I tested this with a sample app locally, using your docker-compose.yml
and Dockerfile
and I needed to:
- Add
POSTGRES_USER
andPOSTGRES_PASSWORD
to both theweb
andpostgres
sections in the respectiveenv
/environment
(you'll add these to the.env
file you're including) - Add
username
andpassword
keys to thedatabase.yml
in the relevant sections to pull in the environment variables I set above
database.yml
:
default: &default
adapter: postgresql
encoding: unicode
host: postgres
pool: 5
timeout: 5000
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
With those changes I was able to connect to the postgres container/database from the web container.
My docker-compose ps
shows similar to yours:
Name Command State Ports
----------------------------------------------------------------------------------------
hello_app_sidekiq_1 bundle exec sidekiq -C con ... Exit 127
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
web /bin/sh -c bundle exec rai ... Up 0.0.0.0:3000->3000/tcp
I'm able to run docker-compose run --rm web /bin/ash
and drop into a rails console
, run rails db:migrate
, etc. without issue with the changes above.
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've usedwait_for_it.sh
in the past successfully
– Jay Dorsey
Jan 3 at 19:18
add a comment |
Since you're using the official postgres docker container, the official documentation has some related information:
The PostgreSQL image sets up trust authentication locally so you may
notice a password is not required when connecting from localhost
(inside the same container). However, a password will be required
if connecting from a different host/container.
Since you're not inside the same container, it looks like you may need to use a POSTGRES_USER
and POSTGRES_PASSWORD
. I tested this with a sample app locally, using your docker-compose.yml
and Dockerfile
and I needed to:
- Add
POSTGRES_USER
andPOSTGRES_PASSWORD
to both theweb
andpostgres
sections in the respectiveenv
/environment
(you'll add these to the.env
file you're including) - Add
username
andpassword
keys to thedatabase.yml
in the relevant sections to pull in the environment variables I set above
database.yml
:
default: &default
adapter: postgresql
encoding: unicode
host: postgres
pool: 5
timeout: 5000
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
With those changes I was able to connect to the postgres container/database from the web container.
My docker-compose ps
shows similar to yours:
Name Command State Ports
----------------------------------------------------------------------------------------
hello_app_sidekiq_1 bundle exec sidekiq -C con ... Exit 127
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
web /bin/sh -c bundle exec rai ... Up 0.0.0.0:3000->3000/tcp
I'm able to run docker-compose run --rm web /bin/ash
and drop into a rails console
, run rails db:migrate
, etc. without issue with the changes above.
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've usedwait_for_it.sh
in the past successfully
– Jay Dorsey
Jan 3 at 19:18
add a comment |
Since you're using the official postgres docker container, the official documentation has some related information:
The PostgreSQL image sets up trust authentication locally so you may
notice a password is not required when connecting from localhost
(inside the same container). However, a password will be required
if connecting from a different host/container.
Since you're not inside the same container, it looks like you may need to use a POSTGRES_USER
and POSTGRES_PASSWORD
. I tested this with a sample app locally, using your docker-compose.yml
and Dockerfile
and I needed to:
- Add
POSTGRES_USER
andPOSTGRES_PASSWORD
to both theweb
andpostgres
sections in the respectiveenv
/environment
(you'll add these to the.env
file you're including) - Add
username
andpassword
keys to thedatabase.yml
in the relevant sections to pull in the environment variables I set above
database.yml
:
default: &default
adapter: postgresql
encoding: unicode
host: postgres
pool: 5
timeout: 5000
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
With those changes I was able to connect to the postgres container/database from the web container.
My docker-compose ps
shows similar to yours:
Name Command State Ports
----------------------------------------------------------------------------------------
hello_app_sidekiq_1 bundle exec sidekiq -C con ... Exit 127
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
web /bin/sh -c bundle exec rai ... Up 0.0.0.0:3000->3000/tcp
I'm able to run docker-compose run --rm web /bin/ash
and drop into a rails console
, run rails db:migrate
, etc. without issue with the changes above.
Since you're using the official postgres docker container, the official documentation has some related information:
The PostgreSQL image sets up trust authentication locally so you may
notice a password is not required when connecting from localhost
(inside the same container). However, a password will be required
if connecting from a different host/container.
Since you're not inside the same container, it looks like you may need to use a POSTGRES_USER
and POSTGRES_PASSWORD
. I tested this with a sample app locally, using your docker-compose.yml
and Dockerfile
and I needed to:
- Add
POSTGRES_USER
andPOSTGRES_PASSWORD
to both theweb
andpostgres
sections in the respectiveenv
/environment
(you'll add these to the.env
file you're including) - Add
username
andpassword
keys to thedatabase.yml
in the relevant sections to pull in the environment variables I set above
database.yml
:
default: &default
adapter: postgresql
encoding: unicode
host: postgres
pool: 5
timeout: 5000
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
With those changes I was able to connect to the postgres container/database from the web container.
My docker-compose ps
shows similar to yours:
Name Command State Ports
----------------------------------------------------------------------------------------
hello_app_sidekiq_1 bundle exec sidekiq -C con ... Exit 127
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
web /bin/sh -c bundle exec rai ... Up 0.0.0.0:3000->3000/tcp
I'm able to run docker-compose run --rm web /bin/ash
and drop into a rails console
, run rails db:migrate
, etc. without issue with the changes above.
answered Jan 3 at 4:04


Jay DorseyJay Dorsey
1,93511115
1,93511115
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've usedwait_for_it.sh
in the past successfully
– Jay Dorsey
Jan 3 at 19:18
add a comment |
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've usedwait_for_it.sh
in the past successfully
– Jay Dorsey
Jan 3 at 19:18
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
thanks @jay, this is very useful and seems to have solved my issue. There still seems to be some weirdness happening, with postgres starting up, shutting down then starting up again (see gist), but this is largely cosmetic and isnt affecting the app. Thanks! gist.github.com/ahharvey/f1b2dafa614e53820638df66731da523
– Andy Harvey
Jan 3 at 17:14
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
BTW, I'm also having some trouble getting Guard running. Any ideas? stackoverflow.com/questions/54026795/…
– Andy Harvey
Jan 3 at 17:15
1
1
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've used
wait_for_it.sh
in the past successfully– Jay Dorsey
Jan 3 at 19:18
@AndyHarvey I think most of those messages are normal, but the database does not, followed by does exist kind of jumps out. You'll need to figure out a strategy for creating that table if it doesn't exist. This will be dependent upon a few things (does your database container persist across up/down for one). That will help you decide whether you want to run the db:create manually or as part of your compose. Regardless, this might be worth a look: docs.docker.com/compose/startup-order I've used
wait_for_it.sh
in the past successfully– Jay Dorsey
Jan 3 at 19:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015204%2fwhy-is-dockerized-rails-app-raising-pgconnectionbad%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)
– David Maze
Jan 3 at 1:53
1
From the point of view of your application, the database will be visible at the host name
postgres
(the name of the block in thedocker-compose.yml
file). You can remove all of thecontainer_name:
andlinks:
sections, they don't really have a practical effect.– David Maze
Jan 3 at 1:55
Thanks @DavidMaze, a couple questions: 1) my database.yml contains
host: postgres
but I am still seeing this error. Is there something else I should change. 2) I had thought thatlinks:
ensures that e.g. the postgres services is available before any attempt is made to start the web service. Have I misunderstood this? Thanks!– Andy Harvey
Jan 3 at 2:24
1
links
is now considered a legacy/potentially deprecated feature. Since you're using the latest v3 syntax you might want to look at depends on– Jay Dorsey
Jan 3 at 2:43
1
...and
depends_on:
is a fairly weak ordering constraint (if youdocker-compose up web
thendepends_on:
will cause it to startpostgres
too, but "start" doesn't guarantee it'll be done with first-time setup and be listening for connections).– David Maze
Jan 3 at 4:49