Ready, Set, Deploy! Rails 7.1 Gets Docker Support

railsJanuary 02, 2024Dotby Alkesh Ghorpade

Rails 7.1 introduced a significant enhancement for Docker integration. It now generates essential Docker files by default when you create a new Rails application. This streamlines the setup process and simplifies deployment using Docker for production environments.

Here's what's included:

  • Dockerfile: Contains instructions for building the Docker image optimized for production use.

  • .dockerignore: Specifies files and directories to exclude from the image, reducing image size.

  • bin/docker-entrypoint: Customizes container startup behaviour for production readiness.

How to setup the Rails 7.1 app using docker

1. Create Rails app

The Rails version should be 7.1. The minimum Ruby version for Rails 7.1 is 3.1.2.

rails new docker_demo

2. Build the docker image

To build the docker image, navigate to the Rails application directory and run the below command:

cd docker_demo

docker build -t docker_demo .

Note:

Once the build command is successful, it displays the below message.

=> naming to docker.io/library/docker_demo                                                                                                                                                                                                                                   0.0s 

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview

The docker scout quickview command:

  • Analyzes the most recently built image

  • Provides a summary of vulnerabilities

  • Highlights base image vulnerabilities

  • Recommends base image updates

3. Running the image

The docker images command lists and manages Docker images on your system. It provides information about the images you have locally, including their repositories, tags, sizes, and creation dates.

> docker_demo % docker images

REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
docker_demo   latest    22ef356334e8   17 minutes ago   474MB

If you try running a container from this image, it will fail with an error:

> docker_demo % docker run docker_demo

bin/rails aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`
/rails/config/environment.rb:5:in `<main>'
Tasks: TOP => db:prepare => db:load_config => environment
(See full trace by running task with --trace)

When you created the Rails application, it added a .dockerignore file. If you check, the config/master.key is added to the file. Due to its extreme sensitivity, the config/master.key file is intentionally excluded from both Git and Docker images by default.

If this is missing, Rails looks out for an environment variable RAILS_MASTER_KEY. If the environment variable is also missing, the above error is raised.

To fix this, you need to pass the RAILS_MASTER_KEY to the docker run command as below:

docker run --rm -it -p 3000:3000 --env RAILS_MASTER_KEY=<your key> docker_demo

The above command should run successfully.

Note:

If you try these steps locally, the application will run in the production environment. Accessing localhost:3000 will not run successfully. The config.force_ssl is set to true by default in the Rails 7 application.

For easy local development, docked provides a pre-configured environment with all the necessary Rails dependencies, eliminating the hassle of manual setup for beginners. Docked Rails CLI simplifies setup with a Docker image, requiring only Docker installation.

4. Accessing Rails console

To access the Rails console, you need to execute the below command.

docker run --rm -it -v docker_demo:/rails/storage --env RAILS_MASTER_KEY=<your key> docker_demo console

Key points to remember:

  • Production focus: The generated Dockerfiles are primarily designed for production deployments, not local development.

  • Multi-stage builds: Employ multi-stage builds for smaller image sizes and better efficiency.

  • JavaScript build support: Include dependencies for JavaScript build environments.

  • Database configuration: Customize database connections within the container for production readiness, ensuring environment variables are set appropriately.

To know more about this feature, please refer to this PR.

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at justin@shakacode.com or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right