Language and Framework Guides

Run a Crystal Container App

9min

Back4App Containers is a powerful platform for hosting Crystal Applications. With its ability to automatically deploy Dockerized Crystal 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 Crystal 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.

If you have any questions or comments, feel free to join the conversation in the #Containers channel on the Back4app Community on Slack. Anytime you can also contact us at [email protected].

1. Prepare Your Crystal Application

Before deploying your Crystal application on Back4App Containers, you need to make sure it's well-prepared. Follow these steps to get your application ready for deployment:

  1. Update dependencies: Make sure your Crystal application has the latest versions of all dependencies. Update your shard.yml file and run shards update to ensure compatibility.
  2. Configure environment variables: Set up any necessary environment variables, such as database connections or API keys. Use the config.cr file to store these variables, and make sure you don't commit sensitive information to your version control system.
  3. Optimize your application: Run your application in the --release mode to enable optimizations and reduce the final binary size. This is important to ensure better performance and reduce resource consumption.
  4. Remove unnecessary files: Clean up your project directory by removing any unnecessary files, such as temporary files, logs, or development-only assets.
  5. Add a file: Create a .dockerignore file to exclude files and directories that should not be included in the Docker image. This will help reduce the image size and speed up the build process.

2. Dockerization

To dockerize your Crystal application, create a Dockerfile in the root of your project. Here's an example for a more complex application:

Text


This Dockerfile uses the official Crystal language image, installs the dependencies, copies your application code, builds the binary, exposes the port 3000, and runs the application.

3. Test Your Project Locally

Before deploying your application, make sure to test it locally using Docker. Run the following command to build the Docker image and start the container:

Text


Visit http://localhost:3000 in your browser to verify that your application is running correctly.

4. Push Your Project to GitHub

Once your application is prepared and tested locally, push it to a GitHub repository. Follow these steps:

  1. Initialize a Git repository: Run git init in the root of your project directory.
  2. Add all files: Run git add . to add all the files to the repository.
  3. Commit the changes: Commit the changes with a descriptive message using git commit -m "Initial commit".
  4. Create a GitHub repository: Log in to your GitHub account and create a new repository.
  5. Link your local repository: Link your local repository to the remote one by running git remote add origin <repository_url>.
  6. Push the changes: Push your changes to the remote repository using git push -u origin main.

5. Deploy Your Application on Back4App Containers

To deploy your Crystal application on Back4App Containers, follow these steps:

  1. Log in to your Back4App account and navigate to the "Containers" section.
  2. Click "Create New App" and choose "GitHub" as the deployment source.
  3. Authorize Back4App to access your GitHub account and select the repository containing your Crystal project.
  4. Choose the appropriate branch and configure any required environment variables.
  5. Click "Create" to start the deployment process. Back4App Containers will automatically build and deploy your application using the provided Dockerfile.

6. Monitor Deployment and Address Possible Errors

After deploying your application, monitor its deployment using Back4App Containers. Access the Back4App dashboard and navigate to your container app. You can view the deployment logs and running logs to identify any errors or warning messages that might occur.

Pay attention to any issues related to dependencies, environment variables, or configuration. If you encounter any problems, address them and redeploy your application.

7. Troubleshooting Common Problems

Here are five common problems you might face when deploying and running a Crystal application on Back4App Containers, along with their solutions:

  1. Dependency issues: Ensure that your shard.yml file has the correct dependencies and versions. Update your dependencies using shards update and rebuild your Docker image if necessary.
  2. Environment variables: Make sure you have set up all required environment variables correctly. Double-check their values and update them in the config.cr file if needed.
  3. Build errors: If you encounter build errors, verify that your Dockerfile is correct and that all necessary files are included in the Docker image. Check the .dockerignore file to ensure you are not accidentally excluding important files.
  4. Port conflicts: If your application fails to start due to port conflicts, check your Dockerfile and make sure you are exposing the correct port. Also, ensure that your application is configured to listen on the correct port.
  5. Memory or CPU issues: If your application is consuming too much memory or CPU, optimize your code and reduce resource consumption. Consider using the Crystal --release flag to enable optimizations and reduce the binary size.

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 Crystal 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 Crystal application on Back4App Containers. This powerful platform offers a seamless and efficient way to host your Crystal applications, freeing you from the complexities of DevOps and allowing you to focus on your code.