Part 4: Docker with rails, postgres(database which is running inside the docker)
Published in
1 min readFeb 6, 2018
Please follow the basic steps from here and i’m listing here only the changes for running postgres service inside the docker, still it store the data in local disk.
database.yml
default: &default
adapter: postgresql
encoding: utf8
pool: 5
username: name
password: password
host: db
development:
<<: *default
database: project_db1
test:
<<: *default
database: docc
production:
<<: *default
database: docc
Different from the previous file is host: db
Dockerfile (No change in this file)
FROM ruby:2.4.0
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY . /myapp
RUN bundle install
docker-compose.yml
version: '3'
services:
db:
image: postgres
ports:
- "5432"
volumes:
- /usr/data/volumes/pgdata/etc:/etc/postgresql
- /usr/data/volumes/pgdata/log:/var/log/postgresql
- /usr/data/volumes/pgdata/data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
changes here
ports:
- "5432"
volumes:
- /usr/data/volumes/pgdata/etc:/etc/postgresql
- /usr/data/volumes/pgdata/log:/var/log/postgresql
- /usr/data/volumes/pgdata/data:/var/lib/postgresql/data
which will copy the content from docker location to local location.
docker location : /etc/postgresql
local location : /usr/data/volumes/pgdata/etc
Once all set.. try
$ sudo docker-compose run web bundle exec rake db:create
$ sudo docker-compose run web bundle exec rake db:migrate
& sudo docker-compose up
Sample rails repo found at github
Previous : Part 3: Docker with rails, postgres