Why containers?
As developers, to make programs work we need other programs and therefore we need to make those programs run too. When I just started learning about programming the process generally starts with me installing the necessary dependencies before a single line of code is written. As time went by, when I started exploring the mythical land that is known as ‘production’, it became clear that just installing dependencies isn’t enough. What if we need to start a new project and we have to use the latest technologies? What about setting up a development environment on Windows instead of macOS?
Don’t worry, containers come to the rescue. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Containers are built to include everything needed to run an application: code, runtime, system tools, system libraries and settings.
Docker for Containers and how it works
Docker is a platform that allows developers to easily create, deploy, and run applications in containers. It uses two concepts that exists on most operating systems (OS) to achieve containerization.
Namespaces
Namespacing is an OS feature that allows segmentation of hardware resources on a computer. A running process (or group of processes) may communicate with any segmented hardware via Kernel directing those communications to a specific namespace.
Control groups
Control groups (cgroups) is an OS feature that limits how much resources a process may use. The limit may be applied to various hardware such as memory, CPU usage, hard disk space and more.
Namespacing and cgroups facilitates the creation of a container which is just a set of processes with a grouping of limited resources assigned.
Creating a container with Docker
To create a container for an application we first have to setup an image, which is a directory snapshot for our application. The instructions for creating a docker image is contained within the Dockerfile
as shown below.FROM node:alpine
COPY . /app
WORKDIR /app
CMD ["npm", "start"]
The above example will build an docker image from a base Node image, and then copy the current directory from where the Dockerfile
is situated into an /app
directory within the container. Lastly it specifies a startup command npm start
for kicking of your applications Node process.
To build the docker image you would run the following command in the directory where the Dockerfile
is situated:docker build -t <image-name> .
The above command will create the docker image from which a container may be created. To start running a docker container based on a specific image, you should run the following command:docker run -it <image-name>
The above command will start running a container with your node process kicked off.
Conclusion
In this article we discussed how docker containers work under the hood and also provided a quick example of how to create a docker container by first setting up a image. I hope this helps you to understand the technology and its uses a bit better!
About the author
Xiao Ming (Mason) Hu is the Lead Engineer of Pre-production at Syft. Mason has a Masters degree in Electrical Engineering. He loves expanding his knowledge and is into fitness and cryptocurrencies.