Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Chandra Shettigar
Chandra Shettigar

Posted on • Originally published atdevteds.com on

Create Ruby On Rails Application Using Docker

A guide for creating a newRuby on Rails application usingDocker,without installing Ruby or Rails directly on your OS. It also addresses specific requirements such as using a specific version of MySQL and needing an isolated environment. Dockerizing an existing Rails app is not difficult, creating a new project without a specific version of Ruby or installed Rails can be challenging. This guide will help you out!

Thinking of creating a newRuby on Rails application and possibly run it as container on Kubernetes, Amazon ECS, etc? If so, do your requirements align with the following:

  • I want to create a newRuby on Rails application and build it using the latest stableRuby andRuby on Rails
  • I currently have an older version of Ruby installed for my other Rails applications and do NOT want to install a new version directly on my work machine for the new application
  • The new app will use a specific version of MySQL, and I already have an older version of MySQL installed on my local machine for other applications. Installing a new version may lead to conflicts or may not work at all
  • I require an isolated environment for my new Rails application and its database setup. While options such as RVM or rbenv exist for the Ruby portion, but I still need to solve for running multiple versions of MySQL servers
  • I have some experience in Docker but not a pro.Dockerizing an existing Rails application seems straightforward but creating a new Rails project without a specific Ruby or installed Rails, seems a bit complicated. I need aGet Started guide on creating a newRails project using Docker

If your requirements align with the above, this post will guide you through creating a new Rails application using Docker without installing Ruby or Rails directly on your OS.

Prerequisite: Ensure that you have Docker Engine installed on your machine. I have Docker Desktop (Docker for Mac) installed on my machine; Docker version is 20.10.11

Create project directory

mkdir my-rails-appcd my-rails-app
Enter fullscreen modeExit fullscreen mode

Create a project directory on your host machine. At the end of this article, this directory will contain a Rails application code files & folders.

Ruby Version

Before we create a Rails project using a specific Ruby version, we should have an environment with that version of Ruby installed. I am going to useRuby version 3.1.2.

Instead of installing Ruby on my machine or on OS directly, I will run a docker container using a Docker image containing Ruby 3.1.2 and then shell into the container.

docker run --rm -it -v $PWD:/app:rw ruby:3.1.2 /bin/bash> ruby -vruby 3.1.2p20 ...>> gem -v3.3.7>> bundle -vBundler version 2.3.7>
Enter fullscreen modeExit fullscreen mode

Let’s breakdown the docker command in the abovedocker run ... command,

  • We run this command from inside the project directorymy-rails-app and we mount the current directory to a path/app inside the container, with read-write (RW) permission. With this, when the container writes any files to/app, those get written to the project directorymy-rails-app on host
  • We use the Docker Imageruby:3.1.2 from DockerHub. When this container is run, we should be able to run any ruby commands inside that container
  • Last part of the command is/bin/bash. This instructs the Docker Engine to run the command/bin/bash inside the container when the container starts up. This is how you would shell into a new container
  • The option-i enables an interactive session with container shell. The option-t allocates puedo-TTY. The option–-rm ensures that the container is destroyed when we exit from the container shell

Once we’re inside the container shell, you may verify the ruby, bundler, gem, etc. And when you’re done, just typeexit to end the session and that should delete the container.

Install Rails Gem

We will be installing the Rails gem inside the container while we’re still on the container shell. Let’s run thegem install ... command inside the container shell. If you exited from that shell, run the abovedocker run ... command again.

> gem install -v 7.0.3 --no-prerelease --no-document railsFetching tzinfo-2.0.4.gem......Successfully installed actioncable-7.0.3Successfully installed rails-7.0.335 gems installed>> rails -vRails 7.0.3
Enter fullscreen modeExit fullscreen mode

We now haveRails 7 installed inside the container.However, if you exit from the container shell, you’ll lose this installation and you will have to run it again. That is because, on exiting the container shell the docker engine will delete the container and all its data.

Create a new project - Ruby on Rails

Now that we are inside the container shell, let’s create anew Ruby on Rails app; provide the directory/app as the project directory which maps to the current project directory,my-rails-app, where we ran docker run command to shell into container.

> rails new --skip-bundle --database=mysql /appInitialized empty Git repository in /app/.git/      create app      create app/assets/config/manifest.js      ...      ...      ...> ls /appGemfile Rakefile bin config.ru lib public test vendorREADME.md app config db log storage tmp
Enter fullscreen modeExit fullscreen mode

It created a new rails project and the code is at/app inside the container. Because we have/app mounted frommy-rails-app directory on the host machine, with read-write permissions, we would expect the project directory structure with all the files/folders is actually written tomy-rails-app. And the folders and files inside themy-rails-app directory should NOT be deleted if I exit from the container shell.

Let’s exit from the container shell and list the content ofmy-rails-app directory on host.

> exitexit$$$ lsGemfile app db storageGemfile.lock bin lib testREADME.md config log tmpRakefile config.ru public vendor$
Enter fullscreen modeExit fullscreen mode

This is how you can create the Rails project structure using Docker, without having to install specific versions of Ruby or Rails on your work machine.

Next Steps

To run this new Rails application as a container, there are a few things we need to do first:

  • Create a Dockerfile that uses the same Docker image for ruby:3.1.2
  • Optionally, create a docker-compose.yml file to provide the container with run-time configurations
  • Run a MySQL database as a container instead of installing a MySQL server directly on the host machine
  • Build the Docker image for the Rails application
  • Finally, run the application and build some amazing things!

If you need more details on these steps, I’ve covered them in the following blog post titledDockerize An Existing Ruby on Rails Application.” Now that Rails project has been created, one should be able to follow the same instructions toDockerize and Run as docker containers.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Technologist - Applications, AI, DevOps, Platform & Cloud Engineering | Creator and Instructor at Devteds (https://www.devteds.com) | AWS | Kubernetes
  • Location
    Chicago, IL
  • Work
    Creator and Instructor @ Devteds. (https://www.devteds.com)
  • Joined

More fromChandra Shettigar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp