Skip to content

Instantly share code, notes, and snippets.

@Yoshyn
Last active September 2, 2021 12:37
Show Gist options
  • Save Yoshyn/7e260bbf69a6f6fc5a264c2417f05785 to your computer and use it in GitHub Desktop.
Save Yoshyn/7e260bbf69a6f6fc5a264c2417f05785 to your computer and use it in GitHub Desktop.
Rails last version & docker-compose setup easy
Rails last version & docker-compose setup easy
RAILS_ENV=development
version: "3.8"
services:
# If you need a posgres database
postgresql:
image: "postgres:latest"
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: pass1234
ports:
- 5432:5432
container_name: postgresql
hostname: postgresql
labels:
- postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
# Else setup a mariabd, elasticsearch or whatever you need here
web:
build:
context: "."
dockerfile: "Dockerfile"
image: "my_app/web:local"
hostname: my_app
ports:
- "3000:3000"
command: "bash /usr/src/app/docker-start.sh" # "tail -f /dev/null" in case of the cmd does not work, this can be usefull to get a container that stay alive and connect into it with a shell
volumes:
- node-modules:/usr/src/app/node_modules
- ./:/usr/src/app:cached
env_file: .env.development
environment:
PGHOST: "postgresql"
PGUSER: "postgres"
PGPASSWORD: "pass1234"
volumes:
node-modules:
#!/bin/sh
set -e
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid
fi
yarn install --check-files
bundle exec rails s -b 0.0.0.0 -p 3000
FROM ruby:3-alpine
ENV APP_PATH /usr/src/app
ENV RAILS_PORT 3000
ENV BUNDLER_VERSION=2.2.14
ENV BUNDLE_GEMFILE=$APP_PATH/Gemfile
ENV BUNDLE_JOBS=2
RUN apk add --update-cache \
binutils-gold \
build-base \
g++ \
gcc \
git \
less \
libstdc++ \
libffi-dev \
libc-dev \
linux-headers \
libxml2-dev \
libxslt-dev \
libgcrypt-dev \
tzdata \
openssl \
wget \
curl \
ca-certificates \
postgresql-client \
sqlite sqlite-libs sqlite-dev \
nodejs yarn \
bash \
&& rm -rf /var/cache/apk/* \
&& mkdir -p $APP_PATH
ADD Gemfile* $APP_PATH/
RUN gem install bundler -v "$BUNDLER_VERSION"
RUN bundle install
EXPOSE $RAILS_PORT
WORKDIR $APP_PATH
ENTRYPOINT [ "bundle", "exec" ]

Create the rails app :

Do not install anything inside your laptop :

docker run -ti -v $(pwd):/usr/src/app --entrypoint /bin/bash --workdir=/usr/src/app ruby:latest

Make dummy install inside a temporary container :

apt-get update && apt-get install -y nodejs npm git build-essential
npm install --global yarn
gem install rails
rails new my_app

Exit the container.

Set up dockerized files :

Copy/Paste Dockerfile, .env.development, docker-compose.yml inside the root folder of the project (my_app)

Usage :

Then launch the database (postgres in example here). Setup what you need (mariadb?, sqlite? in docker-compose) :

docker-compose -f docker-compose.yml up --renew-anon-volumes --remove-orphans --force-recreate -d postgresql

Options :

    --renew-anon-volumes       Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    -d, --detach               Detached mode: Run containers in the background,
                               print new container names.

Launch the web application :

docker-compose -f docker-compose.yml up --renew-anon-volumes --remove-orphans --force-recreate --build web

Options :

    --build                    Build images before starting containers.

Once it's launched the first time, you'll need to run migration & seeds :

docker-compose exec web bundle exec rake db:setup docker-compose exec web bundle exec rake db:migrate docker-compose exec web bundle exec rake db:seed

If you need to check logs of a demonized container :

docker-compose -f docker-compose.yml logs -f postgres

If you need to get a terminal inside a container :

docker-compose exec -ti web /bin/bash

You can access to the app with your browser at : localhost:3000

Cleanup :

docker-compose -f docker-compose.yml down

Kill every docker container running :

docker kill $(docker ps -q)

In case of conflict with an existing container :

Identify the container and manage it : docker ps -a

Or dummy remove everything existing :

docker rm -f $(docker ps -a -q)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment