I am using rails 3.0.0 with rails 7.
My dockerfile is as:
FROM ruby:3.0.0-alpine
RUN apk add --update --virtual \
runtime-deps \
postgresql-client\
build-base \
libxml2-dev \
libxslt-dev \
yarn \
libffi-dev \
readline \
build-base \
postgresql-dev \
libc-dev \
linux-headers \
readline-dev \
file \
imagemagick \
git \
tzdata \
&& rm -rf /var/cache/apk*
WORKDIR /app
COPY . /app/
ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install
ENTRYPOINT ["bin/rails"]
CMD ["s", "-b", "0.0.0.0"]
EXPOSE 3000
docker-compose.yml is as:
version: '3.8'
services:
db:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=password
ports:
- "5433:5432"
volumes:
- "dbdata:/var/lib/postgresql/data"
redis:
image: redis:latest
ports:
- "6380:6379"
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
- redis
environment:
- DATABASE_URL=postgres://postgres:password@db:5433/postgres
- REDIS_URL=redis://redis:6380
volumes:
- .:/app
volumes:
dbdata:
database.yml file is as:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 5432
development:
<<: *default
database: my_app_development
username: postgres
password: password
test:
<<: *default
database: my_app_test
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>
using ubunut 20.04 LTS. getting error as: ActiveRecord::ConnectionNotEstablished could not connect to server: Connection refused Is the server running on host "172.22.0.3" and accepting TCP/IP connections on port 5433?
How to resolve this issue, any suggestion would help, thanks in advance.
CodePudding user response:
Your web
service is connecting with the db
(postgresql) service using the internal docker network; when doing so, it connects directly to the IP address of the container, which (for postgresql) is listening on port 5432
. Connections over the docker network does not require ports to be published / exposed, so the 5433:5432
port-mapping is not used for that.
I should add that your compose file is publishing (port-mapping) both the db
and redis
services; publishing them means that they'll be publicly accessible on the host's network interface (which, if you're not on an internal network, may be publicly accessible on the internet).
Make sure to only publish ports for services that must be publicly accessible (or are protected through other ways); as described above, connections between containers / services does not require those ports to be published.
CodePudding user response:
reached to solution from above shared suggestions:
docker-compose.yml
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: password
command: -p 5000
ports: "5000:5432"
volumes:
- "dbdata:/var/lib/postgresql/data"
redis:
image: redis:latest
ports:
- "6380:6379"
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
- redis
environment:
- DATABASE_URL=postgres://postgres:password@db:5432/postgres
- REDIS_URL=redis://redis:6379
volumes:
- .:/app
volumes:
dbdata:
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: my_app_development
username: postgres
password: password
test:
<<: *default
database: my_app_test
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>
Dockerfile:
FROM ruby:3.0.0-alpine
RUN apk add --update --virtual \
runtime-deps \
postgresql-client\
build-base \
libxml2-dev \
libxslt-dev \
yarn \
libffi-dev \
readline \
build-base \
postgresql-dev \
libc-dev \
linux-headers \
readline-dev \
file \
imagemagick \
git \
tzdata \
&& rm -rf /var/cache/apk*
WORKDIR /app
COPY . /app/
ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install
ENTRYPOINT ["bin/rails"]
CMD ["s", "-b", "0.0.0.0"]
EXPOSE 3000
Now with these configurations rails app is running. please update how to do it better, thanks.