In today's world of software development, microservices have gained significant popularity as a method for building scalable, maintainable, and resilient systems. Microservices enable organizations to break down complex applications into smaller, self-contained units that can be developed, deployed, and scaled independently. CTO.ai provides a seamless way to preview changes made to your applications before they are deployed to the production environment.

CTO.ai Services are a popular approach for deploying microservices, as it allows for consistent deployment across different environments, easy scaling, and simplified management. CTO.ai Services is a powerful feature that streamlines the development process by providing an easy and efficient way to preview, test, and collaborate on application changes before deployment to production.

In this blog post, we will explore the process of building and running a microservice, from design to development, using CTO.ai Services.

Prerequisites

  • Ops CLI installed on your local machine
  • CTO.ai Service preview configured on your workflow

Getting Started

  • Initialize your service preview workflow using the ops init command in your CLI and select Service.
  • Select the language you want to use; you can use Javascript, Bash, Python, or Golang.
  • Enter the name and description of your services in your CLI.
  • Next, provide a version for your service workflows too.

When you create your service workflows, a basic template will be created for you, which includes your:

  • .dockerignore file
  • Dockerfile
  • The python main.py file
  • Ops.yml file
  • Requirements.txt  file

In the Dockerfile, the FROM registry.cto.ai/official_images/python:2-3.7-buster-slim specifies the base image for the Docker container. It uses a custom Python image hosted on the registry.cto.ai repository.

  • The WORKDIR /ops sets the working directory for the container to /ops. All subsequent commands in the Dockerfile will be executed in this directory.
  • The USER ops line changes the user for the subsequent commands in the Dockerfile to the ops user. Running as a non-root user is a security best practice to limit potential damage in case of an exploit.
  • ADD requirements.txt . copies the requirements.txt file from the build context (the directory where the Dockerfile is located) to the /ops directory inside the container. The requirements.txt file typically contains a list of Python packages required by the applications.
  • RUN pip3 install -r requirements.txt installs the Python packages specified in the requirements.txt file using the pip package manager. Doing this during the image build process includes the necessary dependencies in the final image.
  • ADD –chown=ops:9999 . . This line copies all files from the build context to the /ops directory inside the container while setting up the ownership to the ops user and the group with the ID 9999. This ensures that the application files are accessible by the ops user during container runtime.

This tutorial will use the Python workflow to build and run our microservice. The Python code generated from the ops init command is a Python script that utilizes the flask framework and the cto_ai library to create a web application that returns Service Preview Workflows.

Book-a-Consultation_v2

Build and Publish Services

When you are done configuring your service workflows, defining your microservice is essential to automatically build and test your application to help your team deliver high-quality product changes faster.

In your terminal, build your application using the ops build . command. The ops build . command will build the Docker image and the `ops.yaml file containing all the instructions to build your microservice.

  • Publish your service workflow, and enter the version changelog for it. Next, publish your workflow after building it. The ops publish . command is a command used in the CTO.ai CLI to publish your custom application operation from the current directory to the CTO.ai registry.
  • Wait for some minutes; you’ll see that your workflow has been published.

Start your Microservice Application

When you are done publishing your application to your container registry, start your microservice using the ops start <service_name> command. In my application, the command will be ops start service-workflows:0.1.0. Your service name can be generated from the ops.yml file. When you run your microservice, you will see the preview URL for your application in your terminal.

  • Visit your preview URL in your browser, you will see the changes you configured in your Python application.

The preview URLS generated from the terminal allows developers to see their changes live as they work on a feature or bug fix. This real-time feedback helps identify issues early in the development process, speeding up the development cycle and reducing potential errors.

View Microservice in CTO.ai Dashboard

Back in your CTO.ai dashboard, click on Services. You will see your isolated microservice environment that mirrors your production setup.  In your Service dashboard, you can see your VERSION number, number of RUNS, and LAST RUN, track when it was created, and view the status of your microservice and the required action.

  • You will see your environment logs when you click on the RUN ID.

In the ACTIONS tab, click on the view icon; it will redirect you to your specific preview URL with the new changes.


Unlock the Full Potential of Microservices with CTO.ai Today

Ready to streamline your development process and scale your applications like never before? Dive into the world of microservices with CTO.ai and experience a whole new level of efficiency and flexibility.


By utilizing the CTO.ai Service feature, development teams can catch bugs and other issues early in the development cycle, reducing the risk of deploying problematic code to the production environment. Additionally, it facilitates better collaboration and communication within the team, as everyone can review and discuss the changes in real-time using the preview deployment.

Ready to unlock the power of CTO.ai for your team? Schedule your consultation now with one of our experts today!