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
simplifies deployment using Docker for production environments.
Here's what's included:
Contains instructions for building the Docker image
optimized for production use.
Specifies files and directories to exclude from the image,
reducing image size.
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.
2. Build the docker image
To build the docker image,
navigate to the Rails application directory
run the below command:
docker build -t docker_demo .
build command is successful,
it displays the below message.
=> naming to docker.io/library/docker_demo 0.0s
View a summary of image vulnerabilities and recommendations → docker scout quickview
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
docker images command lists
manages Docker images on your system.
It provides information about the images you have locally,
including their repositories,
> 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
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`
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
If you check,
config/master.key is added to the file.
Due to its extreme sensitivity,
config/master.key file is intentionally excluded from both Git
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.
If you try these steps locally,
the application will run in the
localhost:3000 will not run successfully.
config.force_ssl is set to
true by default
in the Rails 7 application.
For easy local development,
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:
The generated Dockerfiles are primarily designed for production deployments,
not local development.
Employ multi-stage builds for smaller image sizes
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