Docker – the way to simplify your life.

0
1122
views

As you may know, I am working on a web project.
To make it more portable and host independent I decided to use tool that is one of the most popular GitHub project – Docker.
What is it? When it could be helpfull? How to use it?
In this post I will answer this questions, but I won’t dive deep into details.
My intention is to show benefits of this tool for person who hasn’t deal with this tool before.

Docker – what is it?

This tool designed to help you unify environment on which you run applications.
Following Docker platform official site “developers use Docker to eliminate ‘works on my machine’ problems when collaborating on code with co-workers”.
To do so it uses containers – isolated part of system which provides everything what is needed to run application.
It is much lighter than virtual machine due to lack of operating system.
Containers are build from Dockerimage files, which provides reproducibility.

When to use Docker?

This tool is very usefull for people who works on the same codebase.
Everyones computer can have slightly different configuration because someone tried new version of library and forget to switch to previous version or  forgot to download some update, etc. And  there is a problem: “Why this code doesn’t work?”.

Most of the times it is a little detail which could be hard to detect and fix.
And here Docker shows its power – everyone has exacly the same configuration – the same OS, libraries, server application, so application runs always in the same environment.

Docker is also usefull when you want to experiment with different versions of library or database engine without messing host system.

For better understanding let me tell you a story. About two months ago I was during my work on BSc Thesis. Project was finnished there was only report needed final touch.
In the same time I decided to try React Native – library for creating mobile apps in JavaScript. To create development environment I was forced to install some software.
Just before deadline I wanted to add some screenshot from project web application. So I tried to build my project, and… error ocured – some libraries didn’t work properly.
Trying another time… the same situation. I strugled to find solution in internet, but nothing worked. Finally I decided to try build it on another computer.
This situation cost me a lot of time, effort and stress.

But if I knew how to use docker it would take only a few seconds to run container which, would build and host my web application.

How to use Docker?

If you don’t work on Windows 10 education or enterprise edition – Linux, you need to download Docker Toolbox, which is avaliable here
If you are owner of one of Windows version listed above you can use this link to download Docker.

To indicate if instalation was successful run in command line:

In command line should appear inscription: “Hello from Docker!”.
Actually, you’ve just pulled image from Docker Hub (place where you can put Docker images), builded container, and ran it.

Lets create our first application which will be hosted inside container.
I will create default web API application in .Net Core in src folder by three simple commands:

Then we need to specify how our container should look like.
To do so we are going to create file called Dockerfile:

Then add following commands inside this file:

FROM keyword specifies base image, which we can customize – in this example we don’t want to spend much time to create image from scratch, so we are using image created by Microsoft.
WORKDIR /app
WORKDIR – working directory – specifies directory inside which we are going to operate.
COPY – copies files from src directory to working directory.
ENV – changes environment variable so our application will listen on port 5050
EXPOSE – shows specified port outside container.
RUN – specifies commands to run when creating new image – often used to install some libraries, in our case we are going to restore all project dependencies.
CMD – specifies default actions after running container.

We have our Dockerfile. It’s time to build our first image:

By using -t sample-api we are tagging our image by name. Dot at the end of the command specifies that Docker should look for the Dockerfile in current directory.

Finally it’s time to run our container:

To use ports exposed by container to localhost port i used option -p 8080:5050.
Now you can go to http://localhost:8080/api/values and see results.

Docker is very powerfull tool and in this sample we haven’t used even 1% of it’s potential.
If you are intrested in Docker and want to learn more I recomend official documentation which contains tutorials and samples and this video tutorial:
https://www.youtube.com/watch?v=YFl2mCHdv24&t=1s

LEAVE A REPLY

Please enter your comment!
Please enter your name here