Code to Containers on Magalix

How to take your source code to a running service on Magalix - 7 minutes read

Containers Alert!

Knowledge about Docker containers is not required for this guide. It is recommended though read the basic Docker docs to familiarize yourself with containers.

You can go from source code to a running application on top of Magalix relatively quickly.

This article will walk you through steps to commit your code into Github, compile it using CirlceCI, and run it on top of Magalix. The workflow is fairly simple and needed only once – see below illustration. Once is set up, each time your code is checked into your Github, CircleCI will build it, create container image and push container image to Docker Hub. Magalix then pulls the container image and runs it as part of your application.

Containerized Application?

If you already have your application containerized, you can jump directly to step three.

A typical Workflow to compile and deploy your application to Magalix

A typical Workflow to compile and deploy your application to Magalix

1. Commit Code to Github

In this article, we will use a simple micro-service that exposes an API that sends back a Fibonacci of your input number.It also exposes /about path that provides basic info about the micro-service.

We are committing to our Github private repo three files:

simple.go, which is a golang based micro-service source code
circle.yml, which is the circleCI’s build manifest file
Dockerfile, which is the docker image build file

package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
  //write welcome message to the response buffer
  fmt.Fprintf(w, "Welcome to Magalix Simple Service!! Your Path is: %s!", r.URL.Path[1:])
  //log the call to the standard I/O. You can read these logs at Application web console
  log.Println(fmt.Sprintf("request recieved - path: %s", r.URL.Path))
}

func main() {
  //http request handlers
  http.HandleFunc("/", handler)
  http.HandleFunc("/about/", about)
  //listen to port 8080
  http.ListenAndServe(":8080", nil)
}

//Message is the http parameter
type Message struct {
  Text string
}

func about(w http.ResponseWriter, r *http.Request) {

  //send json text back when user calls the /about
  m := Message{"Welcome to the Simple Service API, v1.0"}
  log.Println("request recieve - path: about")
  b, err := json.Marshal(m)

  if err != nil {
    panic(err)
  }

w.Write(b)
}

Service listens to port 8080 after starting the application. It responds back with a welcome message and the path used in the API call.

# requires statically linked go binary to be compiled
# to ./magalix/simple-service before docker build
FROM golang:latest
# executable folder
RUN mkdir /magalix
COPY simple-service /magalix
# run micro-service after boot up
ENTRYPOINT ["/magalix/simple-service"]
#expose port 8080
EXPOSE 8080

In the above Docker file, we create a new folder and copy executable to it before the service is started. We expose port 8080 is also exposed to route to allow the simple web service receive and respond to http requests.

machine:
services:
- docker

dependencies:
pre:
# compile go application before building docker image
- go build -o simple-service
override:
#docker container command. Make sure you replace Magalix with your registry name
- docker build --rm=false -t magalix/simple-service .
- docker run -p 8080:8080 magalix/simple-service:
background: true
# Give container 3 seconds to load before testing it.
- sleep 3

test:
override:
# check if service is running as expected
- "curl http://localhost:8080/about"
# - "[[ $(curl http://localhost:8080/about) = '{\"Text\":\"Welcome to the Simple Service API, v1.0\"}' ]]"

deployment:
hub:
#commit branch that will trigger the build process
branch: master
commands:
#docker access credentials. Add these as environment variables to CircleCI project
- docker login -e $DOCKER_EMAIL -u $DOCKER_USERNAME -p $DOCKER_PWD
# private registry image to be pushed to. Replace Magalix with your registry's name'
- docker push magalix/simple-service

In the above circle.yaml file, we build the source code before building docker image. We run post building a simple service test before we push it to our docker private registry.

You will need to manually set the variables for $DOCKER_EMAIL, $DOCKER_USER, and $DOCKER_PASS. The next section explains how this can be done.

2. Prepare CircleCI and Docker Hub

Link CirlceCI to your Github

If you don’t have CircleCI account yet, register with your Github account for easier access to your Github repos

  1. Click on the button labeled by “Account Settings”
  2. Click “Account Integrations”
  3. Then proceed with connecting your GitHub Account
  4. Next click on the box with a plus on it labeled “Add Project”
  5. Select your GitHub Profile
  6. Finally, locate the project you wish to add, in our case it would be golang-deploys-tutorial

Once you have located the project click the button that says “Build Project”

Select your repo that you want to build out of Github

Select your repo that you want to build out of Github

It will do the basic build of your go code and container image, but deployment will fail due to missing credentials of the docker account. You will need to add environment variables, as described below to allow CircleCI push your docker image.

Set up variables for docker deployment

  1. Click on builds
  2. Click the icon next to the project name
  3. This will open up the configurations page for your project. There are a lot of settings that you can tweak here but our main focus will be setting the environment variables
  4. Under the heading “Build Settings” you will see a link labeled “environment variables”
  5. Click on “Add variable”.
  6. The first one will be our Docker Email. In the “name” field enter DOCKER_EMAIL, then in the “value” field enter your email address associated with your docker account.
  7. Click “Add variable”
  8. Repeat steps 5 through 7 for DOCKER_USER and DOCKER_PASS

Before you rebuild to push the image to Docker hub, you will need to add the repository that will host your docker image.

Create Docker Repository

Go to http://hub.docker.com and create a new repository named simple-service. You can make it either public or private repository. Magalix support both.

3. Create Magalix Application

Go now to http://console.magalix.cloud and log into your account. Click on the top + sign to create a new application from scratch.

Add application’s name, description, and target cloud.
Click on the + sign to create a new service
Add service’ name and description. Keep the default number of replica, ICU, memory, and I/O
Add a new port 8080 and name it http-port
Click on the + beside containers to add a new container
Add container’s name and description. Add container’s image url. Using Magalix Docker hub account, the url is docker.io/magalix/simple-service. If you decided to make your container in private repository, check images saved in a private registry.
Select the private registry your added your Magalix account. If you didn’t add yet your private repository account details, click on add private registry
Add container’s port name as http-port and number as 8080
Click Next and then Finish to create the application.
Click the button labeled Application Dashboard to go application’s dashboard

It will take your image few seconds to few minutes to be downloaded, depending on your image size. When application’s state change from pending to running, you can visit the service’s url to test it.

Each time you commit your code into Github repository, CircleCI will compile it and push it to the private repository, and Magalix will upgrade your service immediately after image push.

Reasonable Input

If you pass a large number, i.e. more than 40, the service will take a significantly long time to compute it and return back the results

FAQ

Can I use BitBucket or Other CI tools?

Of course. You can use any CI tool of your choice as long as it will push a container image to a supported registry. Magalix will pull container’s image from any of the supported private registries – read more about our supported registries.

How long does it take to build container image?

It depends on multiple factors, such as packages that you want to install, your base image, any extra steps that you want to do before running your application inside the container, etc.

How fast can my running container get deployed or upgraded after pushing to the private registry?

It depends on the size of your container, but the process should start almost immediately after you updated container image is pushed.

Resources

Code to Containers on Magalix

How to take your source code to a running service on Magalix - 7 minutes read