A Beginner’s Guide to Docker
Told from someone who has gone through the struggle himself.
Hey there! My name is Numan and I am a working student for the company itdesign based in Tübingen, Germany.
I wrote this tutorial so you guys can make some sense of Docker. When I first got into Docker about a year ago, I found it relatively difficult to understand why you need it and what was actually happening.
If you still have any question (meaning this tutorial didn’t help you), ask me (numan.takkac@itdesign.de) or ask the internet (official Docker page or Stackoverflow).
DISCLAIMER: All of this information is all that I personally know about Docker (which means it’s probably missing some stuff) and I take no responsibility for any future damages that will occur because people followed this guide! (Please don’t destroy your software 😂)
Docker
So let me introduce you to the topic with the following statement: Docker is really cool and you should really start using it whenever you can.
Why?
Well Docker is kind of a lightweight VM. It isolates a process in a restricted environment but e.g. doesn’t actually virtualize the hardware layer (vcpu, mem, etc.).
Allright, thanks for that info…
Ok, ok, that explanation won’t do, I guess. 😁 For me the thing that was hard to grasp was, that a docker container is similar in spirit to a VM. Docker does not use the classic images which we know (ISOs etc.), however, but instead the image is derived from the so called Dockerfiles. Today, it even serves as a standard format to ship production-ready software — all you need is the image, some configuration and a container execution platform to run it on.
Dockerfile
The Dockerfile is just a recipe on how Docker should create your image. Instead of the usual fixed image which you got for Ubuntu etc. you instead can now define your image all yourself!
Example
Imagine you want to have a docker contains which contains the actual Ubuntu userland packages. What you would do with Docker would be to either write a Dockerfile all by yourself or you use the well-known resource Docker Hub. On Docker Hub you can find pre-compiled, oftentimes professionally curated, images for literally ALL kind of technologies. In our Ubuntu example, we would search for Ubuntu and take the image with the “latest” tag.
To import Ubuntu as a docker image you would simply create a Dockerfile (naming the docker file “Dockerfile” is the current convention) and write
FROM ubuntu:latest
<Rest of your docker stuff>
When you now build this Dockerfile (will be explained later) you will have an Ubuntu base image on which you can work on. The Ubuntu images occupy (depending on the version you are using) around 30MB of disk space. In contrast to the 2–4 GB you need to run a classic image with a VM!
At this point Docker is already better than VMs in the customization aspect as well as in the performance aspect.
But, how do I get all of this running now?
Good question! The explanation of the Dockerfile might not be sufficient and to be honest, there is some work that has to be done in the console aswell (since all the operations happen here). If you want to use Docker on a Windows machine, you can download Docker for Windows.
You can use Docker on literally any other OS. I am using Windows because first of all, I do not care about the OS I use and second of all because Windows is easy to use and I have used Windows all my life. Deal with it 😎
K, and why do I care about all this Docker stuff?
Well, because containerization has pretty much changed the way how applications are shipped and run today. It has made quite a lot of aspects (e.g. segregating incompatible application dependencies from each others) way easier and certainly won’t go away anytime soon. And also, because now you can have an Ubuntu machine running on your PC which uses 50MB instead of 2GB. This means you can have many Docker images running simultaneously on low resource demand!
And, how?
Well this part would be pretty technical. AFAIK it is running on your OS kernel which makes all of the stuff less resource demanding. In contrast, a VM is actually emulating the kernel of the VM image and thus ACTUALLY needs to have the whole OS on your computer. If you want to read more about it, I think this will help you.
The intention of this blog post is to explain you everything that I need in my daily work. 😁
Thanks and all, but right now this still does not make any sense to me…
Allright, allright. Let’s try to get an application on Docker working. Then you maybe will get the hang of it. We will try the example nginx webserver here. Just pull the repository and you should have a folder structure like this:
If we now take a look at the contents of the “nginx-hello” folder, we can see the following repository structure:
Let me explain all of the contents.
Dockerfile
FROM nginx:mainline-alpine
RUN rm /etc/nginx/conf.d/*
ADD hello.conf /etc/nginx/conf.d/
ADD index.html /usr/share/nginx/html/
As you can see the docker file is importing the image with the tag “mainline-alpine” from the nginx repo on Docker Hub. Here we can already see one of the big advantages of Docker: we can add custom files, create custom configs and can even copy things into the Docker image. We do not need to add the files after starting the VM, we can start the VM with the files needed!
Here we are replacing configuration files of the nginx server which are there by default. We also put in our index.html to the corresponding places.
hello.conf, hello.png
These files are either replaced or even just added in the docker image.
index.html
Same as the hello.conf and hello.png file.
As you can see Docker has pre-defined commands, e.g. ADD, RUN, COPY, FROM etc. The functionality of these commands is well documented in the docker website. I recommend you to have an own cheat sheet where you write all of the relevant commands for you and how you would use them! All the code snippets below are also on my own cheat sheet 😊
Now we need to install Docker for our OS. For Windows I already provided one before. If you have any problems with the install, you can simply ask the internet, many people probably had the same problem as you have!
After installing Docker, we open up our command line and enter the command
docker build -t nginx_example .
And you will (hopefully) see the following output:
It is important that the console is in the same folder as the Dockerfile. There are ways to give a path to the Dockerfile, but why bother, when you can just start the console on the relevant place 😋
The docker build command executes the Dockerfile and generates our custom image! The parameter -t just gives our image a tag. If we don’t provide this our container is going to have its image ID. These IDs are a bunch of letters and numbers and if you have multiple containers on your machine, you will definitely lose track of your images. Thuuuus, we provide a tag.
If you want to see if your image actually exists, just enter the following command:
docker images
You will see something like this:
As you can see: Our image only has a size of 22,6 MB! Imagine setting up an Ubuntu image! It would take you much more time and also much more ressources.
Now… Since we built our docker container (docker container is literally the same as an image, container is used when it is actually runnable) we can now run it with the following command:
docker run -p 8080:80 -d nginx_example
And you should see a random hash as output. Now our container is running! You will be able to connect to our webserver at the URL localhost:8080. The connected page looks like this:
Now, if you would like to customize this application you would write your own HTMLs, PHPs whatever really and just put it into the Docker container through the Dockerfile.
Conclusion?
You have seen that deploying an application with docker is pretty easy and simple. The exact use case can not be generalized. You could dockerize everything, every application that exists. If it needs to be scalable, Docker is a great start.
This whole Docker thing makes much more sense if you get to know tools like Kubernetes. Kubernetes builds whole environments around docker containers (pods), so that you can actually scale a whole ecosystem around your application. More about this later!
Last but not least 😀
If you want, also check out the company I work for (itdesign):
itdesign GmbH — Software und Beratung aus einer Hand
One of our own products, check it out:
Meisterplan: Planen Sie Projektportfolios und Ressourcen in der Cloud
Werden Sie als Unternehmen agiler Treffen Sie mit den drei As innerhalb eines Monats bessere Entscheidungen. Werden Sie…
If you ever want to apply but don’t know what for, itdesign has this cool feature of you being able to request a programming task and submitting it without writing a full CV etc: