Dockerfile
FROM ruby:2.6.3
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /paper_scammer_docker
WORKDIR /paper_scammer_docker
COPY Gemfile /paper_scammer_docker/Gemfile
COPY Gemfile.lock /paper_scammer_docker/Gemfile.lock
RUN bundle install
COPY . /paper_scammer_docker
# 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
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: '3'
services:
db:
image: postgres:10
ports:
- "5432:5432"
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/paper_scammer_docker
ports:
- "3000:3000"
links:
- "db:db"
depends_on:
- db
I am successfully able to build image using the following command
docker-compose build
If i do docker-compose up
in project root directory my app runs fine and i can see two containers one for my rails app and other for postgres when i do docker container ls
. I observed that postgres image is loaded from library.
database.yml file
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
host: db
username: postgres
password:
# user: gnpsllco_papaer
# password: sharma@123
development:
<<: *default
database: paper_scammer_development
# user: postgres
# password: sharma@123
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: paper_scammer_test
# user: postgres
# password: sharma@123
production:
<<: *default
database: gnpsllco_paper_scammer
Problem
I run the following command to start docker container from the image of my rails app.
sudo docker run -p 3000:3000 paperscammer_web
postgres container is not running or missing.
I want to understand how this dockerfile-compose handle this, when i spin the image of rails app which says its depends in postgres service shouldn't it also spin a postgres container and set 'db' as host for connections.
or do it needs to be spinned manually for postgres.
I get the following error when i run http://localhost:3000
could not translate host name "db" to address: Name or service not known
I can see postgres container is not running.
Thanks