Docker and its basic commands
Published: 2020-04-30 15:12:47

### What is docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. (by- opensource.com)

### What is a docker container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

### What is a docker image?
A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

A Docker image is a file, comprised of multiple layers, that is used to execute code in a Docker container. When the Docker user runs an image, it can become one or multiple instances of that container.
  
## Basic Docker commands 
Here, we will look at some of the basic commands for developers.

### Docker Images

```yml
# List available images in the local registry
docker images

# Get image history 
docker image history <image-name/id>

# Get all details about the images
docker image inspect <image_name/id>

# Remove image from the local registry
# We can use an alias like rm or rmi as well
docker image remove <image-name/id>

# Remove all unused images
docker image prune

# Pull an image or a repository from a registry
docker image pull <image/repository>

# Push an image to a registry
docker image push <repository-name:tag>
```

### Docker container
```yml
# All running containers
docker container ls

# All containers
docker container ls -a

# Stop the running container 
docker container stop <container-id>

# Pause the running container
docker container pause <container-id>

# Unpause the running container
docker container unpause <container-id>

# Kill the docker container
docker container kill <container-id>

# Inspect the container 
docker container inspect <container-id>

# Remove the unused container
docker container prune 

# Remove one or more container
docker container rm <container-id>

```

Let's try running 
`docker run -p 5000:5000 learntechfree/simple-express-app:0.0.1.RELEASE`

What we just wrote is the shortcut for docker container command 
`docker container run -p 5000:5000 learntechfree/simple-express-app:0.0.1.RELEASE`

__On pausing__ the container, that will not receive any request and that might cause the timeout if we try to access that docker container. In Linux, that will send the `SIGSTOP` signal.

Moreover, the difference between the `STOP` and `KILL` is, `STOP` allows us to close the container gracefully and `KILL` not. Internally `STOP` executes the command `SIGTERM` and if needed `SIGKILL` and `KILL` directly execute `SIGKILL`.

### Docker System
Docker system basically has four basic commands:
```yml
# Shows the docker disk usage
docker system df

# Get real-time events from the server
docker system events

# Display system-wide information
docker system info

# Remove unused data, images, containers, networks, and build caches.
# Will be useful when you want to remove all unused things
docker system prune

```

### Docker Stat

```yml

# Display resource usage statistics
docker stats <container-id>

# or
docker container stats <container-id>
```
This command is used to view the CPU usage, memory usage, memory limit of the container.

We can explicitly set the limit for memory usage for that container with parameter -m.

Check the following command where we have provided 512Mib of memory limit for the container.

`docker container run -p 5000:5000 -d -m 512m learntechfree/simple-express-app:0.0.1.RELEASE`

Now, if we do `docker stats <container-id>` then you will get the limit for that container to 512mb only not the default 2GB. 

We can also say how much percentage of CPU we want to assign to our container or say how much part of CPU we want to provide to the container with cpu-quota parameter.

The total CPU quota is of 100,000 or says a hundred thousand so that if we want to provide half of it then we  have to provide `--cpu-quota=50000`

Try,
`docker container run -p 5000:5000 -d -m 512m --cpu-quota=50000 wordpress`

As only half of the CPU is available it might take a bit more time for a startup and process.

Next: [Creating our own docker image and deploy to docker hub](https://dev.to/shrijan00003/creating-and-deploying-your-first-dockerized-web-application-3bhm)