Getting Started with Docker
Published: 2022-03-18 05:28:46

**What is Docker?**

Docker is a sophisticated platform that enables IT teams to effortlessly design, test, and deploy programmes in Docker containers, complete with all of its dependencies. In a Dockerfile, a developer defines all of the programmes and their dependencies, which are then used to create Docker images, which define a Docker container. Docker can be utilised at several points of the DevOps cycle, but it excels in the deployment stage. It is more powerful than Virtual Machines and has additional features that developers would appreciate.

**Advantages of using Docker**

Docker can speed up the delivery of your code and provide you more control over your applications. You can run programmes in containers, which makes it easier to deploy, scale, rollback, and troubleshoot difficulties. It also aids in cost-cutting by maximising resources. Docker-based applications may be transferred from local development to production deployments with ease. Microservices, Data Processing, Continuous Integration and Delivery, and Containers as a Service are all possible with Docker.

**How does Docker work?**

Docker creates, manages, and operates containers. The operating system provides access to container technology: A container contains all of the libraries, configuration files, dependencies, and other components and parameters required for the application service or function to work. One underlying operating system is shared by all containers. Containers that travel between Docker environments with the same OS run without changes since Docker images contain all of the dependencies needed to execute code within a container. 

To run several containers on the same OS, Docker leverages resource isolation in the OS kernel. Virtual machines (VMs), on the other hand, enclose a whole operating system with executable code on top of an abstraction layer of physical hardware resources.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szuqo9puwxf6rdwqzz2h.jpg)

**Docker Architecture**

Docker is built on a client-server model. The Docker client communicates with the Docker daemon, which handles the construction, execution, and distribution of your Docker containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ty5qot8gsq2ud2p5fvpg.png)

_Docker Daeomon_

It handles Docker objects such as images, containers, networks, and volumes by listening to API calls made through the Docker client.

_Docker Client_

This is how you communicate with Docker. When you use docker to perform a command, the client delivers the command to the daemon, which executes it. The Docker client has the ability to communicate with many daemons.

_Docker Registries_

This is where Docker images are stored. Docker Hub is a public registry that anyone can use. When you pull an image, Docker by default looks for it in the public registry and saves the image on your local system on DOCKER_HOST. You can also store images on your local machine or push them to the public registry.

_Dockerfile_

This article explains how to make a Docker image. It's similar to a recipe, with all of the ingredients and processes you'll need to make your cuisine. You can use this file to make a Docker image. In any setting, these images can be used to construct containers. These images can also be saved to docker hubs online. Docker containers are created when you run docker image. The programme will be in the container, along with all of its dependencies.

**Containers**

A container is an image that can be run. This is the location where your application runs. The Docker API or CLI can be used to manage containers. You can attach storage to a container, connect it to one or more networks, or even construct a new image based on its existing state. Data will be lost if we delete a container! Because the last layer was produced as a new layer when the container fell down and then brought back up. If you don't want to keep a record for each test, this is useful in development. Use volumes to store data if you want to be persistent.

**Some Basic Commands**

- docker --version - tells us the current version of docker

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43opqr1is6x97gh64j6c.png)

- docker pull - pulls images from docker repository


```
docker pull <image name>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jz1ls4fykg28alwe3zzl.png)

- docker run - creates a container from an image

```
docker run <image name>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7a7u6ybj5jqq5pw5fj3.png)

- docker ps - lists the running containers

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/842hru77ygy3v6y086ge.png)

- docker ps -a - shows all running and exited containers

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iuwug5zlk3zifiegaym.png)

- docker exec - accesses the running container

docker exec -it <container id> bash_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1udms8oy17c6zrazziu.png)

7. _docker stop_ - stops a running container

```
docker stop <container id>_
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23mmucylq5aj8x8gfcca.png)

8. docker kill - kills the container by stopping the execution completely

```
_docker kill <container id>_
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6b4odu8hnovpas0e41pw.png)

- docker commit - creates a new image of an edited container on the local system

```
docker commit <container id> <username/imagename>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbmhadq1ym186falk0ac.png)

- docker login - used when in logging into the docker hub repository

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en5l974cskck2xm07yy4.png)

- docker push

```
docker push <username/image name>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwkelo7ofoo64xpvvorx.png)

- docker images - lists all locally stored docker images

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf4oujglguwx88ofkelf.png)

1- docker rm - deletes a stopped container

```
docker rm <container id>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4pk6dbyq63xnpj0s85y.png)

- docker rmi - deletes image from local storage

```
docker rmi <image-id>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faejjepz9acy14r6hrub.png)