Skip to content

Instantly share code, notes, and snippets.

@masuidrive
Last active November 15, 2022 06:02
Show Gist options
  • Save masuidrive/3f8707336ffb286ad212 to your computer and use it in GitHub Desktop.
Save masuidrive/3f8707336ffb286ad212 to your computer and use it in GitHub Desktop.
Docker ComposeでRails環境作成 mkdir -p railsapp && cd railsapp && curl -L http://bit.ly/rails6-docker | sh
#!/usr/bin/env bash
rails_version="${RAILS_VERSION:-7}"
ruby_version="${RUBY_VERSION:-3.0}"
node_version="${NODE_VERSION:-16}"
docker run --rm -v `pwd`:/usr/src/app ruby:$ruby_version sh -c "gem install rails --version $rails_version && rails new /usr/src/app --database mysql ${RAILS_OPTIONS} --git --force --version $rails_version"
cat << __EOT__ > Dockerfile
FROM ruby:$ruby_version
ENV LANG C.UTF-8
RUN apt-get update && \\
curl -sL https://deb.nodesource.com/setup_$node_version.x | bash - && \\
apt-get install -y nodejs gcc g++ make mariadb-client --no-install-recommends && \\
npm install --global yarn && \\
rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock /usr/src/app/
RUN bundle install
# COPY package.json yarn.lock /usr/src/app/
# RUN yarn install
COPY . /usr/src/app
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
__EOT__
cat << __EOT__ > docker-compose.yml
version: "3"
services:
rails:
build: .
command:
["bash", "-c", "rm -f tmp/pids/server.pid; ./bin/rails server -b 0.0.0.0"]
mem_limit: 384m
environment:
DISABLE_SPRING: "1"
ports:
- "3000:3000"
volumes:
- ".:/usr/src/app"
- "/usr/src/app/node_modules"
links:
- "mysql"
mysql:
image: mysql/mysql-server:8.0.20
environment:
MYSQL_ROOT_PASSWORD: "passwd"
MYSQL_ROOT_HOST: '%' # https://zenn.dev/ryo_kawamata/articles/mysql-connect-error
__EOT__
sed -i "" -e 's/password:$/password: "passwd"/;s/localhost/mysql/' config/database.yml
sed -i "" -e "s|^end| config.web_console.whitelisted_ips = %w( 0.0.0.0/0 ::/0 )"\\$'\n'"end|" config/environments/development.rb
touch Gemfile.lock
echo ">>>> Build docker image"
docker-compose build
echo ">>>> Create DBs"
docker-compose run --rm rails rails db:create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment