If you want to use docker for rails app, and use database, you need to know that all the data in the docker container will be destroyed (unless you configure the container specifically for keeping data) Sometimes, you need to create a docker container with an application and attach it to an old container with a database.
As an example of rails application, I used a simple app. You can create it from command:
rails new compose-app --database=postgresql
Of course, you need to install rails, ruby, etc. beforehand.
Then, create Dockerfile in your project, and set this data to it:
FROM ruby:2.3.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /compose-app
WORKDIR /compose-app
ADD Gemfile /compose-app/Gemfile
ADD Gemfile.lock /compose-app/Gemfile.lock
RUN bundle install
ADD . /compose-app
Next step - create docker-compose.yml with the data:
version: '2'
services:
db:
image: postgres
web:
build: .
command: bundle exec rails s -e development -p 80 -b '0.0.0.0'
volumes:
- .:/compose-app
ports:
- "80:80"
depends_on:
- db
You can replace 80 port (-p 80 ) with another.
Develop section of database.yml config must be changed to:
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
host: db
Now you can build images from command:
docker-compose build
(Run this in project directory)
And start all from:
docker-compose up
If everything is done correctly, you will be able to see logs from rails in the console.
Close console. It will be working.
If you want to delete only the container with the rails application without the database, you need to run then in project directory:
docker-compose stop web
docker-compose build web
docker-compose up -d --no-deps web
New container with rails app will be created and launched.