Quick pills

Dockerizing the world

8 years ago
3 min read

Have you switched to Docker yet? Well, my short introduction (masked as a 5 minutes tutorial) could be your chance to learn how to properly move forward and start using containers the easy way. Docker is an open platform to build and ship your application as standardized units, called containers. Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.

1. Instruct your container

Containers are built upon an image and a set of instructions you tell Docker using a file called Dockerfile. Here below you can see a full example of how to build a Node.js app as a container.

p.s. the above doesn't cache "npm install" between builds like it should, but I decided to keep it for a second tutorial as you'll need quite a few tweaks to make it work.

We basically tell Docker "hey, grab the latest nodejs image — which includes a ubuntu + nodejs set up — then add my working folder in the container filesystem, install npm dependencies, expose port 80 to the outside and then run 'npm start' to start the server". Simple, right?

2. From image to container

— in 20 seconds, as building your Dockerfile to a container and spinning it up is dead simple and requires just two lines of code.

I believe the above is pretty much self-explanatory — but just in case I've lost you — it's telling docker "hey, build this image as a container and run it binding port 80 of the container to the port 80 of my docker machine" (which is a VM or sort-of installed on your computer).

Benefits of using containers for your applications are dozens, but among them I'd say "software will always run the same, regardless of its environment" and being able to deploy and scale your applications on literally any major platform out there (e.g. AWS, Google Kubernetes).

3. Further considerations

Docker-powered containers are also much better than traditional VMs like Vagrant as they differ really much in terms of kernel. The great thing about Docker is that it is light-weight (because it relies on shared-kernel linux containers) and it is distribution agnostic. 

Docker gives you a very lightweight virtualisation with close-to zero overhead. The effect of this delivers some impactful advantages. Primarily, you can benefit from an extra layer of abstraction offered by Docker without having to worry about the overhead. The next significant advantage is that you can have many more containers running on a single machine than you can with virtualization alone. You also want your development environment to mimic as much as possible production one, and that's where Docker gives its best! Not to mention developers can spin up their machines in literally 5 minutes and don't have to worry about dependencies, devops, etc.

Now, this has been just a very brief introduction but next week we'll dig more into Docker containers and talk about app isolation, debugging capabilities, multi-container applications with docker-compose and much more — so stay tuned, and come back in a while!