I'm trying to dockerize my Ruby on Rails API, the output of the command "docker-compose ps" is this:
Since the containers are up, I tried to migrate my database use this command "docker-compose exec app bundle exec rails db:setup db:migrate", the output I got is this:
About my config file, this is my Dockerfile:
FROM ruby:2.7.0-alpine
ENV BUNDLER_VERSION=2.1.2
RUN apk add --update --no-cache \
binutils-gold \
build-base \
curl \
file \
g \
gcc \
git \
less \
libstdc \
libffi-dev \
libc-dev \
linux-headers \
libxml2-dev \
libxslt-dev \
libgcrypt-dev \
make \
netcat-openbsd \
nodejs \
openssl \
pkgconfig \
postgresql-dev \
python \
tzdata \
yarn
RUN gem install bundler -v 2.1.2
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install
#COPY package.json yarn.lock ./
RUN yarn install --check-files
COPY . ./
ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]
And this is my docker-compose.yml:
version: '3.4'
services:
app:
build:
context: .
dockerfile: Dockerfile
depends_on:
- database
ports:
- "3000:3000"
volumes:
- .:/app
- gem_cache:/usr/local/bundle/gems
- node_modules:/app/node_modules
env_file: .env
environment:
RAILS_ENV: production
networks:
- letsorder
database:
image: postgres:13.7
env_file:
- ./.env
volumes:
- db_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- letsorder
volumes:
gem_cache:
db_data:
node_modules:
networks:
letsorder:
driver: bridge
So, I searched this issue, most of them lead to set-up a network adapter in my docker-compose.yml, I did it but I got the same error.
I would like to request your suggestions and comments how to debug and fix this issue; thanks a lot.
CodePudding user response:
It looks like in your database.yml
file you have something like
host: rorletsorderdatabase
But in your docker-compose.yml
file you have
database:
foo
A possible fix is changing the host inside your database.yml
file
host: database
Another option is changing the service name inside your docker-compose.yml
file
rorletsorderdatabase:
image: postgres:13.7
env_file:
- ./.env
volumes:
- db_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- letsorder
Also in any of the cases, you need to expose the pg port 5432
in the service definition inside docker-compose.yml
ports:
- "5432:5432"