Sneaking into Docker 🐳
Tags: docker, devops, cloud, container
Published: 2021-06-14 02:36:00

The virtual machine has long been the grab standard for cloud infrastructure because of its many benefits. What if there was a more lightweight, cost-effective, and scalable option to a virtual machine?

**Docker** 🐳 is just like that.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v22zrkyqgssc4ovqgw3.jpg)

## What is a Virtual Machine?

A virtual machine is a system which acts exactly like a computer. Following the virtualization of the hardware, each virtual machine need its own operating system.

VMs have several advantages

- On the same computer, multiple OS environments can exist in isolation from one another
- Easy maintenance
- Availability
- Rich with resources
- Cloud service providers like AWS, DigitalOcean, Google, Azure etc.

What limitations do VMs have?

- Less efficiency because they access hardware indirectly
- You'll require more resources as you operate more VMs
- Performance may be low when several virtual machines are operating on the same host
- Portability is restricted

**Containers** are a solution to the challenge of getting software to run effectively when it is transferred from one computing environment to another.

Containers are commonly used for web, applications, caching services, and small databases.

Docker is a container-based solution for creating distributed applications.

## What is Docker?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v54o2mqbi08rhhorhu9i.png)

Docker is an open platform enabling programs to be developed, shipped and executed. Docker allows you to separate your applications for speedy delivery of software from your infrastructure. With Docker, it's the same way you can manage your infrastructure.

## Why do we need Docker?

Sometimes you can't set your app on a friend's computer and can't run several programs on the same port.

Docker offers tools and a platform to manage the container lifecycle.

- The container is the unit that distributes and tests your application
- Deploy your application without a problem on any server
- Containers are great for continuous integration and continuous delivery (CI/CD) workflows
- Lightweight and fast
- The container-based technology from Docker enables extremely portable workloads.

## Docker architecture

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxysy4wik4ouelapn8q6.jpg)

### Dockerfile

A Dockerfile is a text file that contains all of the commands that a user may use to create an image from the command line. Users may establish an automated build using docker build that performs numerous command-line commands in a row.

Here's an example:

```docker
FROM node:14-alpine

RUN mkdir /app

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install

COPY . .

EXPOSE 4000

RUN yarn build

CMD [ "yarn", "start" ]
 
```

### Images

An image is a read-only template for building a Docker container. An image is frequently based on another image, with some added modifications.

### Containers

A container is a runnable image instance. The Docker API or CLI may be used to create, start, stop, move, or delete containers. You may attach storage to a container, link it to one or more networks, or even create a new image based on its existing state.

### Registry

A Docker registry stores Docker images.🐳 
Docker Hub is a public registry that anybody may access, and Docker is set up by default to seek for images on Docker Hub. You may even set up your own personal registry.

When you use, 

```docker
docker pull NAME[:TAG]
```

```docker
docker run NAME[:TAG] 
```
commands, the required images are pulled from your configured registry. 

When you use,

```docker
docker push NAME[:TAG] 
```
command, your image is pushed to your configured registry.

### Engine

Docker Engine is an open source containerization engine that may be used to develop and containerize applications.

## A few of key commands to remember

**List images**

```docker
docker image ls
```

**List containers**

```docker
docker container ls
```

**Build from Dockerfile**

```docker
docker build -t <name> .
```

**Run a container from image**

```docker
docker run <tag name>
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fsp7crovy3exw9hswcu.png)