Exploring Docker Commands
Tags: docker
Published: 2020-01-13 03:14:13

In this article I'll talk about what docker is and we'll explore few useful Docker commands. We will look at different commands to create containers, images, docker hub.

Docker is used to run software application environment, programs, services inside an container. Example nginx or apache, mysql, wordpress, mongodb which can run inside a container which are separate from your operating system, separate from your local machine environment.

Instead of having to build an app on one machine and having to try to mimic that environment on another machine specially if it's a different operating system, you might have to install things differently and it becomes tedious in the end

Docker completely gets rid of that because you can simply run it in a container and it's going to be the same, no matter what machine you run it on - Mac, Linux, Windows machine or even the versions of the operating system.

**Docker concepts**
Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Containerization is increasingly popular because containers are:

Flexible: Even the most complex applications can be containerized.

Lightweight: Containers leverage and share the host kernel, making them much more efficient in terms of system resources than virtual machines.

Portable: You can build locally, deploy to the cloud, and run anywhere.

Loosely coupled: Containers are highly self-sufficient and encapsulated, allowing you to replace or upgrade one without disrupting others.

Scalable: You can increase and automatically distribute container replicas across a datacenter.

Secure: Containers apply aggressive constraints and isolations to processes without any configuration required on the part of the user.

**Images and containers**
Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application -- the code or binary, runtimes, dependencies, and any other filesystem objects required.
Containers and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.

![Docker Architecture](https://thepracticaldev.s3.amazonaws.com/i/jjalwi7rrhk2xtunc2xn.PNG)

*Image taken from [here](https://docs.docker.com/get-started/)*

On VMware, you have host machine/ server run multiple VM's and each VM machine has full blown OS and has their own kernel and you run apps on top of each OS. However, with docker you have one host machine/ server, one OS, it could be any OS - Windows, Mac, Linux and run multiple containers which are nothing but processes that run on your system. They all share the same kernel. It's much lighter and less overhead and very easy to port to other machine and very easy to deploy. So docker is very much better solution than doing it the VMware way.

**Orchestration**
The portability and reproducibility of a containerized process mean we have an opportunity to move and scale our containerized applications across clouds and datacenters; containers effectively guarantee that those applications will run the same way anywhere, allowing us to quickly and easily take advantage of all these environments. Furthermore, as we scale our applications up, we’ll want some tooling to help automate the maintenance of those applications, able to replace failed containers automatically and manage the rollout of updates and reconfigurations of those containers during their lifecycle.

Tools to manage, scale, and maintain containerized applications are called orchestrators, and the most common examples of these are Kubernetes and Docker Swarm. Development environment deployments of both of these orchestrators are provided by Docker Desktop, which we’ll use throughout this guide to create our first orchestrated, containerized application.

**Docker Commands**
If you have Docker installed on your system and if you open your terminal and type `docker`, you should be able to see list of docker commands such as below:

List Options and Management Commands
`docker`

![Docker](https://thepracticaldev.s3.amazonaws.com/i/1nb9f4skve2ms9ug4q11.PNG)

and few more!
Shows list of Options and Management Commands.
Usage: docker [OPTIONS] COMMAND
Run `docker COMMAND --help` for more information on a command.

Docker Version Info
`docker version`

 ![Docker Version](https://thepracticaldev.s3.amazonaws.com/i/wiiv56dipmr09opo8x1k.PNG)

Shows the Client and the Server versions.
Server is the docker engine which runs in the background and client is used to communicate with the engine.
It shows the version in most cases.
Docker is built on Go programming language, hence it shows the Go version.
The server OS/ arch is linux/amd64 and the client is what your machine is. 
For me its windows/amd64 or if you use mac you might have darwin/amd64

Docker Info
`docker info`

![Docker Info](https://thepracticaldev.s3.amazonaws.com/i/4ukg0gfk6g0wltff1ldx.PNG)
 
Shows you how many containers - running, paused, stopped. Also shows the number of Images installed on your system etc.

**Creating Containers**

Let's create a container. We'll create nginx - it's a very common web server similar to Apache.

Create nginx
`docker container run -it -p 80:80 nginx`

![Create nginx](https://thepracticaldev.s3.amazonaws.com/i/w036ev8o0jm3kp0vo20h.PNG)

'docker container' is the 'Management Command' and it has 'run' as the sub command
'-it' is for interactive mode, will run in the foreground rather than background. Running in foreground means it will shows logs in the terminal.
-p or --publish is publish
'80:80' first port 80 is the port that you want to use on your machine, you can set it to any unused port on your machine, since it's locally on your system, the second port 80 is what is exposed from the container, it's default port for nginx. You cannot change it.
'nginx' is the name of the image. You cannot change it, it has to be the same.

You will also notice "Unable to find image 'nginx:latest' locally", it means that it was unable to find the image locally on your system hence it will pull from docker hub will go to [Docker Hub](https://hub.docker.com/) which is GitHub of all docker images and pulls it down.
'latest' is just the tag it is using to get the latest build.
Once it is downloaded on your system, it will automatically start running.
Since it is downloaded on my system, now it's running and if I run it localhost on browser
No need to type localhost:80, as port 80 is default.

![Nginx](https://thepracticaldev.s3.amazonaws.com/i/095jea757jbyjde5f0r7.PNG)

Voilà, I’m running nginx on my machine!
 
Its running in a container, it not installed on my machine. Its inside a container.
Since I ran it as interactive mode (-it) in the foreground, every time I reload it going to show logs. it would not show had I run it detached (-d)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9zsppkqnfpmvl1grrj18.PNG)

All the images are stored on [Docker Hub](https://hub.docker.com/)

Create a container in foreground
`docker container run -it -p 80:80 nginx`

Creates a container which runs in foreground

Create a container in background
`docker container run -d -p 80:80 nginx`

Creates a container which runs in background

Naming Containers
`docker container run -d -p 80:80 --name nginx-server nginx`

Names the container as specified in name. In this case it is named as 'nginx-server'. If the Containers are not named, docker will set default names. Names are named as <adjective>-<name of famous hacker or scientist>.

List All Running Containers
`docker container ls`  or `docker ps` 

![Running Containers](https://thepracticaldev.s3.amazonaws.com/i/zappw0mw3g6435ga4yx7.PNG)

Shows list of running containers

List All Containers
`docker container ls –a`

![List All Containers](https://thepracticaldev.s3.amazonaws.com/i/6vd12b5n77icj1pexeg6.PNG)

Shows list of all containers, running or not

Stop A Running Container
`docker container stop [ID or NAME]`
`docker container stop mysql`

Stops the specified running container .

Stop All Running Containers
`docker stop $(docker ps -aq)`

Stops all running containers.

Remove A Container
`docker container rm [NAME]`
`docker container rm mysql` 


`docker container rm [ID]`
`docker container rm 5515`

![Remove A Container](https://thepracticaldev.s3.amazonaws.com/i/fdyizaq5y997wg7j7ifx.PNG)

Removes the specified non running container.

Remove A Running Container
`docker container rm -f [ID]`
`docker container rm mysql –f`

Removes a running container by force.
By running the command `docker container rm mysql` will throw error that it cannot remove the container because it is running. So we need to use force (-f).

Remove Multiple Containers
`docker container rm [ID] [ID] [ID]`

Removes all the container specified with the ID.

Remove All Non Running Containers
`docker rm $(docker ps -aq)` 

Removes all non running containers.

Remove All Containers
`docker rm $(docker ps -aq) -f`

Removes all containers, running as well as non running.

Get Logs (Use NAME or ID)
`docker container logs [NAME]`

Shows logs of the container specified either by NAME or ID.

List Processes Running in Container
`docker container top [NAME]`

Lists the processes running in container by the name specified.


**Image Commands**

List Docker Images
`docker images`

![List Docker Images](https://thepracticaldev.s3.amazonaws.com/i/0yiyt8clbz7ahexok7h0.PNG)

Shows list of all the images which are downloaded on your local system. Even though we have removed the container, the image still remains on the system

Remove An Image.
`docker image rm ID`
`docker image rm f7bb`

![Remove An Image](https://thepracticaldev.s3.amazonaws.com/i/rs19md1qjb038xa26552.PNG)

Removes the image specified by ID.
No need to put the entire ID, just 3-4 characters are fine.

Remove All Images
`docker rmi $(docker images -a -q)`

Removes all images present on your system.

Pull Image
`docker pull [IMAGE]`
`docker pull mongo`

![Pull Image](https://thepracticaldev.s3.amazonaws.com/i/l603er62q2jbfqhqvza6.PNG)

Manually pulls down the image specified by the Image Name if exists.

Environment Variables

mysql

`docker container run –d –p 3306:3306 –name mysql –env MYSQL_ROOT_PASSWORD=123456 mysql`

![Environment Variables](https://thepracticaldev.s3.amazonaws.com/i/0jors5sq2ghf4hisv1x8.PNG)

When you start the mysql image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the docker run command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.

See also https://dev.mysql.com/doc/refman/5.7/en/environment-variables.html for documentation of environment variables which MySQL itself respects (especially variables like MYSQL_HOST, which is known to cause issues when used with this image).


Docker Login
`docker login`

![Docker Login](https://thepracticaldev.s3.amazonaws.com/i/es91550s4ejjrn9jodaf.PNG)

Used to login in Docker. If you run the command and you are already logged in, it will try to login with your existing credentials.