What on earth is docker anyway? 🤔
Read the entire post patiently to get the most out of it
What is docker?
Keeping it really simple here, docker is a program that runs operating systems as a process on the host operating system. Unlike a virtual machine(VMWare, VirtualBox) it does not need any dedicated hardware or software from the host. It is simply a process that can be seen running in the system's task manager or system monitor.
Terms associated with docker
Image: Have you ever dual booted your computers or installed an OS on any system? If not it can be done by downloading images. Consider you want to install Ubuntu on a computer, Ubuntu's image can be downloaded from their official website, put inside a bootable USB and fired up. Similarly, in docker, some images are cut down versions of the original operating systems having only the specific program environment or library installed. For example: Consider Python Image. Docker hub is an official repository that houses a ton of images, be it python, ruby, PostgreSQL, node, etc. These images can be found in various versions. For eg: python:alpine is an alpine Linux image having python installed inside of it. Click this URL to take a look at that image on the docker hub.
Container: Now that you've chosen the image, you'll need something to use or to run it. This is when containers come in. Every image that you run using docker is running inside of a container. In other words, Docker creates a container (a process that we discussed in the first paragraph) and run the image inside it.
Dockerfile: Consider you've created a node.js project. For example, A web application that has multiple files and folders and you need a certain set of commands to fire it up like "npm migrate", "npm start 3000", etc. You decide to containerize this application using docker. It will need the following steps inside of that container:
- Get the node image from docker
- Copy your project inside the container
- Expose the port from inside of your container to the host. (In the above case 3000)
- Run the migrations
- Finally, spin up the server
All these instructions above which are written in plain English are written inside of a Dockerfile following a certain format which can be found in the docs.
Docker-Compose: Assume you have a very big project consisting of 3 services the front end, the back end and the database, that is there are 3 servers that need to be fired up and communicate with each other. Also, there are 3 separate folders for all the three services that you have. Following the Dockerfile convention create 3 docker files in the respective folders having the desired commands to start that particular service. Now you could run 3 separate commands to run 3 different containers. To save you from all this hassle we have something called Docker-compose that takes care of all the folders and can be created outside these folders. In the Docker-compose (.yml file) we can specify the paths to the Dockerfiles of the services or we can write the entire Dockerfiles inside it. Then simply run a single command that will fire up all the three services together flawlessly.
I am planning to write down a hands-on tutorial on how to dockerize an entire full-stack application and make it production-ready. This project would have the back end written in Django, the front end in React.js, the database will PostgreSQL and the webserver will be Nginx. All these will run inside of their own containers and communicate with each other using the REST API interface that docker has.
Now why run my application using docker over conventional development setup you may ask?
Have you ever faced an issue with your testers, developers, colleagues saying "That's strange, it was working on my machine? Maybe it's a dependency issue." Well,. docker to the rescue. Using the docker approach you pack everything into a container and then ship those containers over prod servers, testers and your developers. Docker will make your and your team's life easy enabling you to focus more on development rather than setting things up. Set only once, run anywhere. Sooo simple!
Stay tuned for this post. See you next time!