Docker Command Line / Docker Cheat Sheet

########## Docker ########## 

## To pull the image from registry

docker pull <registry>/<repository>:<tag>
docker pull docker.io/busybox:latest

## To list images on the host machine

docker image ls
docker images

## To run container in detached mode 

docker container run --detach --name <name-to-container> <container-name>
docker container run -d --name <name-to-container>  <container-name>

## To run container in interactive mode 

docker container run --tty --interactive --name <name-to-container> <container-name>
docker container run -it --name <name-to-container> <container-name>

## To execute comand on running container

docker container exec --tty --interactive  <name-to-container> <cmd-to-run>
docker container exec -it <name-to-container> <cmd-to-run>

## To inspect docker container

docker container inspect <container-name>

## To list running containers 

docker container ls

## To list all the containers including running, exited 

docker ps 
docker ps -a
docker container ls -a
docker container ls -all

## To list all the containers with exit statue 

docker ps -a --filter "status=exited"
docker container ls -a --filter "status=exited"

## To list all the containers with running status 

docker ps --filter status=running
docker container ls --filter status=running

## To list container id's 

docker container ls --all --quiet
docker container ls -a -q

## To check the stats  

docker container stats <container-id>

## To check the logs

docker container logs <container-id>

## To check process running inside container

docker container top <container-id>

## To check disk space that docker is using

docker system df

## To remove stopped containers, unused volumes, networks, and dangling images

docker system prune

## To remove dangling images

docker image prune

## To remove stopped containers

docker container prune

## To List all networks

docker network ls 

## To create network

docker network create <network-name>
docker network create -d <driver> <network-name>  
docker network create --driver <driver> <network-name> 

## To display detailed information of network

docker network inspect <network-name> 

## List port mappings for the container

docker container port <container-id>

## To Remove one or more networks

docker network rm <network-name>

## Create volume

docker volume create <vol-name>

## Bind Mount local directory to container 

docker container run --mount type=bind,source=<source-path>,target=<target-path> <image-name>
docker container run -v <source-path>:<target-path> <image-name>

## Mount local directory to container 

docker container run --mount type=volume,source=<vol-name>,target=<target-path> <image-name>
docker container run -v <vol-name>:<target-path> <image-name>

## To run container with restart policy

docker container run -d --restart always <image-name>
docker container run -d --restart on-failure <image-name>
docker container run -d --restart unless-stopped <image-name>

## To create docker secret

echo "<secret>" | docker secret create <my_secret> -
echo "pass123" | docker secret create db_pass -

## To create docker secret using file

docker secret create <my_secret> <file-name>
docker secret create db_pass pass-file.txt

## To list the secrets in docker

docker secret ls

## To inspect secret

docker secret inspect <my_secret>
docker secret inspect db_pass

## To removes a secret

docker secret rm <my_secret>
docker secret rm db_pass


########## DOCKER COMPOSE ##########

## To create and start the container 

docker-compose up

## To create and start the container in detached mode

docker-compose up --detach
docker-compose up -d

## To List all the containers

docker-compose ps

## To Display services

docker-compose ps --services

## To scale particular service in docker-compose

docker-compose up --detach --scale <service-name>=<count>

## Stops containers and removes containers, networks, volumes, and images created 

docker-compose down

## To Validate and view the Compose file

docker-compose config

##List images used by the created containers

docker-compose images

## To view logs output from services

docker-compose logs
docker-compose logs --tail=10

## To stop running containers without removing them

docker-compose stop

## To start running containers for service

docker-compose start

## Displays the running processes

docker-compose top


########## DOCKER SWARM ##########

## To initialize swarm mode 

docker swarm init --advertise-addr <IP-Address> 

## To create Token for Worker/Manager

docker swarm join-token worker
docker swarm join-token manager

## To list swarm nodes in the cluster

docker node ls

## To leave worker node from the swarm

docker swarm leave

## To remove worker node from the swarm

docker node rm <node-name>

## To promote node to worker

docker node promote <node-name>

## To demote node to worker

docker node demote <node-name>

## To update role of node 

docker node update --role <manager|worker> <node-name>

## To create service 3 replicas 

docker service create --name <service-name> --replicas <no-of-replicas> <image-name>
docker service create --name web-server --replicas 3 nginx:latest

## To adds a published service port to an existing service

docker service update --publish-add published=<hport>,target=<cport> <service-name>
docker service update --publish-add published=8080,target=80 web-server

## To update no. of replicas 

docker service update --replicas=<count> <service-name>
docker service update --replicas=3 web-server

## To check status of running service

docker service ps <service-name>
docker service ps web-service

## To check logs of service

docker service logs <service-name>
docker service logs web-server

## To scale replicas of service

docker service scale <service-name>=<no-of-replicas>
docker service scale web-server=6

## To run one task for the service on every available node in the cluster

docker service create --name <service-name> mode=global <image-name>
docker service create --name web-server mode=global busybox

## To update service to use new docker image

docker service update --image <new-image> <service-name>
docker service update --image nginx:alpine web-server

## To create service on Manager Node only

docker service create --constraint="node.role==manager" <image-name>

## To create service on Worker Node only

docker service create --constraint="node.role==worker" <image-name>


AWS Command Line Example to configure VPC

Tasks to perform:

1. Create VPC with a CIDR block of 10.0.0.0/16.
2. Create Public and Private Subnets in 4 availability zones and Tag them.
3. Change Public subnet IPv4 addressing behavior (auto-assign public IPv4).
4. Create Internet gateway, tag it and attach it to VPC.
5. Create Public Route Table and Tag them.
6. Tag main Route Table
7. Create Route entry for internet gateway.
8. Associate Public Subnets with Public Route Table and Private Subnets with Private Route Table. 

Note: Change Resource Id's with your Respective Resource Id's (eg: vpc-xxxxxxx, subnet-xxxxxxx, rtb-xxxxxxx)

1.Create VPC with CIDR block 10.0.0.0/16

#aws ec2 create-vpc --cidr-block 10.0.0.0/16
#aws ec2 create-tags --resources vpc-001c305ff96094653 --tags Key=Name,Value=My-VPC

2.Create two public subnets and Tag them

#aws ec2 create-subnet --vpc-id vpc-001c305ff96094653 --cidr-block 10.0.0.0/24 --availability-zone us-east-1a
#aws ec2 create-tags --resources subnet-0e5d976ddcf99803e --tags Key=Name,Value=Public-1a
#aws ec2 create-subnet --vpc-id vpc-001c305ff96094653 --cidr-block 10.0.1.0/24 --availability-zone us-east-1b
#aws ec2 create-tags --resources subnet-0fa40143d62ee153f --tags Key=Name,Value=Public-1b

3.Create two private subnets and Tag them

#aws ec2 create-subnet --vpc-id vpc-001c305ff96094653 --cidr-block 10.0.2.0/24 --availability-zone us-east-1c
#aws ec2 create-tags --resources subnet-08101fac2b2a63a49 --tags Key=Name,Value=Private-1c
#aws ec2 create-subnet --vpc-id vpc-001c305ff96094653 --cidr-block 10.0.3.0/24 --availability-zone us-east-1d
#aws ec2 create-tags --resources subnet-057a3aca47b2b74c9 --tags Key=Name,Value=Private-1d

4.Change a subnet's public IPv4 addressing behavior

#aws ec2 modify-subnet-attribute --subnet-id subnet-0e5d976ddcf99803e --map-public-ip-on-launch
#aws ec2 modify-subnet-attribute --subnet-id subnet-0fa40143d62ee153f --map-public-ip-on-launch

5.Create Internet Gateway For VPC

#aws ec2 create-internet-gateway
#aws ec2 create-tags --resources igw-07de90cac62aeb974 --tags Key=Name,Value=My-IGW

6.Attach Internet Gateway to VPC 

#aws ec2 attach-internet-gateway --internet-gateway-id igw-07de90cac62aeb974 --vpc-id vpc-001c305ff96094653

7.Create Public Route Table 

#aws ec2 create-route-table --vpc-id vpc-001c305ff96094653
#aws ec2 create-tags --resources rtb-0bd1ddee351f41843 --tags Key=Name,Value=PublicRT

8.Create Tag for Main RouteTable(Private RouteTable)  

#aws ec2 create-tags --resources rtb-06928448f0a014e32 --tags Key=Name,Value=PrivateRT

9.Create a route for Internet Gateway

#aws ec2 create-route --route-table-id rtb-0bd1ddee351f41843 --destination-cidr-block 0.0.0.0/0 --gateway-id igw-07de90cac62aeb974

10.Describe a Route Table

#aws ec2 describe-route-table --route-table-id rtb-0bd1ddee351f41843 

11.Associate Public Subnet with Public RouteTable  

#aws ec2 associate-route-table --route-table-id rtb-0bd1ddee351f41843 --subnet-id subnet-0e5d976ddcf99803e
#aws ec2 associate-route-table --route-table-id rtb-0bd1ddee351f41843 --subnet-id subnet-0fa40143d62ee153f

12.Associate Private Subnet with Main RouteTable(Private RouteTable)  

#aws ec2 associate-route-table --route-table-id rtb-06928448f0a014e32 --subnet-id subnet-08101fac2b2a63a49
#aws ec2 associate-route-table --route-table-id rtb-06928448f0a014e32 --subnet-id subnet-057a3aca47b2b74c9