Dockerfilebackendsoftware developmentrails 7 apiAPI

5 min read

|

August 13, 2024

How to Deploy & Run a Rails 7 API on a Remote Server with Docker

Deploy your Rails 7 API on a remote server using Docker. Learn key steps, configurations, and tips for smooth operation.



Chapters

Introduction

Docker installation

Docker image push 

Configuration on the server

Next up

Introduction

This blog is an extension of How to containerize an existing Rails 7 API with Docker, so make sure to read it before we dive into this one. In this blog, I will show you how to set up Docker and Rails 7  API on a remote server (Linux—Ubuntu). If you are curious about automating deployments, I've covered it here.

Docker installation

The first step is to log in to the server console, and from there, I recommend you install Docker on the server using the apt repository; it’s Docker's official tutorial. After you have successfully installed Docker, add your user to the Docker group. The next thing you need to do is create an account on the Docker Hub. Docker Hub is a cloud-based repository service where users can store, share, and manage Docker container images. The last thing in this section is the creation of a repository:  1.  Sign in to Docker Hub.  2. On the Repositories page, select Create repository.  3. Name it <your-username>/<your app name>.  4. Set the visibility to Private.  5. Select Create.

Docker image push 

 If you followed my previous tutorial, you should have successfully run your Rails 7 API in Docker in the local environment. In order to push our first image to Docker Hub, we need to login to Docker Hub from the local terminal. You can log in with $ docker login -u ; it will prompt you for the password you set in the previous step. After you get “Login Succeeded”, we can build and push an image to the Docker Hub.

Before proceeding to the image build, make sure that you have staging or production config in your config/database.yml (it’s an optional part from the previous tutorial). We are going to build the docker image with the following command from your application root directory:

$ docker build --platform linux/amd64 -t your_username/your_app_name:version .
  • docker build: This command is used to build Docker images from a Dockerfile.
  • --platform linux/amd64: This flag specifies the platform for which the image is built. In this case, it's targeting the AMD64 architecture running Linux because it’s for our Ubuntu server.
  • -t your_username/your_app_name:version: This flag tags the built image with a name and, optionally, a tag. Replace your_username with your Docker Hub username, your_app_name with the name of your application, and version with the version number of your application.
  • . : This dot represents the build context. It specifies the location of the Dockerfile and any other files that are needed to build the Docker image. In this case, it's assumed that the Dockerfile is in the current directory.

So, when you run this command, Docker will look for a Dockerfile in the current directory and use it to build an image tagged with the provided username, app name, and version. Make sure to replace your_username, your_app_name, and version with your actual values. Here is an example:  $ docker build --platform linux/amd64 -t thespian/thespian_app:1.0.0 . 

The final step is to push the image that we recently built:   $ docker push  your_username/your_app:version , for my example, that would be:   $ docker  push thespian/thespian_app:1.0.0

Configuration on the server

Once again, log in to your server console. Then make your app directory where you will add the docker-compose.yml file. docker-compose.yml is just slightly different from the one we used in the previous tutorial. Instead of the: build . , you should have something like this: image: your_username/your_app_name:version. Under the db service, instead of the environment part, you just need to pass the env_file. You should create a .env file in the same directory as your docker-compose.yml.docker-compose.yml

  app:    depends_on:         - db    image: your_username/your_app_name:version    command: bundle exec rails s -p 3000 -b '0.0.0.0'    ports:      - 3000:3000    tty: true    stdin_open: true    env_file:     - ".env"  db:   image: postgres:16.2-bullseye   restart: always   env_file:     - ".env"   volumes:     - data:/var/lib/postgresql/data/  volumes:   data:   

   

An example of .env file:

  RAILS_ENV=staging  POSTGRES_STAGING_USER=your_postgres_user    POSTGRES_STAGING_PASSWORD=your_postgres_password

If you don’t know how to import an existing database dump file, here is the link to my short blog about managing databases in Docker.Lastly, run  $ docker-compose up -d  from the path where your docker-compose.yml file is located, and that’s it!! Your Rails 7 API should be reachable on your_server_ip:3000. You can stop containers with  $ docker-compose down.

Next up

Now that you've mastered how to deploy and run a Rails 7 API on a remote server with Docker, check out what else you can do to tackle environment inconsistencies, deployment complexities, and scalability issues with Docker:

Share


WRITTEN BY

author

Software Engineer

Dario truly cares about the projects he works on and it shows in the quality of his work. He's always picking up new skills and loves to share what he learns with you.

UP NEXT

We Are The Manifest’s Leaders 2024 in Design and Development

Thespian is proud to be one of Croatia’s top-reviewed design and development companies in 2024. Here’s what’s behind this success and whom we owe it to!