Cobus, one of my good friends asked me to do a tutorial on Docker, and after spending the last 3 months playing around with Docker, I am excited to share my journey with the world.
In this guide I will give an overview on Docker, and show you how to Install Docker on Ubuntu 16, then we will run a container to test that our setup is working.
To keep things short, I will break up the posts into the following:
So lets kick back a notch:
What is Docker?
In my own words, Docker allows you to have applications hosted in the form of containers, which is very lightweight, and makes it easy to build, test and deploy your applications in a faster way than comparing with traditional virtualization.
But there is MUCH more than that, to add some context to that:
Docker is a Open Source Technology that allows you to create lightweight, isolated, reproducible application instances which is called Containers. Docker is built on top of the LXC technology, so it uses Linux Containers and as mentioned, it's lightweight compared to a traditional VM. And the awesome thing is, you can run Docker from Windows, Mac and Linux, which makes it a great way to build, test, deploy reproducible application environments.
A Container is isolated and uses the Kernel of the Docker host, it also utilizes Kernel features such as cgroups and namespaces in order to make them isolated. So when you are in a container, it feels just like a VM, except of the overhead of the guest operating system.
Comparison: VM vs Container:
In this diagram we can see the difference:
Have a look at: Containers are Not VMs for more info
How does it work?
Docker uses a image based deployment model.
A Docker Image is a lightweight, executable package that includes everything from libraries, environment variables, etc that is needed in order to be instantiated so that a container can be executed.
For example, when you pull down a base image like "ubuntu 16.04", which is more or less 130MB, once you have the image in your local cache, you can then create a runtime instance from that image, which we call a container.
You also get much smaller images like Busybox which is about
1.11MB or Alpine which is about
So upon entering the shell of a ubuntu container, you will see all processes that is running is:
$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 1.7 0.0 18252 3352 ? Ss 13:36 0:00 bash root 11 0.0 0.0 34428 2936 ? R+ 13:36 0:00 ps aux
So at this moment in time, its a basic "ubuntu 16.04" container, you can now do some work on your container, make some changes, then you can commit your changes to the image that is in your local cache, with a identifier that we call a tag for example:
So the next time when you create a container from the image
mycontainer:v01, you will see that the container is exactly like you configured it from the previous step. So this really speeds up deployment of launching containers.
Let's get Started:
I learn from examples, so let's get started on setting up Docker.
I will be using Ubuntu 16.04 in this demonstration. Remove any older versions of Docker that might be present and install the dependencies:
$ sudo apt-get remove docker docker-engine -y $ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual -y $ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
Get the needed repository to setup Docker Community Edition:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - apt-key fingerprint 0EBFCD88 $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the repository index and Install Docker Community Edition:
$ sudo apt-get update $ sudo apt-get install docker-ce -y
Enable Docker on Startup and Start the Docker Engine:
$ sudo systemctl enable docker $ sudo systemctl restart docker
If you would like to run docker commands without sudo:
$ sudo usermod -aG docker <your-username>
Setup Docker Compose
curl -L https://github.com/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
Test your Setup by Running a Hello World Container:
You will see that if the image is not on the local docker host, it will pull the image from docker hub, then once the image is saved locally, docker will then create the container from that image:
$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 78445dd45222: Pull complete Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
Yeah! Docker is working like a champ!
Next up, we will go through some examples on using Docker