# Day 16 : Docker for DevOps Engineers

### **Docker**

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

### Images

An *image* is a read-only template with instructions for creating a Docker container. Often, an image is *based on* another image, with some additional customization. For example, you may build an image that is based on the `ubuntu` image but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your images or you might only use those created by others and published in a registry. To build your image, you create a *Dockerfile* with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.

### **Containers**

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or the host machine.

A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.

`docker run` command

The following command runs an `ubuntu` container that attaches interactively to your local command-line session and runs `/bin/bash`.

```plaintext
$ docker run -i -t ubuntu /bin/bash
```

## **Steps for Docker Installation :**

To install Docker on an Ubuntu instance, you can follow these steps:

**Step 1**: First, update the database package on your Ubuntu instance to ensure you have the latest versions of software packages.

```plaintext
sudo apt-get update
```

**Step 2**: Install Docker on your Ubuntu instance using the below command.

```plaintext
sudo apt-get install docker.io
```

**<mark>Important</mark>**: Granting user access to Docker By default, Docker creates a group named <mark>"docker,</mark>" and to allow your user to access and use Docker, you'll need to add your user to that group. Follow these steps to add your user to the "docker" group:

**Step-3**: Run the below command to add your user to the "docker" group.

1. ```plaintext
       sudo usermod -aG docker $USER
    ```
    
2. Check which docker version is installed by using the below commands:
    
    ```plaintext
     docker --version
    ```
    

You've successfully installed Docker on your Ubuntu instance.

# Tasks

### **1: Use the** `docker run` **command to start a new container and interact with it through the command line**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687431196214/c89ed319-abd5-43db-90f3-af6f059342a4.png align="center")

### **2: Use the** `docker inspect` **command to view detailed information about a container or image**

The `docker inspect` provides detailed information on constructs controlled by Docker. By default, `docker inspect` will render results in a JSON array.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687431782892/06f449e1-12cb-4f96-b613-d95fbe168a64.png align="center")

### **3: Use the** `docker port` **command to list the port mappings for a container.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687436876843/68f2a3b6-d376-47c8-b09f-ff1e95b305ab.png align="center")

### **4: Use the** `docker stats` command to view resource usage statistics for one or more containers

The `docker stats` **returns a live data stream for running containers**. To limit data to one or more specific containers, specify a list of container names or ids separated by a space. You can specify a stopped container but stopped containers do not return any data.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687437043137/51ba4215-8ade-46ec-b051-706ec3ab6867.png align="center")

### **5: Use the** `docker top` command to view the processes running inside a container

`docker top` command displays the running processes of a container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687437185201/0ef5a9e2-d0ff-4743-b35a-a47d2b17e0cd.png align="center")

### **6: Use the** `docker save` command to save an image to a tar archive

`docker save` command to save each image to a tar file. The `docker save` command allows you to export Docker images to a tar archive file. This can be useful for backup purposes, transferring images to another system, or sharing images with others.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687437921039/6b8cc67c-63c1-4523-93c2-8a71af58439c.png align="center")

### **7: Use the** `docker load` **command to load an image from a tar archive**

The `docker load` command enables you to import Docker images from tar archive files, allowing you to restore images that were previously saved or shared.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687438686294/aa02edef-40b5-418b-bc77-d09fba934afb.png align="center")

***#90DaysOfDevOps Challange. Happy Learning :)***
