Write down Docker Compose file for Ruby On Rails and MySQL Application

Write down Docker Compose file for Ruby On Rails and MySQL Application

Here's an example Docker Compose file for a Ruby on Rails app using a MySQL database. It includes services for the Rails app, MySQL database, and a volume to save MySQL data permanently:

  1. Create a new directory for your Ruby on Rails application.
mkdir my_rails_app
cd my_rails_app
  1. Create a Dockerfile for the Rails application.

Create a file named Dockerfile in the my_rails_app directory:

# Dockerfile for Rails application
FROM ruby:3.0

# Set the working directory in the container
WORKDIR /app

# Install dependencies
RUN apt-get update -qq && \
    apt-get install -y nodejs yarn && \
    gem install bundler

# Copy the Gemfile and Gemfile.lock to the container
COPY Gemfile Gemfile.lock ./

# Install gems
RUN bundle install

# Copy the rest of the application code to the container
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000
  1. Create a Docker Compose file (docker-compose.yml).

Create a file named docker-compose.yml in the my_rails_app directory:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: my_rails_app
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: mysecretpassword
      MYSQL_DATABASE: my_rails_app_development
      MYSQL_USER: root
      MYSQL_PASSWORD: mysecretpassword
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:

This Docker Compose file defines two services: web for the Rails application and db for the MySQL database. It also specifies the necessary configurations, dependencies, and volumes.

  1. Start the Rails application using Docker Compose.

Run the following commands in the my_rails_app directory:

docker-compose build
docker-compose up

This will build the Docker images and start the services. Access the Rails application at http://localhost:3000.

Remember to modify the Rails application code according to your project's setup and requirements. Also, consider using environment variables and secure credentials for production use.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!