---
title: Run a Ruby Container App
slug: docs-containers/run-a-ruby-container-app
docTags: 
createdAt: 2023-04-20T01:15:54.000Z
---

Back4App Containers is a powerful platform for hosting Ruby Applications. With its ability to automatically deploy Dockerized Rails apps, you can launch your project in a scalable and flexible environment with ease. In this guide, we will walk you through the process of preparing and deploying your Rails application on Back4App Containers, covering everything from simple projects to more complex setups. We will begin with the necessary preparations, then move on to dockerizing the application, testing it locally, pushing the project to GitHub, setting up the project on Back4App Containers, monitoring deployment, and troubleshooting common issues.

:::hint{type="info"}
At anytime if you want to check a sample working Ruby on Rails project on Back4app containers go to : [[[[[[https://github.com/templates-back4app/containers-ruby-rails-template](https://github.com/templates-back4app/containers-ruby-rails-template)](https://github.com/templates-back4app/containers-asp-net-sample)](https://github.com/templates-back4app/containers-go-sample)](https://github.com/templates-back4app/containers-elixir-phoenix)](https://github.com/templates-back4app/containers-node-express-sample)](https://github.com/templates-back4app/containers-python-flask-sample)
:::

### 1. Prepare your Ruby application

Before deploying your Ruby application on Back4App Containers, you need to prepare it properly. Follow these steps to get your application ready:

1. **Update your Ruby version**: Make sure your Ruby version is up-to-date and compatible with your application's dependencies. You can use [rbenv](https://github.com/rbenv/rbenv) or [RVM](https://rvm.io/) to manage multiple Ruby versions.
2. **Update your application's dependencies**: Update your `Gemfile` and run `bundle update` to ensure all dependencies are up-to-date.
3. **Configure your database**: Update your `config/database.yml` file to use the correct database adapter and credentials for your production environment.
4. **Setup environment variables**: Create a `.env` file to store sensitive information like API keys, secrets, and database credentials. Make sure to add `.env` to your `.gitignore` file to prevent it from being pushed to GitHub.
5. **Precompile assets**: Run `bundle exec rails assets:precompile` to precompile your assets (stylesheets, JavaScript files, images, etc.) for production.

### 2. Dockerization

Create a `Dockerfile` in your project's root directory to specify how your application should be built and run. Here's an example of a `Dockerfile` for a more complex Ruby application:

```dockerfile
# Start from the official Ruby image
FROM ruby:3.0.3

# Install Node.js and Yarn (needed for Rails asset compilation)
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client yarn

# Set the working directory
WORKDIR /myapp

# Add the Gemfile and Gemfile.lock to the image
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

# Install gems
RUN bundle install

# Copy the rest of the application into the image
COPY . /myapp

# Expose the port that the Rails server will run on
EXPOSE 3000

# Define the command to start the server
CMD ["rails", "server", "-b", "0.0.0.0"]
```

### 3. Test your Project Locally

Before pushing your project to GitHub, make sure it runs correctly in a Docker container. Build the Docker image by running:

```text
docker build -t my-ruby-app .
```

Then, run a container using the built image:

```text
docker run -p 3000:3000 my-ruby-app
```

Visit `http://localhost:3000` to confirm that your application runs correctly.

### 4. Push your project to GitHub

Create a new repository on GitHub and follow the instructions to push your local project to the remote repository. Be sure to include the `Dockerfile` and any other necessary files, but exclude sensitive information like the `.env` file.

### 5. Deploy your application on Back4App Containers

In your GitHub repository, install the Back4App GitHub App to allow Back4App Containers to access your code. Next, create a new container app on the Back4App Containers platform. Follow these steps:

1. **Sign in** to your Back4App account.
2. **Create a new container app**: Click on "New Container App" and provide the necessary information, such as the app name and description.
3. **Select the GitHub repository**: Choose the GitHub repository containing your Ruby application.
4. **Configure the deployment**: Specify the branch to deploy and the path to the `Dockerfile`.
5. **Deploy the application**: Click "Deploy" to initiate the deployment process. Back4App Containers will automatically build and deploy your application using the provided `Dockerfile`.

### 6. Monitor deployment and address possible errors

After initiating the deployment, monitor the progress using the Back4App Containers dashboard. Keep an eye on the logs and pay attention to any error or warning messages that may appear. Address any issues that arise during the deployment process to ensure your application runs smoothly.

### 7. Troubleshooting common problems

When deploying and running your Ruby application on Back4App Containers, you may encounter some common issues. Here are five of them and their respective solutions:

1. **Docker build fails**: Check the error messages in the build logs to identify the cause. Common issues include incorrect syntax in the `Dockerfile`, missing dependencies, or incompatible Ruby versions.
2. **Application fails to start**: Ensure that your `CMD` or `ENTRYPOINT` instructions in the `Dockerfile` are correct. Verify that the specified command starts your Ruby application properly.
3. **Application crashes after starting**: Review your application logs for any error messages or stack traces. Common issues include incorrect environment variable values, missing files, or misconfigured settings.
4. **Application is slow or unresponsive**: Check the resource usage of your container in the Back4App Containers dashboard. If your application consumes too much CPU, memory, or disk space, consider optimizing your code, increasing the container resources, or scaling out your application.
5. **Database connection issues**: Ensure that your `config/database.yml` file is configured correctly for the production environment, and that your database server is accessible from your container. Double-check your environment variables and database credentials.
6. **Asset-related issues**: If your assets (stylesheets, JavaScript files, images, etc.) are not loading or appear broken, confirm that you precompiled your assets with `bundle exec rails assets:precompile`. Additionally, verify that your production environment is correctly configured to serve assets.
7. **Issues with third-party services**: If your application relies on third-party services (such as APIs or payment gateways), check that you have the correct API keys, credentials, and endpoints configured for your production environment. It may be necessary to update your environment variables or make changes to your application's code.

:::hint{type="info"}
At anytime if you want to check a sample working Ruby on Rails project on Back4app containers go to : [[[[[[https://github.com/templates-back4app/containers-ruby-rails-template](https://github.com/templates-back4app/containers-ruby-rails-template)](https://github.com/templates-back4app/containers-asp-net-sample)](https://github.com/templates-back4app/containers-go-sample)](https://github.com/templates-back4app/containers-elixir-phoenix)](https://github.com/templates-back4app/containers-node-express-sample)](https://github.com/templates-back4app/containers-python-flask-sample)
:::

When troubleshooting, remember that logs are your best friend. The Back4App Containers platform provides detailed logs for your application, allowing you to identify and resolve issues quickly. By addressing these common problems, you can ensure a smooth deployment and runtime experience for your Ruby application on Back4App Containers.

With the help of this guide, you should now have a better understanding of how to prepare, deploy, and troubleshoot a Ruby application on Back4App Containers. This powerful platform offers a seamless and efficient way to host your Ruby applications, freeing you from the complexities of DevOps and allowing you to focus on your code.
