Skip to content

Instantly share code, notes, and snippets.

@dtelaroli
Last active June 12, 2018 12:25
Show Gist options
  • Save dtelaroli/db2abc7dcb1b1c68c2bd3706ccfe751a to your computer and use it in GitHub Desktop.
Save dtelaroli/db2abc7dcb1b1c68c2bd3706ccfe751a to your computer and use it in GitHub Desktop.
Docker and docker-compose configuration for rails and mysql

Docker

Installation

Use docker on your own rails project

Create the files (Dockerfile, docker-compose.yml and Makefile) in the rails root folder and run commands:

make build # only first time
make up

Usage

# Build the docker image
make build

# Start the containers
make up # To stop use Ctrl + C

# Stop the containers if something wrong have happen on Ctrl + C
make stop

# Running bundle
make bundle

# Running db:migrate
make migrate

# Accessing the container
make bash # To stop use exit
default: &default
adapter: mysql2
encoding: utf8
pool: 15
host: db
username: user
password: password
development:
<<: *default
version: '3.4'
services:
db:
container_name: db
image: library/mysql:5.6.22
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=development
- MYSQL_USER=user
- MYSQL_PASSWORD=password
app:
container_name: app
image: myapp
user: $USER
volumes:
- bundle:/usr/local/bundle
- ${PWD}:${APP_PATH}/app
tty: true
ports:
- 3000:3000
links:
- db
command: rails s -b 0.0.0.0
volumes:
bundle:
name: bundle
# From one of the official ruby images
FROM ruby:2.4.3
LABEL [email protected]
# Using same user name and id is important to keep same permission between container and local machine
ARG USER
ARG UID
ARG RAILS_ENV
ARG APP_PATH=/var/www
RUN echo "RUN env=$RAILS_ENV UID=$UID USER=$USER"
RUN addgroup --gid $UID $USER && \
adduser --system --gid $UID --uid $UID $USER && \
mkdir -p ${APP_PATH} && \
chown -R $USER:$USER $APP_PATH /usr/local/bundle
RUN chown -R $USER:$USER $APP_PATH
RUN curl -sS http://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update
RUN apt-get -y install node yarn
USER $USER
WORKDIR ${APP_PATH}/app
CMD bash
build:
docker build -t myapp . --build-arg USER=$$USER --build-arg UID=$$(id -u) --no-cache
up:
docker-compose up
stop:
docker-compose stop
bundle:
docker-compose exec app bundle install -j 10 || docker-compose run app bundle install -j 10
migrate:
docker-compose exec app rails db:migrate || docker-compose run app rails db:migrate
bash:
docker-compose exec app bash -c "stty cols 200 && bash" || docker-compose run app bash -c "stty cols 200 && bash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment