# 🚀 Exploring Docker for DevOps Engineers! 🐳 | Day 16 of 90 Days Of DevOps

In this blog, I’ll introduce you to Docker, a powerful tool for creating and managing containers.

> <mark>Docker is a set of platforms as a service (PaaS) products that use the operating system level virtualization to deliver software in packages called containers.</mark>
> 
> <mark>Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.</mark>

* Docker enables you to separate your applications from your infrastructure so you can deliver software quickly and consistently.
    
* Docker also provides tooling and a platform to manage the lifecycle of your containers: develop, test, deploy, and run.
    

In this blog, I’ll show you some basic Docker commands that you can use to interact with containers and images.

These commands will help you understand how Docker works and what you can do with it.

Let’s get started!

# `docker run`

Use the `docker run` command to start a new container and interact with it through the command line.

The `docker run` command is one of the most important and frequently used commands in Docker. It allows you to create and run a new container from an image.

> An image is a template that contains everything needed to run an application, such as code, runtime, libraries, and settings.

You can use images from the Docker Hub, which is a service provided by Docker for finding and sharing container images, or you can create your own images using the `docker build` command.

The syntax of the `docker run` command is:

```bash
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
```

The `OPTIONS` are optional flags that modify the behavior of the command, such as `-d` for running the container in detached mode (in the background), `-p` for publishing a container’s port(s) to the host, or `--name` for giving a custom name to the container. The `IMAGE` is the name of the image that you want to use to create the container. The `COMMAND` and `ARG` are optional arguments that specify what command to run inside the container.

For example, if you want to run a container using the `hello-world` image, which is a simple image that prints a message and exits, you can use this command:

```bash
docker run hello-world
```

This will pull the image from the Docker Hub if it doesn’t exist locally, create a new container from it, and run it. You should see something like this:

```bash
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...
```

The output shows that the image was pulled from the Docker Hub, and then the message from the container was printed. You can also see some information about the container ID, name, and status by using the `docker ps -a` command, which lists all containers (running and stopped):

```bash
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
54f4984ed6a8   hello-world   "/hello"   12 seconds ago   Exited (0) 11 seconds ago             festive_mendeleev
```

You can also use the `docker run` command to start a new container and interact with it through the command line. For example, if you want to run a container using the `ubuntu` image, which is a Linux distribution, and open a shell inside it, you can use this command:

```bash
docker run -it ubuntu bash
```

The `-it` flag attaches an interactive terminal to the container’s standard input and output. The `ubuntu` is the name of the image, and `bash` is the command to run inside the container. You should see something like this:

```bash
root@9b0fdb8b2a37:/#
```

This means that you are now inside the container as a root user. You can execute any commands that are available in the Ubuntu image, such as `ls`, `apt-get`, or `ping`. To exit from the container, you can type `exit` or press Ctrl+D.

---

# `docker inspect`

Use the `docker inspect` command to view detailed information about a container or image.

The `docker inspect` command returns low-level information on Docker objects, such as containers or images. You can use it to view the configuration, status, network settings, volumes, logs, and other details of a container or image.

The syntax of the `docker inspect` command is:

```bash
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
```

The `OPTIONS` are optional flags that modify the output format, such as `--format` for using a Go template, or `--type` for specifying the type of object to inspect. The `NAME` or `ID` are the names or IDs of the objects to inspect. You can specify one or more objects to inspect at once.

For example, if you want to inspect the `hello-world` image that you used in the previous task, you can use this command:

```bash
docker inspect hello-world
```

This will return a JSON array with a lot of information about the image, such as its ID, name, tags, labels, layers, size, history, and so on. You can use a tool like jq to parse and filter the output. For example, if you want to see only the size of the image, you can use this command:

```bash
docker inspect hello-world | jq '.[].Size'
```

This will return the size of the image in bytes:

```bash
13336
```

Similarly, if you want to inspect the `ubuntu` container that you used in the previous task, you can use this command:

```bash
docker inspect ubuntu
```

This will return a JSON array with a lot of information about the container, such as its ID, name, state, status, network settings, mounts, logs, and so on. You can use a tool like jq to parse and filter the output. For example, if you want to see only the IP address of the container, you can use this command:

```bash
docker inspect ubuntu | jq '.[].NetworkSettings.IPAddress'
```

This will return the IP address of the container:

```bash
"172.17.0.2"
```

---

# `docker port`

Use the `docker port` command to list the port mappings for a container.

The `docker port` command lists the port mappings for a container. You can use it to see which ports on the host are mapped to which ports on the container. This is useful when you want to access a service running inside a container from outside.

The syntax of the `docker port` command is:

```bash
docker port CONTAINER [PRIVATE_PORT[/PROTO]]
```

The `CONTAINER` is the name or ID of the container to list port mappings for. The PRIVATE\_PORT and PROTO are optional arguments that specify a specific port and protocol (TCP or UDP) to list.

For example, if you want to run a container using the `nginx` image, which is a web server, and publish its port 80 (the default HTTP port) to a random port on the host, you can use this command:

```bash
docker run -d -P nginx
```

The `-d` flag runs the container in detached mode (in the background), and the `-P` flag publishes all exposed ports to random ports on the host. The `nginx` is the name of the image. You should see something like this:

```bash
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
b380bbd43752: Pull complete 
667692510b70: Pull complete 
4ee07b099f6f: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
a8c3a0f82802f1f5ecb9a9f814ae3c9a994e6fdcfb2c7fd67a2c5d3a5e74fb32
```

The output shows that the image was pulled from the Docker Hub and then a new container was created and run. You can also see some information about the container ID by using the `docker ps` command, which lists running containers:

```bash
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                   NAMES
a8c3a0f82802   nginx     "/docker-entrypoint.…"   13 seconds ago   Up 12 seconds   0.0.0.0:49153->80/tcp   optimistic_mendel
```

You can see that port 80 on the container is mapped to port 49153 on the host. You can also use the `docker port` command to see the port mappings for the container:

```bash
docker port a8c3a0f82802
```

This will return the port mappings for the container:

```bash
80/tcp -> 0.0.0.0:49153
```

You can also specify a specific port and protocol to see the mapping for that port:

```bash
docker port a8c3a0f82802 80/tcp
```

This will return the same output as before:

```bash
0.0.0.0:49153
```

You can now access the web server running inside the container by opening your browser and typing [`http://localhost:49153`](http://localhost:49153) or `http://<host-ip>:49153`, where `<host-ip>` is the IP address of your host machine. You should see something like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691178577074/f81a9e6f-a723-4c9e-8008-b87c014a210c.png align="center")

---

# `docker stats`

Use the `docker stats` command to view resource usage statistics for one or more containers.

The `docker stats` command displays a live stream of resource usage statistics for one or more containers. You can use it to monitor the CPU, memory, network, and disk activity of your containers.

The syntax of the `docker stats` command is:

```bash
docker stats [OPTIONS] [CONTAINER...]
```

The `OPTIONS` are optional flags that modify the output format, such as `--all` for showing all containers (default shows just running), `--format` for using a Go template, or `--no-stream` for disabling streaming and only showing the first result. The `CONTAINER` are the names or IDs of the containers to show statistics for. If you don’t specify any containers, all running containers are shown.

For example, if you want to see the resource usage statistics for all running containers, you can use this command:

```bash
docker stats
```

This will return a live stream of statistics for each container, such as its name, ID, CPU percentage, memory usage and limit, memory percentage, network input, and output, and block input and output. You should see something like this:

```bash
CONTAINER ID   NAME                 CPU %     MEM USAGE / LIMIT   MEM %     NET I/O          BLOCK I/O        PIDS
a8c3a0f82802   optimistic_mendel    0.00%     2.344MiB / 1.941GiB   0.12%     1.11kB / 0B      4.1kB / 0B        2
9b0fdb8b2a37   ubuntu               0.00%     1.113MiB / 1.941GiB   0.06%     1.16kB / 648B    4.1kB / 4.1kB     1
```

You can also specify one or more containers to show statistics for by using their names or IDs:

```bash
docker stats optimistic_mendel ubuntu
```

This will return the same output as before, but only for the specified containers:

```bash
CONTAINER ID   NAME                 CPU %     MEM USAGE / LIMIT   MEM %     NET I/O          BLOCK I/O        PIDS
a8c3a0f82802   optimistic_mendel    0.00%     2.344MiB / 1.941GiB   0.12%     1.11kB / 0B      4.1kB / 0B        2
9b0fdb8b2a37   ubuntu               0.00%     1.113MiB / 1.941GiB   0.06%     1.16kB / 648B    4.1kB / 4.1kB     1
```

To stop the live stream, you can press Ctrl+C.

Use the `docker top` command to view the processes running inside a container.

The `docker top` command displays the running processes inside a container. You can use it to see what processes are consuming resources or causing issues inside your container.

The syntax of the `docker top` command is:

```bash
docker top CONTAINER [ps OPTIONS]
```

The `CONTAINER` is the name or ID of the container to show processes for. The `ps OPTIONS` are optional arguments that are passed to the `ps` command inside the container.

For example, if you want to see the processes running inside the `ubuntu` container that you used in `docker run hello-world`, you can use this command:

```bash
docker top ubuntu
```

This will return a table of information about each process, such as its PID, user, time, command, and so on. You should see something like this:

```bash
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                1                   0                   0                   00:12               pts/0               00:00:00            bash
```

You can also specify some `ps` options to modify the output format. For example, if you want to see only the PID and command of each process, you can use this command:

```bash
docker top ubuntu -o pid,cmd
```

This will return a table of information about each process, but only with the PID and command columns. You should see something like this:

```bash
PID                 CMD
1                   bash
```

---

# `docker save`

Use the `docker save` command to save an image to a tar archive.

The `docker save` command saves one or more images to a tar archive. You can use it to back up your images or transfer them to another machine.

The syntax of the `docker save` command is:

```bash
docker save [OPTIONS] IMAGE [IMAGE...]
```

The `OPTIONS` are optional flags that modify the output format, such as `-o` for specifying the output file name. The `IMAGE` are the names or IDs of the images to save. You can specify one or more images to save at once.

For example, if you want to save the `hello-world` image that you used in `docker run hello-world` to a file called `hello-world.tar`, you can use this command:

```bash
docker save -o hello-world.tar hello-world
```

This will create a file called `hello-world.tar` in your current directory, which contains the image and its metadata. You can check the size of the file by using the `ls -lh` command:

```bash
-rw------- 1 root root 3.2K Aug  5 00:49 hello-world.tar
```

You can also save multiple images to a single file by specifying more than one image name or ID:

```bash
docker save -o images.tar hello-world ubuntu nginx
```

This will create a file called `images.tar` in your current directory, which contains all three images and their metadata. You can check the size of the file by using the `ls -lh` command:

```bash
-rw------- 1 root root 133M Aug  5 00:51 images.tar
```

---

# `docker load`

Use the `docker load` command to load an image from a tar archive.

The `docker load` command loads an image or a repository from a tar archive. You can use it to restore your images or transfer them to another machine.

The syntax of the `docker load` command is:

```bash
docker load [OPTIONS]
```

The `OPTIONS` are optional flags that modify the input format, such as `-i` for specifying the input file name. If you don’t specify any options, the command reads from the standard input.

For example, if you want to load the `hello-world` image that you saved in `docker save` from the file called `hello-world.tar`, you can use this command:

```bash
docker load -i hello-world.tar
```

This will load the image and its metadata from the file and add it to your local image repository. You should see something like this:

```bash
Loaded image: hello-world:latest
```

You can also load multiple images from a single file by using the same command. For example, if you want to load all three images that you saved in `docker save` from the file called `images.tar`, you can use this command:

```bash
docker load -i images.tar
```

This will load all three images and their metadata from the file and add them to your local image repository. You should see something like this:

```bash
Loaded image: hello-world:latest
Loaded image: ubuntu:latest
Loaded image: nginx:latest
```

You can verify that the images are loaded by using the `docker images` command, which lists all images in your local repository:

```bash
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   4 months ago  13.3kB
ubuntu        latest    fb52e22af1b0   4 months ago  72.9MB
nginx         latest    f6d0b4767a6c   4 months ago  133MB
```

---

In this blog, I have introduced you to Docker, a powerful tool for creating and managing containers. I have also shown you some basic Docker commands that you can use to interact with containers and images. These commands will help you understand how Docker works and what you can do with it.

I hope you have learned something new and useful from this blog. If you have any questions or feedback, please feel free to leave a comment below.

Thank you for reading and happy learning! 😊
