Skip to content

Instantly share code, notes, and snippets.

@nickstanish
Created October 2, 2018 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickstanish/0f48b383d4f681383cc55240b9a17fe1 to your computer and use it in GitHub Desktop.
Save nickstanish/0f48b383d4f681383cc55240b9a17fe1 to your computer and use it in GitHub Desktop.
Docker Compose + Rails + MySQL

README

Environment

Download and install Docker community edition

Setup

Generate a rails app

$ rails new

Update the Gemfile to use gem 'mysql2' instead of sqlite

Copy the files from here to your project and update project_name to whatever you'd like.

Start the containers:

$ docker-compose up --build

Create the database:

$ docker-compose run --rm web rake db:create

Visit the website at http://localhost:3000

Start a console within the docker environment:

$ docker-compose run --rm --service-ports web bash

It's also helpful to use kitematic so you can easily see which containers are running, see logs, and stop/remove them.

default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
reconnect: false
timeout: 5000
username: root
password: 'root'
host: db
port: 3306
encoding: utf8mb4
collation: utf8mb4_unicode_ci
development:
<<: *default
database: project_name_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: project_name_test
production:
<<: *default
database: project_name
version: '3'
services:
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./tmp/db:/var/lib/mysql
networks:
- project_name-net
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/home/web
ports:
- "3000:3000"
depends_on:
- db
networks:
- project_name-net
networks:
project_name-net:
driver: bridge
FROM ruby:2.5.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /project_name
WORKDIR /project_name
COPY Gemfile /project_name/Gemfile
COPY Gemfile.lock /project_name/Gemfile.lock
RUN bundle install
COPY . /project_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment