Xylos brands

How to build a container

In the previous blog post, we explained how to package an application in a container. To illustrate this, we used a simple web application that recognises images, which we executed on a PC and an Azure Container Instance (ACI). In this post, we’ll delve deeper into how to build and package the application.

1. A developer’s perspective

For you as a developer, there are hardly any differences, but it’s important to use the right operating system. When we build new applications at Xylos, we usually choose Linux. Our Xylos experts (and our other brands’ experts, such as Neo Learning and Bagaar) use languages like PHP, JavaScript (Node.js), .NET, .NET Core, Python and Go. Except for the original .NET, these languages all support Linux and Linux containers.

We wrote the web application from the previous blog post in Go, which supports Windows, Mac and Linux. You can compile Go code with a simple command:

go build -o /usr/bin/app .

This command generates a Linux executable called ‘app’ in the /usr/bin folder. To activate the web application, just start the ‘app’. The web application then becomes available on http://localhost:9090 – or on https://localhost if you’ve configured an SSL certificate. Remember that this web application only works on Linux and Mac OS because it contains several dependencies that aren’t compatible with Windows.

As a developer, you can test and execute the application locally without using containers. Be sure to look at the code as well.

2. How to build a container

As a developer, you’re in charge of packaging the web application in a container. You can use the application’s own workstation for this. If you want to build a container, you’ll need the following:

  • Have Docker Desktop installed: it works with Mac, Linux and Windows
  • A Docker file: a recipe with instructions to build the container image

Don’t forget to read the Docker file with instructions for the web application.

This Docker file does the following:

  • Start with an existing image and build on it. This process uses the tensorflow/tensorflow:1.12.0 image that contains all necessary elements to execute a deep learning model that recognises images.
  • Install the ‘TensorFlow C’ library in the container image; it’s needed for Go and its TensorFlow support.
  • Install Go in the container image.
  • Build the web application and save the executable ‘app’ in /usr/bin.
  • Set up the ‘app’ so that it’s executed when the container is started. This is what the last command in the Docker file (CMD [ "app" ]) does.

With the Docker file and the code in the same folder, you can use the following command to build the container image:

docker build -t xylos/nasnet .

This command will download the tensorflow/tensorflow:1.12.0 image if you haven’t used it already. After completing the download, follow the instructions in the Docker file and save the result in a new container image titled xylos/nasnet.

You can now use the following command to test the container image on its workstation:

docker run -p 80:9090 -d xylos/nasnet

This command makes the web application available on http://localhost. If everything works as desired, you can save the container image in a Docker repository by using the following command:

docker push xylos/nasnet

Docker knows that we’ll want to use a public registry, so it’ll request the user’s (in this case: Xylos’) login information.

3. What about private registries?

When we build an application at Xylos, such as OASE, we don’t put the container image on Docker Hub – we use a private Azure Container Registry (ACR). This ACR offers several advantages:

  • Georeplication: replication of the registry across multiple Azure regions
  • Images are stored close to your infrastructure, which means less network traffic costs
  • Azure ID authentication

Private registries are fully compatible with the existing Docker tools, so developers can continue using their usual toolkit.

4. Conclusion

We’ve discussed how you as a developer can build a container image and store it in a container registry. You can build, test and upload the container image using its own workstation. If you want to put the application into production, you’ll need to use Kubernetes and CI/CD (Continuous Integration/Continuous Deployment).

Ready for the next step? In the next blog posts, we’ll explain how to Azure Kubernetes Service (AKS) to put containers into production and we’ll talk about Azure DevOps as a CI/CD solution.

Share this blogpost

Leave a reply

Your email address will not be published. Required fields are marked.