Photo by Rubaitul Azad on Unsplash
Docker 101
A beginner friendly tutorial on Docker and some basic Docker concepts.
Table of contents
Docker is something you might have heard about at one point or the other in your development journey as a software developer. In this tutorial, we will be going over: what is docker, what are containers, why we use containers, how docker processes and structures applications and how to install docker on your machine.
Prerequisites
To follow up on this tutorial, you will need:
Ability to use CLI(Command Line Interface).
A bit of programming experience.
What is Docker
We must understand what it is we are learning, and this section is dedicated to that. Docker is a platform for building, running and deploying applications consistently, so if your applications work on your development machine, using docker will make it work the same way on any other machine. Docker is written in Go programming language and it makes use of the Linux kernel for its functionality. One of the ideas behind docker is that it creates a new isolated environment called containers(container is explained in the next section) for each application.
Containers
A container is an isolated environment created by docker for each application. This container will package up the application and all its dependencies We can create, start, stop or delete a container using the CLI. One of the advantages of containers in docker is that it packs in everything the applications need -like code, dependencies etc.- and it stands alone and is also lightweight. Think of a container as a one-room apartment that contains everything an individual needs to function in the room, docker can create multiple of these one-room apartments for different individuals, the individuals in this case will be the applications. Applications are safe in containers due to the isolation of each container. Let's state some advantages of containers:
It is isolated.
It is lightweight.
All containers share the operating system of the machine( no need for some special operating system).
It gets the application to start up quickly.
It does not need a lot of hardware like memory, disk space etc.
Images
We have briefly described containers above but these containers are built with some instructions called docker images. A docker image is a file used to execute code in a docker container these images, are like a template that contains the instructions to build the containers. The images we use to create the containers are read-only files but as soon as the container is created a writable layer is added that allows a user to make some changes. A docker image can be described as a containerized application and one advantage of docker images is that it's a reusable asset and can be deployed to a host -host like docker hub, which we will touch on later- so developers can use an already created image in their project.
Docker image vs Docker container
For clarity let's differentiate between Docker images and Docker containers:
The major difference between a Docker image and a Docker container is that a Docker image is an unchangeable template that instructs how a container will be created while a Docker container is an environment created based on the instructions of the Docker image.
Docker images can exist in isolation but Containers can't exist without the images.
A Docker image can't be started or paused but a Docker container can be started or paused.
Why use docker
You might have experienced some situations, where your application worked well on your development machine but when you tried it on some other machine it wasn't the same; this can be caused by either of the following:
If one or more files are missing when the application was deployed i.e the application was not deployed fully.
If the other machine is running some different version of software the application needs etc.
When we use docker we can package our application with everything it needs -let's say our application needs some packages to function properly -and we can run it smoothly on any other machine that runs docker.
Docker Architecture
Docker uses a client-server architecture, the client side communicates with the server side using REST API. The server side is also called the Docker engine. The Docker client(which is the client side of the architecture) communicates with the Docker daemon(which is on the server side of the architecture) which is responsible for building, running and distributing docker containers.
Let's briefly explain some parts of the architecture.
Docker daemon
The Docker daemon(server side of the architecture) is actively listening to the Docker API requests and it also manages Docker images and containers.
Docker client
The Docker client (docker
) is the fundamental way that the users interact with Docker when we run some commands like, this will send the commands to the Docker daemon which will carry out the command.
Docker Desktop
The Docker Desktop is an application that can be installed on Windows, Linux or Mac OS machines this will help us with the Docker process on both the server and client side. The Docker Desktop contains the Docker daemon and the Docker client.
Docker registries
We can store Docker images in a public Docker registry. Docker hub is a public Docker registry- it's like Github which a lot of developers are familiar with- on Docker hub, we don't push codes instead we store Docker images that can be used by other developers. Similar to GitHub we can push images to the Docker hub which can be used by another developer and we can also pull images already stored on the Docker hub(we will see how this is done later on).
A visual representation of Docker architecture:
Installing Docker
I am using windows but I think the steps are similar on other OS. At this stage, if you don't have Docker installed on your system you can download it here - check the system requirements before downloading, to make sure your machine meets those requirements-. After the download is complete, you find "Docker Desktop" from your downloads list and then you run as an administrator to begin the installation process. Now search for windows features on your machine and enable "Hyper-V" and "Containers" features -tick the box of both Hyper-V and Containers-. You might get an error like this:
Click on the link in the error message, it will provide you instructions on how to resolve the error after you have followed the instructions, restart your machine. Open your command line and run the command docker version
you should get something like this if the installation was successful:
If the server part is not showing like it is above then the installation was not successful, but don't worry we can still solve the problem. If you don't see the server part after running docker version
it is most likely because the Docker daemon is not running so follow the solution provided here after following the steps, restart your pc. Now click on the Docker Desktop icon to start Docker, you might get stuck on "Docker Engine starting" forever like this:
The image above shows the Docker Desktop getting stuck on "Docker Engine starting..." the solution to this problem is provided by a channel on YouTube called '"GEEKrar" check it out below.
After following all the instructions, restart your machine and run docker version
on your command line you should be getting both the client and server information as shown in one of the images above. If you encounter any other form of error that wasn't discussed here, you can google it I'm sure you will find solutions.
Conclusion
We now understand what docker is, how it's structured and how to install it on your machine. I will like to sound one warning at this point, before trying to install Docker on your machine make sure that your machine's specifications are good enough to use Docker if not you will encounter a lot of issues- I experienced this personally- so please check if your machine can handle it. Working with docker on real-life projects is beyond the scope of this tutorial- due to some issues- you can check here for more insights.
"Genkai wo Koero" - Surpass Your Limits.