docker-compose Rails and Postgresql
- Dockerfile
FROM ruby:2.6.3 RUN apt-get update -qq && apt-get install -y nodejs postgresql-client RUN mkdir /myapp WORKDIR /myapp COPY . /myapp RUN gem update --system RUN bundle install RUN yarn install --check-files # 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", "-e", "production", "-b", "0.0.0.0"] # Production # CMD ["rails", "server", "-b", "0.0.0.0"]
- entrypoint.sh
#!/bin/bash set -e # Remove a potentially pre-existing server.pid for Rails. rm -f /myapp/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@"
- docker-compose.yml
version: '3' services: db: image: postgres 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: - .:/myapp ports: - "3000:3000" depends_on: - db
- database.yml
default: &default adapter: postgresql encoding: unicode host: db username: postgres password: pool: 5 development: <<: *default database: myapp_development test: <<: *default database: myapp_test
- build & run
docker-compose build docker-compose up docker-compose run web rake db:create docker-compose run web rake db:migrate #or docker-compose run web rake db:create db:migrate
$ docker-compose up Creating network "blog_default" with the default driver Creating blog_db_1 ... done Creating blog_web_1 ... done Attaching to blog_db_1, blog_web_1 db_1 | 2019-06-28 03:09:42.063 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 db_1 | 2019-06-28 03:09:42.063 UTC [1] LOG: listening on IPv6 address "::", port 5432 db_1 | 2019-06-28 03:09:42.113 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" db_1 | 2019-06-28 03:09:42.210 UTC [24] LOG: database system was shut down at 2019-06-28 03:09:21 UTC db_1 | 2019-06-28 03:09:42.242 UTC [1] LOG: database system is ready to accept connections web_1 | WARNING: Use strings for Figaro configuration. 305150117040730 was converted to "305150117040730". web_1 | => Booting Puma web_1 | => Rails 6.0.0.rc1 application starting in development web_1 | => Run `rails server --help` for more startup options web_1 | Puma starting in single mode... web_1 | * Version 3.12.1 (ruby 2.6.1-p33), codename: Llamas in Pajamas web_1 | * Min threads: 5, max threads: 5 web_1 | * Environment: development web_1 | * Listening on tcp://0.0.0.0:3000 web_1 | Use Ctrl-C to stop
$ docker-compose down Stopping blog_db_1 ... done Removing blog_web_run_1 ... done Removing blog_db_1 ... done Removing network blog_default
- docker-compose test
docker-compose run -e "RAILS_ENV=test" app rake test