Rails 7.1 Introduces Default Dockerfiles

In the world of web development, the optimization of deployment processes plays a crucial role in enhancing efficiency.

Rails 7.1 introduces an exciting enhancement by providing default Docker-related files for new applications to make the setup process easier and simplifies the deployment process of our application in production environments using Docker.

The Dockerfile, .dockerignore, and bin/docker-entrypoint are now bundled within the Rails application. These files cater specifically to production deployments rather than local development.

Docker steps

  • Build the Docker image:
docker build -t blog-app .
  • Create a Docker volume:
docker volume create app-storage
  • Run the Rails app:
docker run --rm -it -v app-storage:/rails/storage -p 3000:3000 --env RAILS_MASTER_KEY=<your-config-master-key> blog-app
  • Start a console or runner from this Docker image:
docker run --rm -it -v app-storage:/rails/storage --env RAILS_MASTER_KEY=<your-config-master-key> blog-app console

To create a multi-platform image (e.g., for deployment on Apple Silicon, AMD, or Intel) and push it to Docker Hub, follow these steps:

  • Login to Docker Hub:
docker login -u <your-user>
  • Create a buildx builder and set it as the current builder:
docker buildx create --use
  • Build and push the image:
docker buildx build --push --platform=linux/amd64,linux/arm64 -t <your-user/image-name> .

For local development, we can use docked. Setting up Rails for the first time with all the dependencies necessary can be daunting for beginners.

Docked Rails CLI uses a Docker image to make it much easier, requiring only Docker to be installed.

docker volume create ruby-bundle-cache
alias docked='docker run --rm -it -v ${PWD}:/rails -v ruby-bundle-cache:/bundle -p 3000:3000 ghcr.io/rails/cli'
docked rails new blog-app
cd blog-app
docked rails generate scaffold post title:string body:text
docked rails db:migrate
docked rails server

Need help on your Ruby on Rails or React project?

Join Our Newsletter