Docker 101: Understanding Images and Containers in Plain English
Tags: docker, tutorial, devops, webdev
Published: 2023-06-23 18:23:21




## 📌 Table of content

####📍[What is Docker](#1)
####📍[Advantage of Docker](#2)
####📍[Architecture of Docker](#3)
####📍[What is Image](#4)
####📍[What is Container](#5)
####📍[Docker Commands](#6)
####📍[Image Commands](#7)
####📍[Container Commands](#8)

## 🔎 What is Docker <a name="1"></a>

![docker meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0djkkljn903iunocs1f3.png)

- Docker is an open-source platform that enables the _packaging_, _distribution_, and _deployment_ of applications using _containerization_.

- Docker uses an _isolated_ environment called **container** on the host OS to run applications. It allows applications to use the same Linux kernel as a system on the host computer, rather than creating a whole virtual OS.

- Docker works only on the **Linux kernel**. 


![difference between docker and VM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ih2blh5uoq5dvy60fql.png)



## ✨ Advantage of Docker <a name="2"></a>

👉**Portability**: Containers are _highly portable_, allowing applications to run consistently across different environments.

👉**Scalability**: Docker enables _efficient scaling_ of applications by quickly creating and deploying multiple containers.

👉**Isolation**: Containers provide a level of _isolation_ between applications, improving security and preventing conflicts between dependencies.

👉**Efficiency**: Docker's lightweight architecture and _shared OS_ resources result in efficient resource utilization and faster application startup times.

## 🔑 Key concept of Docker

- **Containers**,**Images**,**Docker Registry** is discussed soon in detail in different section of this blog. 

- **Dockerfile**: A Dockerfile is a text file that defines the instructions for building a Docker image.
 

![Building image from dockerfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0abd0pergnd4nrqhd9y.png)

**Docker Engine**: The Docker Engine is the runtime that enables the execution of Docker containers.

## 🧬 Architecture of Docker <a name="3"></a>

- **Docker client** : provide the user an interface to communicate with the docker daemon in form of commands and REST API's req.
- **Docker Host** : It is the physical or virtual machine where the docker runtime environment is installed. It run the docker daemon (server) which listened to request from the docker client and manages the container and images. 
- **Docker registry** : A Docker registry is a central repository that stores Docker images.
     -  Public Registry: Access to everybody for ex : _DockerHub_
     -  Private Registry: registry accessed by any particular organisation.

 

![understanding docker architechture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nyh8tq97caq2sfqnoaw.png)





![understanding docker architechture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aah2eu5d0pwq54qzvjq4.png)


## 📔 What is an Image  <a name="4"></a>
- A Docker image is a _blueprint_ for creating containers. It contains all the necessary files, dependencies, configurations, and instructions for running a specific application or service.
- Each container created from an image is an instance of that image
- Docker images are stored in a **registry**, such as _Docker Hub_


![understanding the image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moror7gcex8ot6p2m318.png)


## 📦 What is the Container  <a name="5"></a>

 👉A Docker container is a **lightweight** and **isolated** runtime instance created from a _Docker image_.
👉Docker containers **encapsulate** applications and their dependencies, providing a consistent and isolated runtime environment. 
 👉Docker containers are highly **portable** and can run consistently on different systems, such as development machines, servers, cloud platforms. 
👉Docker containers can be easily **scaled horizontally** by running multiple instances of the same container. 
👉 After the allocation of the host system resources to image container is created.
 - _Image + Some part of your system resources (cpu,memory,storage) . = Container_

 - Below example shows how container is made out of an image 😀




![understanding the container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxfxi99jdey4hhy3xeyk.png)

## 📌 Docker Commands <a name="6"></a>

#### 🚀 Image Commands <a name="7"></a>
  - `docker image pull <image-name>:<tag>`:Pull an image from a registry

```
mrityunjay:~$ docker image pull redis:latest
latest: Pulling from library/redis
5b5fe70539cd: Pull complete 
a1bc4b7ad0f0: Pull complete 
a33d2f01ead9: Pull complete 
94b33095e71c: Pull complete 
d3c2b649b97c: Pull complete 
8a5bf7f0acd7: Pull complete 
Digest: sha256:6ccde0cb87c24c09b1970802b10e702ab4491ac932b79f69682609787379fa42
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
```
  - `docker image ls`: show all the images
```
mrityunjay:~$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
redis        latest    8e69fcb59ff4   7 days ago   130MB
```
   - `docker image inspect <image_name or image_id>` :Display detailed information on one or more images

```
mrityunjay:~$ docker image inspect redis
[
    {
        "Id": "sha256:8e69fcb59ff4a25f87cf68cf2558df9c56df3c3c8ef709677dc602a458fe9b2d",
        "RepoTags": [
            "redis:latest"
        ],
        "RepoDigests": [
            "redis@sha256:6ccde0cb87c24c09b1970802b10e702ab4491ac932b79f69682609787379fa42"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2023-06-14T21:57:04.72797832Z",
        "Container": 
        ....


```
  - `docker image rm <image_id / image_name>`: Delete one or more images

```
mrityunjay:~$ docker image rm redis
Untagged: redis:latest
Untagged: redis@sha256:6ccde0cb87c24c09b1970802b10e702ab4491ac932b79f69682609787379fa42
Deleted: sha256:8e69fcb59ff4a25f87cf68cf2558df9c56df3c3c8ef709677dc602a458fe9b2d
Deleted: sha256:fe526a32129d428c70598b5002c0c36375de4c593d5e9a9c5cb23b3029be6535
Deleted: sha256:5ae934b4bc8f1f63c0c096d7c33b9aa6782e4bf32c6584ab4a3374a5c2fea61b
Deleted: sha256:ad2888d341396ec43ce8c9526f4f706951cdd46b9c5c68248560fe19b7eeeebc
Deleted: sha256:ab6989ff03a00c9b4e5524e3858ba3ad6c939482d48434ad61c49bb5d390d31d
Deleted: sha256:67fa40895a637a6ffa36df77928cdd46f9f28f9ca2e7a5f5d1ca4851875b4956
Deleted: sha256:ac4d164fef90ff58466b67e23deb79a47b5abd30af9ebf1735b57da6e4af1323

```


  - `docker image push <image-name>:<tag>`:Push an image or a repository to a registry
  - `docker image build .` : Build image from dockerfile





📝 **NOTE :** If you need any help related to docker image command just type 

`docker image command --help`

```
mrityunjay:~$ docker image command --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

```


- ###🚀 Container Commands <a name="8"></a>

- 🎯 Creating the Container: This command will create the container of specific image in an **interactive mode**. But this will not run the container. 

```
docker container create -it --name <container_name> <image_id / image_name>
```

 - 🎯 Starting the Container: `docker container start` will start the container. `-i` will open container's console for i/o . `CONTAINER` is either name or id of the container.

```
docker container start -i CONTAINER
```

 - 🎯 Running the Container : `docker run` command will **create** and **run** the container `--name` will name the container and `-it` allow you to interact with the container's console. `-d` option make container run in deatched mode. 
 ```
docker run -it --name <container_name> -d <image_name>
```

- 🎯 Running the Command in Container: `docker exec` will execute the command in docker container. `-it` will enable interactive mode. `container` is either container name or id `command` specify the command to be executed.
```
docker exec -it CONTAINER COMMAND
```

 - 🎯 Stopping the Container : `docker container stop` will stop the running container `mycontainer` is either name or id of container.
```
docker container stop mycontainer
```

 - 🎯 Kill the Running Container : `docker container kill` command will forcefully terminate the container after try stopping conatiner gracefully using `docker container stop` command. `mycontainer` is either container name or container id. 

```
docker container kill mycontainer
```

 - 🎯 Removing the container : `docker container rm ` will remove the container. `mycontainer` is one of more container_name or container_id.

```
docker container rm mycontainer
```

📝 **NOTE :** If you need help for the docker container command 

`docker container command --help`

```
mrityunjay:~$ docker container command --help

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
```
🌟 This is all about Images and Containers.In next blog will be be going to see how to Container the appication. 

💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀