Docker How & Why to Use?
Tags: beginners, docker
Published: 2022-08-26 11:04:00

## How to use docker
In my last article, I wrote about how [Docker Hello world](https://dev.to/pratapkute/docker-hello-world-23l6) works. In that, I have explained about container and docker ecosystem 

Docker packs, ships, and runs the application as a lightweight containerization tool and builds an agile software delivery pipeline to ship new features faster. so let’s start using docker.

I recommend you start with the docker command line (CLI) tool first, then go for the docker desktop. Now let's say you want your **Dockerfile image** first, take a simple example

**1. Create a Dockerfile with no extension**
![Dockerfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmkj8fryomn046f8q51f.png)
```docker
FROM alpine
CMD ["echo", "Love from India"]
```

In the first line, we are using a **base image** (A base image is an **image that is used to create all of your container images** ) You can use multiple flavors of images, such as Ubuntu, Debian, etc., but **alpine images** are **small in size.**

Dockerfile has FROM, COPY, RUN, CMD, etc [instructions](https://docs.docker.com/engine/reference/builder/#format) which tell docker demon how to build an image. Each instruction is a **layer** in the container. CMD is a special instruction that tells specifies what command to run within the container. [Best practices for writing Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/)


![Docker container layers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp8jtddka7lbbxnx3eru.png) 

**2. Let’s build the image**

```bash
docker build -t love:latest .
```

![Docker build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8jmdxoklhaubohp8tz1.png)

[Docker build](https://docs.docker.com/engine/reference/commandline/build/) is a command that builds Docker images from a Dockerfile and a “context”. In the above command, we used the `-t` option as a tag in the 'name: tag' format, and `.` is a reference to **context** (The transfer of context from the local machine to the Docker daemon) and we have given the name `love:latest` to our image.

As you see in `step 1/2` the image `alpine:latest` image got pulled from the [docker hub](https://hub.docker.com/search?q=) and in the second `step 2/2` we have given instructions to `echo` a string which is **love from India.**


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

**3. Running docker container**

```bash
docker container run love
```
when you run the above command you will see the output to the terminal
![love from india](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zz79zmvfyxo5u1y18ut.png)
If you want just practice these commands without installing docker on your machine, head to [https://labs.play-with-docker.com/](https://labs.play-with-docker.com/)  on this platform, you need a docker id and play with docker.

## Why is Docker useful?

Docker allows you to exactly replicate your development environment which gives you leverage to distribute it QA team or in a production environment. It alleviates the problem of “it’s broken on my machine!”.

If you want to add another server (Ex: Database) to your existing infrastructure you don't have to worry about configuring that server and reinstalling the dependency, just pull that particular image from the Docker hub.

Docker is an easy tool to use when you get started using its daily development and OPS practices. with docker, you can run multiple separate containers with, a separate version of Python or any other language, app versions

Docker makes running multiple servers very easy, especially with orchestration engines like [Kubernetes](https://kubernetes.io/) and [Docker Swarm](https://docs.docker.com/engine/swarm/)

## When not to use Docker?

By default docker container run as `root` so every service in the container runs as root, it’s not the best implementation for the production environment.

The size of your docker container application grows exponentially when new updates come in. 

Building, testing, managing, and distributing Large and complicated applications require a dedicated sysadmin or DevOps engineer team.