Skip to content

Instantly share code, notes, and snippets.

@GregNing
Last active May 31, 2022 01:40
Show Gist options
  • Save GregNing/3509282cb4ddab7217941a26ccfe99d2 to your computer and use it in GitHub Desktop.
Save GregNing/3509282cb4ddab7217941a26ccfe99d2 to your computer and use it in GitHub Desktop.
Dockerfile

Rails dev environmnent:

This will build the local Dockerfile:

  docker-compose build --no-cache

Get the rails app running on the background:

  docker-compose up -d app

Enter rails console (within running container)

docker-compose exec -ti app bundle exec rails console

To run the test suite:

docker-compose exec -ti app bundle exec rake test   # rails 4
docker-compose exec -ti app bundle exec rails test  # rails 5

To inspect logs:

docker-compose logs -f

To restart just one container

docker-compose restart app

To stop (but not destroy) the cluster

docker-compose stop

To start (a stopped) cluster

docker-compose start

To destroy the cluster (usefull if config or state gets messed up)

docker-compose down

Check docker-compose config

docker-compose config

Debug in Docker(with pry) (As mentioned in below answers Ctrl+p, Ctrl+q will now turn interactive mode into daemon mode.)

docker attach app_name
---
version: '3.8'
services:
db:
image: mysql:8.0.19
container_name: mysql
restart: always
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- db_data:/var/lib/mysql
ports:
- "3307:3306"
cap_add:
- SYS_NICE
app:
build: .
working_dir: /app
container_name: container_name
stdin_open: true
tty: true
volumes:
- .:/app
- ~/.ssh:/root/.ssh
environment:
TZ: Asia/Taipei
command: /bin/sh -c "/usr/bin/start.sh"
ports:
- "3080:3080"
depends_on:
- db
volumes:
db_data:
external: false
driver: local
networks:
default:
external:
name: network-name
FROM ruby:2.7.0-slim
MAINTAINER Greg
RUN apt-get update && apt-get upgrade -y && \
apt-get install -y --no-install-recommends tzdata default-libmysqlclient-dev \
build-essential sudo git curl ssh openssh-client && \
sudo apt-get install -y nodejs yarn && \
apt-get clean autoclean && \
apt-get purge -y --auto-remove && \
rm -rf \
/var/lib/apt \
/var/lib/dpkg \
/var/lib/cache \
/var/lib/log
ENV APP_FOLDER /app
RUN mkdir ${APP_FOLDER}
ENV BUNDLER_VERSION 2.1.4
RUN gem install bundler --version "$BUNDLER_VERSION"
WORKDIR ${APP_FOLDER}
# Copy the main application.
COPY . ${APP_FOLDER}
COPY start.sh /usr/bin/
RUN chmod +x /usr/bin/start.sh
EXPOSE 3080
PORT=3080
RAILS_ENV=development
# Rails Application
DB_NAME=XXXX_development
DB_USER=root
DB_PASSWORD=123456
DB_HOST=db
# MySQL
MYSQL_ROOT_PASSWORD=123456
#!/usr/bin/env bash
# bin/start
echo "Removing old server pid's if any..."
rm -f tmp/pids/server.pid
echo "Checking bundle dependencies..."
bundle install
echo "Booting up..."
bundle exec puma -C config/puma.rb -e development
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment