Last active
December 17, 2019 23:09
-
-
Save akiyoshi83/16cda69ad3f55a6173fb66862863a8a3 to your computer and use it in GitHub Desktop.
Initialize rails dev env using docker-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE | |
# ------ | |
# | |
# Please ensure there is that global_bundle volume before execute this script. | |
# | |
# ``` | |
# docker volume create global_bundle --driver local | |
# bash rails-docker-compose-init.sh | |
# docker-compose run web rails new --database postgresql --skip-bundle --force . | |
# # edit your Gemfile and config/database.yml (host, username, password) | |
# docker-compose run web bundle install | |
# docker-compose run web rake db:create | |
# docker-compose run web rails webpacker:install | |
# docker-compose up -d | |
# ``` | |
___APPNAME___=$(basename $(pwd)) | |
___RUBY_VER___=2.6.5 | |
___RAILS_VER___=6.0.0 | |
___PSQL_VER___=12.0 | |
docker volume inspect global_bundle > /dev/null | |
if [ $? -ne 0 ]; then | |
echo 'This script needs global_bundle volume.' | |
echo 'Please execute below.' | |
echo 'docker volume create global_bundle --driver local' | |
exit 1 | |
fi | |
cat <<EOS > Gemfile | |
source 'https://rubygems.org' | |
ruby '${___RUBY_VER___}' | |
gem 'rails', '~> ${___RAILS_VER___}' | |
EOS | |
touch Gemfile.lock | |
cat <<EOS > Dockerfile | |
FROM ruby:${___RUBY_VER___} | |
RUN apt-get update -qq && \\ | |
apt-get install -y \\ | |
build-essential \\ | |
postgresql-client \\ | |
nodejs \\ | |
npm | |
RUN npm install -g yarn | |
ENV APPROOT /${___APPNAME___} | |
RUN mkdir -p \${APPROOT} | |
WORKDIR \${APPROOT} | |
COPY Gemfile \${APPROOT}/ | |
COPY Gemfile.lock \${APPROOT}/ | |
RUN bundle install | |
COPY . \${APPROOT}/ | |
COPY entrypoint.sh /usr/bin/ | |
RUN chmod +x /usr/bin/entrypoint.sh | |
ENTRYPOINT ["entrypoint.sh"] | |
EXPOSE 3000 | |
CMD ["rails", "server", "-b", "0.0.0.0"] | |
EOS | |
cat <<EOS > docker-compose.yaml | |
version: '3' | |
services: | |
db: | |
image: postgres:${___PSQL_VER___} | |
ports: | |
- 5432:5432 | |
volumes: | |
- db:/var/lib/postgresql/data | |
- ./postgres/init:/docker-entrypoint-initdb.d | |
environment: | |
POSTGRES_USER: ${___APPNAME___} | |
POSTGRES_PASSWORD: password | |
POSTGRES_INITDB_ARGS: "--encoding=UTF-8" | |
hostname: postgres | |
restart: always | |
user: root | |
web: | |
build: . | |
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" | |
volumes: | |
- .:/${___APPNAME___} | |
- global_bundle:/usr/local/bundle | |
- /${___APPNAME___}/log | |
- /${___APPNAME___}/tmp | |
ports: | |
- "3000:3000" | |
depends_on: | |
- db | |
volumes: | |
db: | |
driver: local | |
global_bundle: | |
external: true | |
EOS | |
cat <<EOS > entrypoint.sh | |
#!/bin/bash | |
set -e | |
# Remove a potentially pre-existing server.pid for Rails. | |
rm -f /${___APPNAME___}/tmp/pids/server.pid | |
# Then exec the container's main process (what's set as CMD in the Dockerfile). | |
exec "\$@" | |
EOS | |
mkdir -p postgres/init | |
cat <<EOS > postgres/init/init-development.sql | |
CREATE USER ${___APPNAME___} PASSWORD 'password'; | |
CREATE DATABASE ${___APPNAME___}_development; | |
CREATE DATABASE ${___APPNAME___}_test; | |
GRANT ALL PRIVILEGES ON DATABASE ${___APPNAME___}* TO ${___APPNAME___}; | |
EOS | |
echo 'Please run following:' | |
echo 'docker-compose run web rails new --database postgresql --skip-bundle --force .' | |
echo '# edit your Gemfile and config/database.yml (host, username, password)' | |
echo 'docker-compose run web bundle install' | |
echo 'docker-compose run web rake db:create' | |
echo 'docker-compose run web rails webpacker:install' | |
echo 'docker-compose up -d' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment