If you'd like to run your Ruby on Rails workloads in a container you can do so by using a custom container. The following is a tutorial on how to run your Ruby on Rails application in a container on App Service.
- Ruby on Rails application
- Docker
The first thing you'll need to containerize your Ruby application is to add a Dockerfile. This file will contain the instructions necessary for Docker to build your container.
- Go to your application code and add a new file named
Dockerfile
. This file can be placed next to your Gemfile. - Next, paste the following code:
FROM ruby:2.7.6
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN bundle install
ADD . /usr/src/app/
EXPOSE 3000
CMD rails s -b 0.0.0.0
Now that we've added our Dockerfile to the application, we can build the container and run it locally.
- Open up your terminal and navigate to the applications folder where the Dockerfile lives
- Run the following command to build and name the docker image.
docker build -t my-ruby-docker-image .
- Next, run the Docker run command to run it locally
docker run -it -p 3000:3000 my-ruby-docker-image
Once you run this command, your docker image will run at the port we specified. You can visit https://localhost:3000 and view your application.
Now that our container is working locally, we can confidently deploy our application to Azure Container Registry.
- Use the docker
docker tag
command to tag your local image to your registry
docker tag my-ruby-docker-image:latest <my-registry-name>.azurecr.io/my-ruby-docker-image
- Next, use the docker
docker push
command to push the image to Azure Container Registry
docker push <my-registry-name>.azurecr.io/my-ruby-docker-image
Once your image is pushed, you are now ready to create a new web app in App Service.
The last step is to create a Web App specifically for container workloads
- Create a Web App resource as you normally would
- For the instance details choose the following options:
- Publish: Docker Container
- Operating System: Linux
- Then, click the Next:Docker > button at the bottom of the page
- Find the Image Source drop-down menu and choose Azure Container Registry
- Choose your registry options to match the container you pushed to ACR.
- Click Review+Create to start deploying your application
Once the resource is created, wait a few minutes as the container is building and in a short time you will see your application deployed in a custom container.