Last active
June 16, 2019 21:14
-
-
Save pedryvo/d26dec43083ffbef424db8fcf7858864 to your computer and use it in GitHub Desktop.
Snippets to Dockerize Rails App
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
mkdir nameOfProject | |
cd nameOfProject | |
docker run --rm --user "$(id -u):$(id -g)" -v $(pwd):/usr/src -w /usr/src -ti ruby:latest bash | |
gem install rails | |
rails new . --database=postgresql --skip-bundle | |
--------------------- Dockerfile ---------------------- | |
FROM ruby:latest | |
# add nodejs and yarn dependencies for the frontend | |
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - && \ | |
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ | |
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list | |
# Instala nossas dependencias | |
RUN apt-get update && apt-get install -qq -y --no-install-recommends \ | |
nodejs yarn build-essential libpq-dev imagemagick git-all nano | |
# Seta nosso path | |
ENV INSTALL_PATH /myapp | |
# Cria nosso diretório | |
RUN mkdir -p $INSTALL_PATH | |
# Seta o nosso path como o diretório principal | |
WORKDIR $INSTALL_PATH | |
# Copia o nosso Gemfile para dentro do container | |
COPY Gemfile ./ | |
# Seta o path para as Gems | |
ENV BUNDLE_PATH /gems | |
# Copia nosso código para dentro do container | |
COPY . . | |
----------------- Compose ---------------------- | |
version: "3" | |
services: | |
postgres: | |
image: "postgres:alpine" | |
volumes: | |
- postgres:/var/lib/postgresql/data | |
app: | |
depends_on: | |
- "postgres" | |
build: . | |
command: bash start.sh | |
ports: | |
- "3000:3000" | |
volumes: | |
- .:/myapp | |
- gems:/gems | |
volumes: | |
postgres: | |
gems: | |
-------------------- start.sh --------------------------- | |
# Instala as Gems | |
bundle check || bundle install | |
# Roda nosso servidor | |
bundle exec puma -C config/puma.rb | |
------------------ database.yml ------------------------- | |
default: &default | |
adapter: postgresql | |
encoding: unicode | |
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | |
host: postgres | |
user: postgres | |
development: | |
<<: *default | |
database: myappdb_development | |
test: | |
<<: *default | |
database: myappdb_test | |
production: | |
<<: *default | |
database: myappdb_production | |
username: myappdb | |
password: <%= ENV['EXCHANGE_DATABASE_PASSWORD'] %> | |
---------------- run migration ---------------- | |
docker-compose run --rm app bundle exec rails db:create db:migrate | |
. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment