Files
docker-docs/content/guides/ruby/deploy.md
Igor Aleksandrov c3431b0474 Rails guide upgrade (part 2) (#22114)
## Description

This PR has two goals. First of all it continues work that has been done
in #21559. Besides this, and this is more significant, I tried to
improve the structure of the documentation that is currently used for
different language-specific guides like
[PHP](https://docs.docker.com/guides/php/configure-ci-cd/),
[Go](https://docs.docker.com/guides/golang/),
[Python](https://docs.docker.com/guides/python/) and others, including,
of course, Ruby itself.

Each of these guides currently has a [Configure
CI/CD](https://docs.docker.com/guides/python/configure-ci-cd/) section.
Inside this section there is a GitHub Actions workflow example that has
nothing in common with a CI/CD pipeline. It's just an example of how to
build and push an image to a Docker Hub registry. We should be clear in
our documentation and not mislead our users. This was the main reason
why I renamed this section to "Automate your builds with GitHub
Actions". I also updated the content of this section to reflect the new
name and to make it more clear what the user can expect from this guide.
I suggest the same be done for all other language-specific guides.

Besides this, I changed the order of the sections in the Ruby guide. The
"Develop your app" section has been moved down to the bottom of the
guide. This makes more sense to me because of two reasons:

1. It is more important to start using Docker Hub right after you added
the Dockerfile to your project (section number one in all
language-specific guides).

2. I can hardly imagine anybody using Docker to run the app locally for
development purposes (at least for Ruby). What is really essential and
useful is to know how to run services, that are required by your app,
like a database, a cache server, or a local LLM. This is why the
"Develop your app" section should be rewritten to explain how to run the
infrastructure services that are required by the app and not the app
itself.

Below there are screenshots reflecting the changes that have been made
in this PR.

**Before**
<img width="1512" alt="Screenshot 2025-02-27 at 11 26 39"
src="https://github.com/user-attachments/assets/1ca06aea-ffeb-4efb-a14d-27254d2a2110"
/>

**After** 
<img width="1512" alt="Screenshot 2025-02-27 at 11 26 01"
src="https://github.com/user-attachments/assets/7abbe8b7-d1b3-480f-8105-49f967b51e47"
/>

## Related issues or tickets

#21559

## Reviews

- [x] Technical review
- [x] Editorial review
- [ ] Product review
2025-03-12 08:08:51 -07:00

6.0 KiB

title, linkTitle, weight, keywords, description, aliases
title linkTitle weight keywords description aliases
Test your Ruby on Rails deployment Test your deployment 50 deploy, kubernetes, ruby Learn how to develop locally using Kubernetes
/language/ruby/deploy/
/guides/language/ruby/deploy/

Prerequisites

Overview

In this section, you'll learn how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine. This lets you to test and debug your workloads on Kubernetes locally before deploying.

Create a Kubernetes YAML file

In your docker-ruby-on-rails directory, create a file named docker-ruby-on-rails-kubernetes.yaml. Open the file in an IDE or text editor and add the following contents. Replace DOCKER_USERNAME/REPO_NAME with your Docker username and the name of the repository that you created in Configure CI/CD for your Ruby on Rails application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: docker-ruby-on-rails-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      service: ruby-on-rails
  template:
    metadata:
      labels:
        service: ruby-on-rails
    spec:
      containers:
        - name: ruby-on-rails-container
          image: DOCKER_USERNAME/REPO_NAME
          imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: docker-ruby-on-rails-demo
  namespace: default
spec:
  type: NodePort
  selector:
    service: ruby-on-rails
  ports:
    - port: 3000
      targetPort: 3000
      nodePort: 30001

In this Kubernetes YAML file, there are two objects, separated by the ---:

  • A Deployment, describing a scalable group of identical pods. In this case, you'll get just one replica, or copy of your pod. That pod, which is described under template, has just one container in it. The container is created from the image built by GitHub Actions in Configure CI/CD for your Ruby on Rails application.
  • A NodePort service, which will route traffic from port 30001 on your host to port 8001 inside the pods it routes to, allowing you to reach your app from the network.

To learn more about Kubernetes objects, see the Kubernetes documentation.

Deploy and check your application

  1. In a terminal, navigate to docker-ruby-on-rails and deploy your application to Kubernetes.

    $ kubectl apply -f docker-ruby-on-rails-kubernetes.yaml
    

    You should see output that looks like the following, indicating your Kubernetes objects were created successfully.

    deployment.apps/docker-ruby-on-rails-demo created
    service/docker-ruby-on-rails-demo created
    
  2. Make sure everything worked by listing your deployments.

    $ kubectl get deployments
    

    Your deployment should be listed as follows:

    NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
    docker-ruby-on-rails-demo  1/1     1            1           15s
    

    This indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services.

    $ kubectl get services
    

    You should get output like the following.

    NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
    kubernetes                  ClusterIP   10.96.0.1       <none>        443/TCP          23h
    docker-ruby-on-rails-demo   NodePort    10.99.128.230   <none>        3000:30001/TCP   75s
    

    In addition to the default kubernetes service, you can see your docker-ruby-on-rails-demo service, accepting traffic on port 30001/TCP.

  3. To create and migrate the database in a Ruby on Rails application running on Kubernetes, you need to follow these steps.

    Get the Current Pods: First, you need to identify the pods running in your Kubernetes cluster. Execute the following command to list the current pods in the default namespace:

    # Get the current pods in the cluster in the namespace default
    $ kubectl get pods
    

    This command will display a list of all pods in the default namespace. Look for the pod with the prefix docker-ruby-on-rails-demo-. Here is an example output:

    NAME                                         READY   STATUS    RESTARTS      AGE
    docker-ruby-on-rails-demo-7cbddb5d6f-qh44l   1/1     Running   2 (22h ago)   9d
    

    Execute the Migration Command: Once you've identified the correct pod, use the kubectl exec command to run the database migration inside the pod.

    $ kubectl exec -it docker-ruby-on-rails-demo-7cbddb5d6f-qh44l -- rails db:migrate RAILS_ENV=development
    

    This command opens an interactive terminal session (-it) in the specified pod and runs the rails db:migrate command with the environment set to development (RAILS_ENV=development).

    By following these steps, you ensure that your database is properly migrated within the Ruby on Rails application running in your Kubernetes cluster. This process helps maintain the integrity and consistency of your application's data structure during deployment and updates.

  4. Open the browser and go to http://localhost:30001, you should see the ruby on rails application working.

  5. Run the following command to tear down your application.

    $ kubectl delete -f docker-ruby-on-rails-kubernetes.yaml
    

Summary

In this section, you learned how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine.

Related information: