├── .gitignore ├── Dockerfile ├── MIT-LICENCE ├── README.md ├── bundle └── .gitkeep ├── data └── redis │ └── .gitkeep ├── docker-compose.yml ├── docker-sync.yml ├── my-rails-app └── .gitkeep └── scripts └── start-dev.sh /.gitignore: -------------------------------------------------------------------------------- 1 | my-rails-app/* 2 | !my-rails-app/.gitkeep 3 | data/sql/* 4 | data/redis/* 5 | !data/redis/.gitkeep 6 | bundle/* 7 | !bundle/.gitkeep 8 | .DS_Store 9 | .env 10 | *.log 11 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ruby:2.4.1 2 | RUN apt-get update && apt-get install -y \ 3 | build-essential \ 4 | wget \ 5 | git-core \ 6 | libxml2 \ 7 | libxml2-dev \ 8 | libxslt1-dev \ 9 | nodejs \ 10 | imagemagick \ 11 | libmagickcore-dev \ 12 | libmagickwand-dev \ 13 | libpq-dev \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN mkdir /app 17 | RUN mkdir -p /root/.ssh/ 18 | 19 | WORKDIR /app 20 | 21 | RUN ssh-keyscan -H github.com >> ~/.ssh/known_hosts 22 | 23 | ENV GEM_HOME /bundle 24 | ENV PATH $GEM_HOME/bin:$PATH 25 | ENV BUNDLE_PATH /bundle 26 | ENV BUNDLE_BIN /bundle/bin 27 | 28 | RUN gem install bundler -v '1.10.6' \ 29 | && bundle config --global path "$GEM_HOME" \ 30 | && bundle config --global bin "$GEM_HOME/bin" 31 | -------------------------------------------------------------------------------- /MIT-LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Guillaume Montard 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | UPDATE: You can now use the [DockRails](https://github.com/gmontard/dockrails/) Gem to build your Docker Rails Enironment with a simple CLI 3 | --- 4 | 5 | ### About 6 | 7 | This is a boilerplate Docker environment for you Rails App letting you **code on your Mac while running in Docker**, it includes by default: 8 | - **DB container** (PGSQL or MYSQL *uncomment in docker-compose.yml*) 9 | - **Redis container** 10 | - **Web container** (your rails app) 11 | - **Job Container** (Sidekiq instance) 12 | 13 | Web/Job containers **sync code base from your Rails App in realtime**, letting you code on you Mac and run in the same time everything from the container 14 | 15 | Bundler Gems, DB and Redis data are **persisted accross restart** and you can use **ByeBug or Pry out of the box** with a simple command! 16 | 17 | ### Pre-requisite 18 | 19 | First install: 20 | - [Docker Toolbox](https://www.docker.com/products/docker-toolbox) 21 | - ```gem install docker-sync``` 22 | - ```brew install unison``` 23 | - ```git clone git@github.com:gmontard/rails-docker-dev-box.git``` 24 | - ```cd rails-dev-box && mkdir data/sql``` 25 | 26 | 27 | ### Steps 28 | 29 | 1. Clone you Rails app inside the "my-rails-app" folder (*If you rename it make sure to change the name in the docker-sync file*) 30 | 31 | 2. Move the ```scripts/``` folder into your Rails App root folder 32 | 33 | 3. Create a ```data/sql``` folder *(info: adding a .gitkeep inside will make the PG container fail)* 34 | 35 | 3. Check the *docker-compose.yml* ENV variables for Redis/SQL and setup your *database.yml* in your Rails App file. 36 | 37 | 4. Run everything 38 | 39 | ``` 40 | docker-sync-stack start 41 | ``` 42 | 43 | ### Important Commands 44 | 45 | Start all containers: 46 | - ```docker-sync-stack start``` 47 | 48 | Stop all containers: 49 | - ```docker-sync-stack clear``` 50 | 51 | Check all containers are stopped 52 | - ```docker ps``` 53 | 54 | Run command inside a container 55 | - ```docker-compose run CONTAINER COMMAND``` (ex: docker-compose run web rake db:migrate) 56 | 57 | Rebuild a container in case the DockerFile change: 58 | - ```docker-compose build CONTAINER``` 59 | 60 | Need to debug a container? 61 | - ```docker-compose run CONTAINER /bin/bash``` 62 | 63 | Need to access ByeBug or Pry interactve shell? 64 | - ```docker attach $(docker-compose ps -q web)``` :warning: DO NOT use ```CTRL+C``` here or it will exit the container but use ```CTRL+Q+P``` 65 | 66 | 67 | ### Type less with ZSH Alias! 68 | 69 | ```vi ~/.zshrc``` 70 | 71 | ``` 72 | # Add those lines 73 | alias dss="docker-sync-stack" 74 | alias dbe="docker-compose run web bundle exec" 75 | alias dbi="docker-compose run web bundle install" 76 | function ddbug(){ 77 | docker attach $(docker-compose ps -q $1) 78 | } 79 | ``` 80 | 81 | Reload ZSH 82 | ``` 83 | source ~/.zshrc 84 | ``` 85 | 86 | And now enjoy the simple commands like: 87 | - ```dss start``` 88 | - ```dbe rails c``` 89 | - ```dbe rails restart``` 90 | - ```dbi``` 91 | - ```ddbug web``` 92 | 93 | And obviously create your own! 94 | 95 | ### Miscs 96 | 97 | - Your DB is accessible from your host machine with the 127.0.0.1 IP and using the user/password combination found in *docker-compose.yml* file. 98 | 99 | 100 | ---- 101 | Thanks to Jesal Gadhis, this project was inspired by his [tutorial](https://jes.al/2016/09/setting-up-a-rails-development-environment-using-docker/) 102 | -------------------------------------------------------------------------------- /bundle/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmontard/rails-docker-dev-box/ba37ea5a4ac69f5eefa674d1e60a639e94dadd54/bundle/.gitkeep -------------------------------------------------------------------------------- /data/redis/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmontard/rails-docker-dev-box/ba37ea5a4ac69f5eefa674d1e60a639e94dadd54/data/redis/.gitkeep -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | # db: 4 | # image: mysql 5 | # volumes: 6 | # - ./data/sql:/var/lib/mysql 7 | # ports: 8 | # - "3306:3306" 9 | # environment: 10 | # MYSQL_DATABASE: rails_development 11 | # MYSQL_USER: rails 12 | # MYSQL_PASSWORD: rails 13 | # MYSQL_ROOT_PASSWORD: root 14 | db: 15 | image: postgres 16 | volumes: 17 | - ./data/sql:/var/lib/postgresql/data 18 | ports: 19 | - "5432:5432" 20 | environment: 21 | POSTGRES_DB: rails_development 22 | POSTGRES_USER: rails 23 | POSTGRES_PASSWORD: rails 24 | 25 | redis: 26 | image: redis 27 | ports: 28 | - "6379:6379" 29 | volumes: 30 | - ./data/redis:/data 31 | 32 | web: 33 | build: . 34 | command: sh scripts/start-dev.sh 35 | volumes: 36 | - myapp-web-sync:/app:rw 37 | - myapp-bundle-sync:/bundle:rw 38 | - ./keys:/root/.ssh/ 39 | ports: 40 | - "3000:3000" 41 | environment: 42 | REDIS_URL: redis://redis:6379 43 | MYSQL_USER: rails 44 | MYSQL_PASSWORD: rails 45 | links: 46 | - db 47 | - redis 48 | tty: true 49 | stdin_open: true 50 | 51 | job: 52 | build: . 53 | command: bundle exec sidekiq -C config/sidekiq.yml 54 | volumes: 55 | - myapp-web-sync:/app:rw 56 | - myapp-bundle-sync:/bundle:rw 57 | - ./keys:/root/.ssh/ 58 | environment: 59 | REDIS_URL: redis://redis:6379 60 | links: 61 | - db 62 | - redis 63 | 64 | volumes: 65 | myapp-web-sync: 66 | external: true 67 | myapp-bundle-sync: 68 | external: true 69 | -------------------------------------------------------------------------------- /docker-sync.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | syncs: 3 | myapp-web-sync: 4 | src: './my-rails-app' 5 | dest: '/app' 6 | myapp-bundle-sync: 7 | src: './bundle' 8 | dest: '/bundle' 9 | -------------------------------------------------------------------------------- /my-rails-app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmontard/rails-docker-dev-box/ba37ea5a4ac69f5eefa674d1e60a639e94dadd54/my-rails-app/.gitkeep -------------------------------------------------------------------------------- /scripts/start-dev.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | bundle check || bundle install 3 | rm -rf tmp/pids/* && bundle exec rails server --port 3000 --binding 0.0.0.0 4 | --------------------------------------------------------------------------------