Getting Started With Docker
Tags: docker, linux, devops, microservices
Published: 2021-09-06 12:28:37

### Introduction to Docker

Docker is a technology designed to make it easier to create, deploy, and run applications by using containers. Docker is an open platform, once we build a docker container, we can run it anywhere, say it windows, Linux, mac whether on a laptop, data center, or in the cloud. It follows the build once, run anywhere approach.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma2mylc0ri1llh5ajlzn.png)
 
### Installing Docker

I will not be showing you the steps to install docker here.
You can refer this.

https://dev.to/whattosay/docker-environment-setup-4hac

Docker works on a wide variety of operating systems, this includes:
 - Windows
 - Linux
 - MAC

The installation of Docker is pretty straight forward in each one of them.
https://docs.docker.com/get-docker/



### Docker Containers vs Virtual Machines

- Virtual Machine contains the entire Operating System.
- The container uses the resource of the host operating system 

### Docker [Image vs Containers]

Docker Image is a file that contains all the necessary dependency and configurations which are required to run an application.

Docker Containers is basically a running instance of an image.


### Docker Image Creation and Identification

When you create a Docker container, it is assigned a universally unique identifier (UUID).

These can help identify the docker container among others.
To help humans, Docker also allows us to supply container names.
By default, if we do not specify the name, docker supplies a randomly-generated name from two words, joined by an underscore
By adding _--name=meaningful_name_ argument during the docker run command, we can specify our own name to the containers

#### Creating nginx Docker image and getting IPConfig

```shell
$ docker pull nginx
$ docker images
$ docker run 
$ docker run -dt -p 80:80 nginx
$ docker ps
$ apt install net-tools
$ ifconfig
```
Here is a Snapshot for your reference, Do click on image to see it properly.

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

To check the nginx server
Goto: http://165.22.212.229:80

Here _165.22.212.229_ is droplet Public IP and _80_ is the port on which image has been mapped(_docker run -dt -p 80:80 nginx_)
By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers.
If we want containers to accept incoming connections from the world, you will have to bind it to a host port.


![nginx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i0z0s6g59ypu7ou0snm.png)

### Port Binding

```shell
$ docker inspect mynginx
```
This command shows the config details of docker container
In below Screenshot first I deleted the container by

```shell
$ docker rm 6e80b1835da72da248ae2e2d2b35045c3cd736196159a030f76bd31fb9f16f33
```

then did the porting from 8000 --> 80

```shell
$ docker run -d -p 8000:80 --name mynginx nginx
```

![Port Binding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vs14tlwnmrb8c5wm7o7.png)


### Attach and Detached Mode

When we start a docker container, we need to decide if we want to run in a default foreground mode or the detached mode.
You may want to use this if you want a container to run but do not want to view and follow all its output.

Detached Mode _-d_ 
```shell
$ docker run -d --name detached -p 8081:80 nginx
```
![detached](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/310ycuyfz875dbx8hc09.png)

### Removing Docker Containers

Docker containers can be removed with the help of docker container _rm_ command.

Description | Command
--- | ---
Remove single container | docker container rm CONTAINER
Stop all the containers | docker container stop $(docker container ls -aq)
Remove all the containers | docker container rm $(docker container ls -aq)

![Remove Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3k2rkyxxqffp17zvdqr9.png)

#### Troubleshooting Issue

While I was removing Image I found that images were running. SO, wasn't able to remove.
Therefore, you can't remove the image until you stop.

![troubleshooting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrr8iukjlc9vjtmuce1u.png)

**For more commands related to Docker**
https://docs.docker.com/engine/reference/commandline/docker/

The cli commands were then refactored to have the form docker COMMAND SUBCOMMAND, wherein this case the COMMAND is container and the SUBCOMMAND is run

Older Approach:    docker run 
Newer Approach: docker container run

Both of these approaches will work perfectly. 

### Docker container **_exec_**

The docker container _exec_ command runs a new command in a running container.

The command started using docker exec only runs while the container’s primary process (PID 1) is running, and it is not restarted if the container is restarted.

```shell
# here are the commands to run the commands inside container from outside 

$ docker container exec -it dporwalexec netstat -ntlp

#you can login to the container and run the commands

root@dporwal-docker:~# docker container exec -it dporwalexec bash

#here you are inside the container with the bash Interactive
root@c0efbecf71a2:/# /etc/init.d/nginx
```

Here is the snapshot of above commands.

![containerExecute](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbup8fztwynhk7kw1u6p.png)

### Importance of the IT flag

**_-it_**
Every process that we create in the linux environment , has three open files descriptions, **stdin, stdout, stderr**

**_-i_** --> interactive, keeps stdin open even if not attached

**_-t_** --> tty, flag allocated a paseudo-TTY

![ItFlag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpskql70hgctqtl68kub.png)

###  Default Container Command

Whenever we run a container, a default command executes which typically runs as PID 1.

![defaultcontainercommand](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bws7qx59u88ff4c7plj6.png)

We can override the default container command by manually specifying the command.
```shell
$ docker container run -d nginx sleep 500
```
In the above command, the sleep 500 will run as a PID 1 process overriding any default command that would be present in the nginx container.

![override default](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx8rfr3ixk7jripvrl76.png)

### Restart Docker Policies

By default, Docker containers will not start when they exit or when docker daemon is restarted.

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts.

We can specify the restart policy by using the --restart flag with docker run command

Flag | Description
--- | ---
no | do not automatically restart the container(the default)
on-failure | Restart the container if it exits due to any error, which manifest as a non-zero exit code
unless-stopped | Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted
always | Always restart if its stopped

```shell
$ docker container run -d --restart unless-stopped nginx

```

Here I have created on nginx container with _unless-stopped_ restart command.
After restarting Docker service the container should also get start, as it will run _restart_ command.

![restartcontainer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5700otgjdn6c2jnaylz7.png)

### Disk Usage montior docker containers

The docker system _df_ command displays information regarding the amount of disk space used by the docker daemon.

####Commands:
```shell
$ docker system df
$ docker system df -v
```

In the below Snapshot I'm creating 500MB file using command.
I'm not sure about the below mechanism, Do let me know in discussion section if you know anything about this, how it works.

```shell
dd if=/dev/zero of=bigfile.txt bs=1M count=500
```

![diskUsage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf4x2lilwx05uf033c62.png)

### Remove Docker Images

To remove one image at a time, we will use below command.

```shell
docker rmi nginx
```

To remove all the docker images at once, we will use first to list all the images and then run all the commands

```shell
docker images -a -q
docker rmi $(docker images -a -q)
```

![remove all images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzt93dp9d2w4pc7k06hv.png)

References:
[Official Docker](https://docs.docker.com/get-docker/)
[Udemy Course](https://www.udemy.com/course/docker-certified-associate/)

Credit:
[Zeal Vora](https://in.linkedin.com/in/zealvora)

[Prev: Setting Docker Environment](https://dev.to/dporwal/docker-environment-setup-4hac)

[Next: Image Creation, Management, and Registry(Part 1)](https://dev.to/dporwal/image-creation-management-and-registry-part-1-pk9)