Containing it all! – What Docker is for?

Docker is an open source tool that became pretty hot in IT world recently. There is a good reason for it – it is a really good tool! In this post, I will explain you what containers are all about and that they are not such dark magic as they seem.

Container is an isolated part of your computer’s resources, where you can run separate environment, like software and services, for your own project.

Sounds complicated still? Let me further break this down with an example.

Imagine you are working on a modern app using the newest available technologies. For this purpose you upgraded all your software, libraries, IDEs and what not to support your new fantastic project. However, you just realised there is still this legacy project you have to keep maintaining for you customer for some period of time. Rewriting this project using new technologies is not feasible for you. Conflicts will arise on your computer once you have two different versions of the same compilers and frameworks. What to do?

Contain it!

We are containing software, not cats!
Source: http://i.pinimg.com/736x/b8/e6/f5/b8e6f54e24379cd890ee0fd93321e7b8.jpg

Instead of installing the frameworks on your computer and try to keep them in order (you would lose much time for that and time is money, right?), create two isolated environments: one with modern frameworks and other with legacy stack. Each time you need to work on one or another, you can easily switch between them with a single command. No need to think about conflicts, separate locations and what not! Sounds good?

Then install Docker Community Edition for your platform from https://store.docker.com!

Now let us check if it works by opening your favourite terminal or command prompt and typing in (keep your computer online!):
docker run hello-world
Note: Root privileges might be required in Linux and Mac OS X environments. If this is the case, put sudo in front of each command, or first execute su - to switch to root user, if sudo command is not installed on your system (case for e.g. Fedora and plain Debian). User password or root password will be required in majority of cases.

You should get hello message back from Docker. Got it? Mission accomplished!

What does the command do?

run implies creating new container, while hello-world is the base image for your container that (in this case) will be pulled from Docker Hub.

Now type in docker ps -a to see the list of all available containers. By removing parameter -a, only currently running containers will be displayed.

You might have noticed your container has been assigned a funny name. Docker does it for you if you do not specify your custom name yourself. More about how this assignment takes place here (an easter egg involved). You can give name to your container yourself, by adding parameter --name |container name goes here| to docker run command.

As we are going to get our server running, it would be preferable to connect to it from outside the container. In order to do this, container’s ports need to be opened with parameter -p |host port|:|container port| added to docker run command.

Since we will be developing our app on the host machine and just testing it within Docker, parameter -v will prove useful. It works as following: -v |host location|:|target location| and allows for sharing given directory between host and container, reducing the need to copy your project each time inside the container for test purposes.

Last but not least, we will need to connect (or “attach”, in the Docker’s language) to our container and execute certain operations from within. You need to imply that when creating the container with adding -it parameter (it enables interactive mode) and adding location of shell binary to the end of the command. Do not worry -it is not as complicated as it seems, but I will get to that in more detail later!

Once interactive mode is enabled, you get attached to the container straightaway (you can prevent that by adding –detach parameter). You can leave the shell within the container by pressing Ctrl+p+q and return to it by executing docker attach |container name|.

Other useful commands are quite straightforward:
docker start |container name|
docker stop |container name|
docker pause |container name|
docker unpause |container name|
docker remove |container name|

These are the basics of Docker. This introduction already gives you enough knowledge to develop your software neatly using the power of containers! In the next post, we will apply this know-how and actually start developing back-end of our app with MongoDB, Express and node.js.


Also published on Medium.

Leave a Reply

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