diff --git a/content/language/_index.md b/content/language/_index.md index d99d316e95a..56d0b10a4be 100644 --- a/content/language/_index.md +++ b/content/language/_index.md @@ -52,4 +52,7 @@ Learn how to containerize your applications and start developing using Docker. C
Develop with PHP
+
+ Develop with Ruby +
diff --git a/content/language/images/ruby-on-rails.webp b/content/language/images/ruby-on-rails.webp new file mode 100644 index 00000000000..c5c32ac545b Binary files /dev/null and b/content/language/images/ruby-on-rails.webp differ diff --git a/content/language/ruby/_index.md b/content/language/ruby/_index.md new file mode 100644 index 00000000000..2da18bcabb1 --- /dev/null +++ b/content/language/ruby/_index.md @@ -0,0 +1,18 @@ +--- +description: Containerize Ruby on Rails apps using Docker +keywords: Docker, getting started, ruby, language +title: Ruby on Rails language-specific guide +toc_min: 1 +toc_max: 2 +--- + +The Ruby language-specific guide teaches you how to containerize a Ruby on Rails application using Docker. In this guide, you’ll learn how to: + +* Containerize and run a Ruby on Rails application +* Set up a local environment to develop a Ruby on Rails application using containers +* Configure a CI/CD pipeline for a containerized Ruby on Rails application using GitHub Actions +* Deploy your containerized Ruby on Rails application locally to Kubernetes to test and debug your deployment + +Start by containerizing an existing Ruby on Rails application. + +{{< button text="Containerize a Ruby on Rails app" url="containerize.md" >}} diff --git a/content/language/ruby/configure-ci-cd.md b/content/language/ruby/configure-ci-cd.md new file mode 100644 index 00000000000..b54d7b112e7 --- /dev/null +++ b/content/language/ruby/configure-ci-cd.md @@ -0,0 +1,129 @@ +--- +title: Configure CI/CD for your Ruby on Rails application +keywords: ci/cd, github actions, ruby, flask +description: Learn how to configure CI/CD using GitHub Actions for your Ruby on Rails application. +--- + +## Prerequisites + +Complete all the previous sections of this guide, starting with [Containerize a Ruby on Rails application](containerize.md). You must have a [GitHub](https://github.com/signup) account and a [Docker](https://hub.docker.com/signup) account to complete this section. + +## Overview + +In this section, you'll learn how to set up and use GitHub Actions to build and test your Docker image as well as push it to Docker Hub. You will complete the following steps: + +1. Create a new repository on GitHub. +2. Define the GitHub Actions workflow. +3. Run the workflow. + +## Step one: Create the repository + +Create a GitHub repository, configure the Docker Hub credentials, and push your source code. + +1. [Create a new repository](https://github.com/new) on GitHub. + +2. Open the repository **Settings**, and go to **Secrets and variables** > + **Actions**. + +3. Create a new **Repository variable** named `DOCKER_USERNAME` and your Docker ID as value. + +4. Create a new [Personal Access Token (PAT)](../../security/for-developers/access-tokens.md/#create-an-access-token) for Docker Hub. You can name this token `docker-tutorial`. Make sure access permissions include Read and Write. + +5. Add the PAT as a **Repository secret** in your GitHub repository, with the name + `DOCKERHUB_TOKEN`. + +6. In your local repository on your machine, run the following command to change + the origin to the repository you just created. Make sure you change + `your-username` to your GitHub username and `your-repository` to the name of + the repository you created. + + ```console + $ git remote set-url origin https://github.com/your-username/your-repository.git + ``` + +7. Run the following commands to stage, commit, and push your local repository to GitHub. + + ```console + $ git add -A + $ git commit -m "my commit" + $ git push -u origin main + ``` + +## Step two: Set up the workflow + +Set up your GitHub Actions workflow for building, testing, and pushing the image +to Docker Hub. + +1. Go to your repository on GitHub and then select the **Actions** tab. + +2. Select **set up a workflow yourself**. + + This takes you to a page for creating a new GitHub actions workflow file in + your repository, under `.github/workflows/main.yml` by default. + +3. In the editor window, copy and paste the following YAML configuration. + + ```yaml + name: ci + + on: + push: + branches: + - main + + jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - + name: Build and push + uses: docker/build-push-action@v6 + with: + platforms: linux/amd64 + push: true + tags: ${{ vars.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest + ``` + + For more information about the YAML syntax for `docker/build-push-action`, + refer to the [GitHub Action README](https://github.com/docker/build-push-action/blob/master/README.md). + +## Step three: Run the workflow + +Save the workflow file and run the job. + +1. Select **Commit changes...** and push the changes to the `main` branch. + + After pushing the commit, the workflow starts automatically. + +2. Go to the **Actions** tab. It displays the workflow. + + Selecting the workflow shows you the breakdown of all the steps. + +3. When the workflow is complete, go to your + [repositories on Docker Hub](https://hub.docker.com/repositories). + + If you see the new repository in that list, it means the GitHub Actions + successfully pushed the image to Docker Hub. + +## Summary + +In this section, you learned how to set up a GitHub Actions workflow for your Ruby on Rails application. + +Related information: + - [Introduction to GitHub Actions](../../build/ci/github-actions/index.md) + - [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions) + +## Next steps + +Next, learn how you can locally test and debug your workloads on Kubernetes before deploying. + +{{< button text="Test your deployment" url="./deploy.md" >}} diff --git a/content/language/ruby/containerize.md b/content/language/ruby/containerize.md new file mode 100644 index 00000000000..9b3bd270db1 --- /dev/null +++ b/content/language/ruby/containerize.md @@ -0,0 +1,396 @@ +--- +title: Containerize a Ruby on Rails application +keywords: ruby, flask, containerize, initialize +description: Learn how to containerize a Ruby on Rails application. +aliases: + - /language/ruby/build-images/ + - /language/ruby/run-containers/ +--- + +## Prerequisites + +* You have installed the latest version of [Docker Desktop](../../get-docker.md). +* You have a [Git client](https://git-scm.com/downloads). The examples in this section show the Git CLI, but you can use any client. + +## Overview + +This section walks you through containerizing and running a Ruby on Rails application. + +## Get the sample application + +The sample application uses the popular [Ruby on Rails](https://rubyonrails.org/) framework. + +Clone the sample application to use with this guide. Open a terminal, change directory to a directory that you want to work in, and run the following command to clone the repository: + +```console +$ git clone https://github.com/falconcr/docker-ruby-on-rails.git +``` + +## Initialize Docker assets + +Now that you have an application, you can create the necessary Docker assets to +containerize your application. You can use Docker Desktop's built-in Docker Init +feature to help streamline the process, or you can manually create the assets. + +`docker init`, the command for bootstrapping the Docker-related assets for a project, does not yet support the Ruby programming language. This means that if you are working with Ruby, you'll need to create Dockerfiles and other related configurations manually. + +Inside the `docker-ruby-on-rails` directory, create the following files: + +Create a file named `Dockerfile` with the following contents. + +```dockerfile {collapse=true,title=Dockerfile} +# syntax=docker/dockerfile:1 + +# Use the official Ruby image with version 3.2.0 +FROM ruby:3.2.0 + +# Install dependencies +RUN apt-get update -qq && apt-get install -y \ + nodejs \ + postgresql-client \ + libssl-dev \ + libreadline-dev \ + zlib1g-dev \ + build-essential \ + curl + +# Install rbenv +RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv && \ + echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc && \ + echo 'eval "$(rbenv init -)"' >> ~/.bashrc && \ + git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build && \ + echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc + +# Install the specified Ruby version using rbenv +ENV PATH="/root/.rbenv/bin:/root/.rbenv/shims:$PATH" +RUN rbenv install 3.2.0 && rbenv global 3.2.0 + +# Set the working directory +WORKDIR /myapp + +# Copy the Gemfile and Gemfile.lock +COPY Gemfile /myapp/Gemfile +COPY Gemfile.lock /myapp/Gemfile.lock + +# Install Gems dependencies +RUN gem install bundler && bundle install + +# Copy the application code +COPY . /myapp + +# Precompile assets (optional, if using Rails with assets) +RUN bundle exec rake assets:precompile + +# Expose the port the app runs on +EXPOSE 3000 + +# Command to run the server +CMD ["rails", "server", "-b", "0.0.0.0"] +``` + +Create a file named `compose.yaml` with the following contents. + +```yaml {collapse=true,title=compose.yaml} +services: + web: + build: . + command: bundle exec rails s -b '0.0.0.0' + volumes: + - .:/myapp + ports: + - "3000:3000" +``` + +Create a file named `.dockerignore` with the following contents. + +```text {collapse=true,title=".dockerignore"} +git +.gitignore + +# Created by https://www.gitignore.io/api/git,ruby,rails,jetbrains+all +# Edit at https://www.gitignore.io/?templates=git,ruby,rails,jetbrains+all + +### Git ### +# Created by git for backups. To disable backups in Git: +# $ git config --global mergetool.keepBackup false +*.orig + +# Created by git when using merge tools for conflicts +*.BACKUP.* +*.BASE.* +*.LOCAL.* +*.REMOTE.* +*_BACKUP_*.txt +*_BASE_*.txt +*_LOCAL_*.txt +*_REMOTE_*.txt + +### JetBrains+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### JetBrains+all Patch ### +# Ignores the whole .idea folder and all .iml files +# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 + +.idea/ + +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Rails ### +*.rbc +capybara-*.html +.rspec +/db/*.sqlite3 +/db/*.sqlite3-journal +/public/system +/coverage/ +/spec/tmp +rerun.txt +pickle-email-*.html + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# TODO Comment out this rule if you are OK with secrets being uploaded to the repo +config/initializers/secret_token.rb +config/master.key + +# Only include if you have production secrets in this file, which is no longer a Rails default +# config/secrets.yml + +# dotenv +# TODO Comment out this rule if environment variables can be committed +.env + +## Environment normalization: +/.bundle +/vendor/bundle + +# these should all be checked in to normalize the environment: +# Gemfile.lock, .ruby-version, .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# if using bower-rails ignore default bower_components path bower.json files +/vendor/assets/bower_components +*.bowerrc +bower.json + +# Ignore pow environment settings +.powenv + +# Ignore Byebug command history file. +.byebug_history + +# Ignore node_modules +node_modules/ + +# Ignore precompiled javascript packs +/public/packs +/public/packs-test +/public/assets + +# Ignore yarn files +/yarn-error.log +yarn-debug.log* +.yarn-integrity + +# Ignore uploaded files in development +/storage/* +!/storage/.keep + +### Ruby ### +*.gem +/.config +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +# Ignore Byebug command history file. + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +/.bundle/ +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: + +# End of https://www.gitignore.io/api/git,ruby,rails,jetbrains+all +``` + +You should now have the following three files in your `docker-ruby-on-rails` +directory. + + +- .dockerignore +- compose.yaml +- Dockerfile + + +To learn more about the files, see the following: + - [Dockerfile](../../reference/dockerfile.md) + - [.dockerignore](../../reference/dockerfile.md#dockerignore-file) + - [compose.yaml](../../compose/compose-file/_index.md) + +## Run the application + +Inside the `docker-ruby-on-rails` directory, run the following command in a +terminal. + +```console +$ docker compose up --build +``` + +Open a browser and view the application at [http://localhost:3000](http://localhost:3000). You should see a simple Ruby on Rails application. + +In the terminal, press `ctrl`+`c` to stop the application. + +### Run the application in the background + +You can run the application detached from the terminal by adding the `-d` +option. Inside the `docker-ruby-on-rails` directory, run the following command +in a terminal. + +```console +$ docker compose up --build -d +``` + +Open a browser and view the application at [http://localhost:3000](http://localhost:3000). + +You should see a simple Ruby on Rails application. + +In the terminal, run the following command to stop the application. + +```console +$ docker compose down +``` + +For more information about Compose commands, see the [Compose CLI +reference](../../compose/reference/_index.md). + +## Summary + +In this section, you learned how you can containerize and run your Ruby +application using Docker. + +Related information: + - [Build with Docker guide](../../build/guide/index.md) + - [Docker Compose overview](../../compose/_index.md) + +## Next steps + +In the next section, you'll learn how you can develop your application using +containers. + +{{< button text="Develop your application" url="develop.md" >}} diff --git a/content/language/ruby/deploy.md b/content/language/ruby/deploy.md new file mode 100644 index 00000000000..38474713026 --- /dev/null +++ b/content/language/ruby/deploy.md @@ -0,0 +1,162 @@ +--- +title: Test your Ruby on Rails deployment +keywords: deploy, kubernetes, ruby +description: Learn how to develop locally using Kubernetes +--- + +## Prerequisites + +- Complete all the previous sections of this guide, starting with [Containerize a Ruby on Rails application](containerize.md). +- [Turn on Kubernetes](/desktop/kubernetes/#install-and-turn-on-kubernetes) in Docker Desktop. + +## 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](configure-ci-cd.md). + +```yaml +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](configure-ci-cd.md). + - 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](https://kubernetes.io/docs/home/). + +## Deploy and check your application + +1. In a terminal, navigate to `docker-ruby-on-rails` and deploy your application to + Kubernetes. + + ```console + $ 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. + + ```shell + deployment.apps/docker-ruby-on-rails-demo created + service/docker-ruby-on-rails-demo created + ``` + +2. Make sure everything worked by listing your deployments. + + ```console + $ kubectl get deployments + ``` + + Your deployment should be listed as follows: + + ```shell + 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. + + ```console + $ kubectl get services + ``` + + You should get output like the following. + + ```shell + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE + kubernetes ClusterIP 10.96.0.1 443/TCP 23h + docker-ruby-on-rails-demo NodePort 10.99.128.230 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: + + ```sh + # 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: + + ```console + 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. + + ```sh + $ 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](http://localhost:30001), you should see the ruby on rails application working. + +5. Run the following command to tear down your application. + + ```console + $ 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: + - [Kubernetes documentation](https://kubernetes.io/docs/home/) + - [Deploy on Kubernetes with Docker Desktop](../../desktop/kubernetes.md) + - [Swarm mode overview](../../engine/swarm/_index.md) \ No newline at end of file diff --git a/content/language/ruby/develop.md b/content/language/ruby/develop.md new file mode 100644 index 00000000000..3738e8cd6a4 --- /dev/null +++ b/content/language/ruby/develop.md @@ -0,0 +1,199 @@ +--- +title: Use containers for Ruby on Rails development +keywords: ruby, local, development +description: Learn how to develop your Ruby on Rails application locally. +--- + +## Prerequisites + +Complete [Containerize a Ruby on Rails application](containerize.md). + +## Overview + +In this section, you'll learn how to set up a development environment for your containerized application. This includes: + +- Adding a local database and persisting data +- Configuring Compose to automatically update your running Compose services as you edit and save your code + +## Add a local database and persist data + +You can use containers to set up local services, like a database. In this section, you'll update the `compose.yaml` file to define a database service and a volume to persist data. + +In the cloned repository's directory, open the `compose.yaml` file in an IDE or text editor. You need to add the database password file as an environment variable to the server service and specify the secret file to use. + +The following is the updated `compose.yaml` file. + +```yaml {hl_lines="07-25"} +services: + web: + build: . + command: bundle exec rails s -b '0.0.0.0' + ports: + - "3000:3000" + depends_on: + - db + environment: + - RAILS_ENV=test + env_file: "webapp.env" + db: + image: postgres:latest + secrets: + - db-password + environment: + - POSTGRES_PASSWORD_FILE=/run/secrets/db-password + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + postgres_data: +secrets: + db-password: + file: db/password.txt +``` + +> **Note** +> +> To learn more about the instructions in the Compose file, see [Compose file +> reference](/compose/compose-file/). + +Before you run the application using Compose, notice that this Compose file specifies a `password.txt` file to hold the database's password. You must create this file as it's not included in the source repository. + +In the cloned repository's directory, create a new directory named `db` and inside that directory create a file named `password.txt` that contains the password for the database. Using your favorite IDE or text editor, add the following contents to the `password.txt` file. + +```text +mysecretpassword +``` + +Save and close the `password.txt` file. In addition, in the file `webapp.env` you can change the password to connect to the database. + +You should now have the following contents in your `docker-ruby-on-rails` +directory. + +```text +. +├── Dockerfile +├── Gemfile +├── Gemfile.lock +├── README.md +├── Rakefile +├── app/ +├── bin/ +├── compose.yaml +├── config/ +├── config.ru +├── db/ +│ ├── development.sqlite3 +│ ├── migrate +│ ├── password.txt +│ ├── schema.rb +│ └── seeds.rb +├── lib/ +├── log/ +├── public/ +├── storage/ +├── test/ +├── tmp/ +└── vendor +``` + +Now, run the following `docker compose up` command to start your application. + +```console +$ docker compose up --build +``` + +In Ruby on Rails, `db:migrate` is a Rake task that is used to run migrations on the database. Migrations are a way to alter the structure of your database schema over time in a consistent and easy way. + +```console +$ docker exec -it docker-ruby-on-rails-web-1 rake db:migrate RAILS_ENV=test +``` + +You will see a similar message like this: + +``console +== 20240710193146 CreateWhales: migrating ===================================== +-- create_table(:whales) + -> 0.0126s +== 20240710193146 CreateWhales: migrated (0.0127s) ============================ +`` + +Refresh in your browser and add the whales. + +Press `ctrl+c` in the terminal to stop your application and run `docker compose up` again, the whales are being persisted. + +## Automatically update services + +Use Compose Watch to automatically update your running Compose services as you +edit and save your code. For more details about Compose Watch, see [Use Compose +Watch](../../compose/file-watch.md). + +Open your `compose.yaml` file in an IDE or text editor and then add the Compose +Watch instructions. The following is the updated `compose.yaml` file. + +```yaml {hl_lines="13-16"} +services: + web: + build: . + command: bundle exec rails s -b '0.0.0.0' + ports: + - "3000:3000" + depends_on: + - db + environment: + - RAILS_ENV=test + env_file: "webapp.env" + + develop: + watch: + - action: rebuild + path: . + db: + image: postgres:latest + secrets: + - db-password + environment: + - POSTGRES_PASSWORD_FILE=/run/secrets/db-password + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + postgres_data: +secrets: + db-password: + file: db/password.txt +``` + +Run the following command to run your application with Compose Watch. + +```console +$ docker compose watch +``` + +Any changes to the application's source files on your local machine will now be immediately reflected in the running container. + +Open `docker-ruby-on-rails/app/views/whales/index.html.erb` in an IDE or text editor and update the `Whales` string by adding a exclamation marks. + +```diff +-

Whales

++

Whales!

+``` + +Save the changes to `index.html.erb` and then wait a few seconds for the application to rebuild. Go to the application again and verify that the updated text appears. + +Press `ctrl+c` in the terminal to stop your application. + +## Summary + +In this section, you took a look at setting up your Compose file to add a local +database and persist data. You also learned how to use Compose Watch to automatically rebuild and run your container when you update your code. + +Related information: + - [Compose file reference](/compose/compose-file/) + - [Compose file watch](../../compose/file-watch.md) + - [Multi-stage builds](../../build/building/multi-stage.md) + +## Next steps + +In the next section, you'll take a look at how to set up a CI/CD pipeline using GitHub Actions. + +{{< button text="Configure CI/CD" url="configure-ci-cd.md" >}} diff --git a/data/toc.yaml b/data/toc.yaml index a10873d46ae..d2669b263d3 100644 --- a/data/toc.yaml +++ b/data/toc.yaml @@ -179,7 +179,18 @@ Guides: path: /language/php/configure-ci-cd/ - title: "Test your deployment" path: /language/php/deploy/ - + - sectiontitle: Ruby + section: + - title: "Overview" + path: /language/ruby/ + - title: "Containerize your app" + path: /language/ruby/containerize/ + - title: "Develop your app" + path: /language/ruby/develop/ + - title: "Configure CI/CD" + path: /language/ruby/configure-ci-cd/ + - title: "Test your deployment" + path: /language/ruby/deploy/ - sectiontitle: Use-case guides section: - path: /guides/use-case/