Last active
September 15, 2024 00:17
-
-
Save bpohoriletz/9ba8c5a8eb92727ec24dccfe269f5ea8 to your computer and use it in GitHub Desktop.
Rails 7.0.1/Ruby 3.1/SQLite app in Docker
This file contains hidden or 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
# set ruby/rails version | |
export DOCKER_RAILS_VERSION="7.0.1" | |
export DOCKER_RUBY_VERSION="3.1.0" | |
export RAILS_PROJECT_NAME="rails7" | |
# create folder for the project and add Gemfile with necessary rails version | |
mkdir "$RAILS_PROJECT_NAME" | |
cd "$RAILS_PROJECT_NAME" | |
echo "ruby '$DOCKER_RUBY_VERSION' | |
source 'https://rubygems.org' | |
gem 'rails', '$DOCKER_RAILS_VERSION'" > Gemfile | |
# create folder for Docker configuration | |
mkdir .devcontainer | |
cd .devcontainer | |
# create Dockerfile | |
echo "FROM ruby:$DOCKER_RUBY_VERSION-slim | |
# install required dependencies | |
RUN apt-get update \ | |
&& apt-get install -y make gcc git sqlite3 libsqlite3-dev \ | |
&& rm -rf /var/lib/apt/lists/*" > Dockerfile | |
# create configuration for docker-compose | |
echo "services: | |
# name of the service | |
web: | |
image: ruby-$DOCKER_RUBY_VERSION-rails-$DOCKER_RAILS_VERSION | |
build: | |
context: . | |
# use Dockerfile for build | |
dockerfile: Dockerfile | |
volumes: | |
# mount parent directory as /web with cahed configuration for speedup | |
- "$(pwd)/..":/web:cached | |
# set mounted directory as default | |
working_dir: /web | |
command: sleep infinity | |
environment: | |
# install gems to the folder with project files | |
- BUNDLE_PATH=vendor/bundle | |
ports: | |
# expose port 3000 to the outer world | |
- '3000:3000'" > docker-compose.yml | |
# build container | |
docker-compose up -d --build | |
# install rails | |
docker-compose exec web bundle | |
# generate new rails project | |
docker-compose exec web bundle exec rails new . -f | |
# start rails server at IP 0.0.0.0 so that it's available on the host macOS | |
docker-compose exec web bundle exec rails s -b 0.0.0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks