Getting started with Docker
Tags: devops, docker, beginners, backend
Published: 2023-08-07 17:09:18

Managing application dependencies, ensuring consistent environments, and deploying applications can be a nightmare.

## What we will cover.
1. What is docker.
2. Virtual machine vs docker.
3. Docker Architecture.
4. Docker commands.

## 1. What is docker
Docker is a set of services that uses the operating system virtualization to package an application with all necessary dependencies and configuration.
Simply we can say that docker is a container that contains application dependencies.

## 2. Virtual machine vs docker
Here we will get to understand how a virtual machine compares to docker.

A virtual machine has no significant difference from a physical computer but simply, it is a computer resource that virtualizes the application and operating system kernel.

Both docker an virtual machines have the concept virtualization.

To better understand how docker compares to a virtual machine we will use the illustration below.
![Virtual Machine and docker comparison](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ktnranrf9cbve768ad3.png)

From the illustration we can see that a virtual machine virtualizes the application and operating system layer while docker only virtualizes the application layer.

Other ways that the virtual machine compares to docker include:

| Docker     | Virtual machine |
|------------|----------|
| Occupies less memory since they have only the application layer.        | Occupies lots of memory since they have both the application and operating system kernel layer.     |
| User the host kernel because it only virtualizes the application layer.      | Uses its own kernel because it contains the application and operating system kernel.    |
| They run faster since they run as processes of the host machine.      | They are significantly slower compared to docker container because they involve booting up an entire operating system which can take a lot of time.    |

## 3. Docker Architecture

Docker primarily uses a client server architecture. The architecture consists of a Docker client, Docker host and a Docker registry. The docker client communicates directly to the docker daemon via a REST API over unix socket, where the docker daemon is responsible for building, running and distributing your Docker containers. This is illustrated via the image shown below.


![Docker architecture image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnzviq7jpzstba090xlw.png)

### Docker client

This is the primary way through which that Docker users interact with docker through the use of docker commands such as `docker pull`. The client sends the commands to Docker daemon via the Docker Rest API.

### Docker daemon

This is responsible listening the Docker Rest API requests and also responsible for managing docker images, containers, networks and volumes.

### Images

This is a template for creating docker containers. For an image you can create your own image or use images created by others in `Docker hub` which is an image repository.

To create your own image you use a `Dockerfile` with steps defined for creating an image. Each set of instruction in a `Dockerfile` creates a layer in an image, such that when you rebuild the image only those layers changed are rebuilt. This makes docker images fast, lightweight compared to other virtualization technologies.

### Container

This is simply an executable instance of an image. A container can be created, started, stopped, moved and deleted using Docker CLI.

### Docker registry

This is the repository for images. The primary public repository for docker images is `Docker Hub`.

## 4. Docker commands

To be able follow this section I advise you to first install docker desktop on your local computers.

>To install docker for linux [click here](https://docs.docker.com/desktop/install/linux-install/)

> To install for windows [click here](https://docs.docker.com/desktop/install/windows-install/)

> To install for macOS [click here](https://docs.docker.com/desktop/install/mac-install/)

#### I. Docker pull

This commands pulls or downloads an image from the docker registry.

To test this command, assuming you have docker installed, open your terminal or command prompt and run the following command. 

```docker
docker pull mongo
```

You will see that the mongo images starts downloading. The mongo image is pulled from the docker registry which by default is `Docker Hub`

![Docker pull command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqgoejx2fdaxfa7awc4x.png)

To verify that the container has been successfully pulled, you can open docker desktop and click the Images tab and this is what you will see.


![Docker desktop images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xndqghw7f5oongkaz6fe.png)

#### II. Docker run

To run the container enter the following command in your terminal or command prompt.

```
docker run mongo
```

>This commands runs the mongo image and creates an executable instance of the mongo image which is now referred to as the container.

This is what you will see in your command prompt.

![Docker run command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5znbl2n69clvn2a34xu.png)

Also you can run `docker run -d mongo` where this command runs the container in detach mode. This means that the logs will now be printed in the standard output.

This is what you will see when you run the image in detach mode.

![docker run command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srn77322ae4yiaamfeso.png)

#### III. Docker ps

This commands prints out to the standard output the details of the containers that are currently running. This includes the container ID, Image, the time the container was created, ports the container is running on and the container name.

![docker ps command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47eswdbhkoig51twisj3.png)

## Conclusion

Docker has changed the way we develop and deploy applications. By containerizing our application we ensure flexibility, efficiency and evade the phrase _"it works on my machine"_. 

Those knew to docker, don't be afraid to dive deeper and experiment new things because mastery comes with practice.