Using containers

Another option is to use a container. A container is an operative system-level virtualization mechanism, where you isolate an environment inside your operative system. You still need the space for a base layout, but there are container layouts under 5 MB.

The most common and widely used container environment is Docker, which uses Linux containers and cgroups to create a secure and isolated environment to run applications. What boosted Docker actually was was the centralized repository of base layouts, called Docker images, where people could define, build, and then share these images with others, so people wouldn't have to build them themselves. There are pre-built images for all major database services and programming languages, so it's easy to just choose one and run your microservice inside it in a couple of minutes.

To make things even better, there's a description file, called Dockerfile, which has all the instructions you need to build an image locally. This is a text file that you can store in your code repository that allows developers to build an image to run the environment.

Even more interesting is the fact that images can be built on top of other images. You can pick one, add your specifications, and publish it. This is one of the major advantages that drove Docker to be the most used container technology.

There are a couple of Docker idiosyncrasies that you must understand before using it:

  • Docker images are like a read-only template for running your application. There are ways of saving changes to images, but usually you pick an image and run your application inside an environment that will get lost when the container is removed. This is actually great because it ensures that when you restart your environment, everything is back to a common environment and nothing is left over.
  • A container should run only one command and nothing more. This command can, of course, spawn other commands, but the purpose of a container is not to run multiple commands in parallel. If you have a complex environment, each service should run in its own container. For example, if you have a database, the database service could run in one container and your application should run on another.
  • Because an image is supposed to be read-only, you can mount or link folders from the outside of the container. These are called volumes and allow you to, for example, run a read-only MySQL image with the data folder mapped to a folder on your laptop, allowing you to avoid losing your data when you stop the container. Volumes can either be folders or files and they can also be mounted read-only from the inside of the container.
  • A container runs in an isolated environment and you need to create network links from your interface ports to the container ports. There's also an option to create virtualized networks that work across hosts and give you the power to deploy more complex environments.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset