Docker CLI Cheat Sheet...
Tags: docker, dockercompose, devops, cheatsheet
Published: 2021-02-18 17:18:43

Hey, You found it !...  Didn’t thought you would run into my first post/blog which I would like to share with you. Hope this post satisfies what you been searching for..

Here are the commands which is use a lot. Continuing the post considering u have already installed the docker in your environment.

Table of content :paperclip:
* [Docker Images](#docker-images-t)
  * [Pull](#docker-images-pull)   
  * [Search](#docker-images-search)   
  * [Create new image](#docker-images-create)   
  * [Build](#docker-images-build)   
  * [Pushing](#docker-images-push)   
  * [Remove](#docker-images-remove)   

* [Docker Container](#docker-container-t)
  * [Create](#docker-container-create) 
  * [Run](#docker-container-Run) 
  * [Update](#docker-container-update) 
  * [Rename](#docker-container-rename) 	
  * [Start Stop Restart](#docker-container-ssr)
  * [Pause Resume](#docker-container-pr)
  * [Remove](#docker-container-remove)
  * [Info's](#docker-container-info)

* [Docker Network](#docker-network-t)
  * [Create](#docker-network-create) 
  * [List](#docker-network-ls) 
  * [Inspect](#docker-network-inspect) 
  * [Remove](#docker-network-remove) 
  * [Connect Disconnect](#docker-network-condis) 

***
### Docker Images <a name="docker-images-t"></a>
#### Pull image from Docker Hub <a name="docker-images-pull"></a>

> To pull the Docker image available in docker hub repository. Image includes all the open-source images and your custom images.

`docker pull` **`imageName`**
`docker pull naveenv24/hello_world`

#### Search <a name="docker-images-search"></a>

> To search for any image available in docker hub includes additional filter option.

`docker search` **`imageName`**
`docker search naveenv24/hello_world`

#### Create new image <a name="docker-images-create"></a>

> All images which are created is dependent on the base image eg. alpine image. If we name the file as Dockerfile by default docker picks up the file to perform actions related to the image

```FROM alpine:3.7
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]```

#### Build an image <a name="docker-images-build"></a>

**`docker build .`** (OR) **`docker build dir/pathto/`**

`docker build -f` **`Dockerfile`**

`docker build -f Dockerfile.renamed`

#### Pushing the image <a name="docker-images-push"></a>

> This is used to push your new or any updated image to the docker hub. Perquisite is the docker login to the registry.

`docker push` **`imageNameToBeUploaded`**

`docker push naveenv24/hello_world`

#### Removing the image <a name="docker-images-remove"></a>

> This command will help you remove the image from your environment if it’s not having any dependency. Include `-f` to forcefully remove. `rmi` implies remove-image.

`docker rmi -f` **`imageName`**

`docker rmi naveenv24/hello_world`

***

### Docker Container <a name="docker-container-t"></a>

#### Creating a container <a name="docker-container-create"></a>

> This command will create writable container over the specified image. This command will only create the container but will not run/start it. `-t` implies target `-I` implies the image name. 

`docker create – t  -I` **`ImageName`**  `--name` **`ContainerName`**

`docker create – t  -I naveenv24/hello_world --name hw-container`


#### Running the container <a name="docker-container-run"></a>

> This command will create the container if not created and starts the container the `-it` implies interactive mode and `-d` implies detach mode which run the container background

`docker run –name` **`containerName`**  `-it -d imageName`

`docker run –name hw-container -it -d naveenv24/hello_world`

#### Renaming the container <a name="docker-container-rename"></a>

> This command will rename you’re container name 

`docker rename` **`oldContainerName`** **`newContainerName`**

`docker rename hw-container hw-con-new`

#### Updating the container <a name="docker-container-update"></a>

> To update any container related configuration update command is used. Configuration more related to the CPU, memory etc. , Note: currently not available in windows at the time of this been posted.

`docker run -dit –name containerName –kernel-memory 50M ubuntu bash`

`docker update –kernel-memory 100M  containerName`

#### Container Start, Stop and Restart <a name="docker-container-ssr"></a>

> Stop the running container or start the non running container or use restart to perform both. `-t` wait time before killing the container.

`docker stop` **`containerName`**
`docker stop hw-con-new`

`docker start` **`containerName`**
`docker start hw-con-new`

`docker restart -t` **`containerName`**
`docker restart -t hw-con-new`

#### Pausing and Resuming <a name="docker-container-pr"></a>

> To suspend any process running in the container temporarily and to un-suspend the process again.

`docker pause` **`containerName`**
`docker pause hw-con-new`

`docker unpause` **`containerName`**
`docker unpause hw-con-new`

#### Removing the container <a name="docker-container-remove"></a>

> To remove the one or more container. To remove the container forcefully add -f or –force.

`docker rm -v  -f` **`containerName`**
`docker rm -v  -f hw-con-new`

#### Information’s related to Container <a name="docker-container-info"></a>

> Listed commands will provide info related to container or process running In the container.

`docker ps` > will show all the active running container (-a includes remaining available container)

`docker logs hw-con-new` >> gets logs from container. (You can use a custom log driver, but logs is only available for json-file and journald in 1.10).

`docker inspect hw-con-new` >> displays all the info related to container includes the ips and ports related details`

`docker port hw-con-new`>> shows all the ports running in the container`

`docker stats hw-con-new` >>  Display a live stream of container(s) resource usage statistics

***

### Docker Networks <a name="docker-network-t"></a>

#### Creating a Network <a name="docker-network-create"></a>

> To create a network by default will create a bridge network, to specify a network type use -d (driver) option along with the command.

`docker network create -d` **`bridge`** **`networkName`**

`docker network create -d bridge mynetwork`

#### Listing all Network <a name="docker-network-ls"></a>

> To view all the available networks in the docker

`docker network ls`

#### Inspecting a Network <a name="docker-network-inspect"></a>

> To view the detailed information about the network

`docker network inspect` **`networkName`**
`docker network inspect mynetwork`

#### Removing <a name="docker-network-remove"></a>

> To remove the network from the available list

`docker network rm` **`networkName`**
`docker network rm mynetwork`

#### Connect & Disconnecting container from a network <a name="docker-network-condis"></a>

> To connect the container from the network

`docker network connect` **`networkName containerName`**
`docker network connect mynetwork hw-con-new`

> To disconnect the container from the network

`docker network disconnect` **`networkName containerName`**
`docker network disconnect mynetwork hw-con-new`

***

These are all the command we will frequently looking into when any implementation is undergone. 

Though it might seem few these are basically enough to perform and docker build with multiple services connected through.. :rocket:

Appreciate spending your time on this. Hope you liked it.. Share your thoughts this might be useful for improving the quality. :beers: