Friday, July 9, 2021

Docker Desktop + WSL2 + Ubuntu + WSL +docker

 docker run -it --rm --privileged --pid=host justincormack/nsenter1

\\wsl2$ - Special Shared Path on Windows - which hides complex Path of Windows 
Docker Desktop + Ubuntu(WSL2) --- linked
Docker commands run from both
but Images go in Docker Desktop only -  \\wsl$\docker-desktop-data\version-pack-data\community\docker
Volume should be uploaded from Ubuntu/WSL2
Volume upload from Windows wont work
You can upload Folders from Windows to \\wsl$ shared path -specfically here  - \\wsl$\Ubuntu-20.04\home\karankaw
and it will show up as ~ in WSL2/Ubuntu

Thursday, July 8, 2021

Docker Logs


 docker logs  <web_container_ID_Or_Name>


 docker logs --follow web

Windows: Docker : exec :Docker Desktop

https://www.bretfisher.com/getting-a-shell-in-the-docker-for-windows-vm/


Getting a Shell in the Docker for Windows Moby VM

Moby VM - Mean Dummy Linux VM running on WSL Windows



docker run -it --rm --privileged --pid=host justincormack/nsenter1

Docker Volumes - File Locations for docker which Persist after container

https://www.freecodecamp.org/news/where-are-docker-images-stored-docker-container-paths-explained/

https://stackoverflow.com/questions/34809646/what-is-the-purpose-of-volume-in-dockerfile/34810191#34810191


https://www.docker.com/blog/how-to-use-the-official-nginx-docker-image/   [Example]

Docker Volumes

It is possible to add a persistent store to containers to keep data longer than the container exists or to share the volume with the host or with other containers. A container can be started with a volume by using the -v option:

$ docker run --name nginx_container -v /var/log nginx
We can get information about the connected volume location by:
$ docker inspect nginx_container 

Adding Custom HTML

By default, Nginx looks in the /usr/share/nginx/html directory inside of the container for files to serve. We need to get our html files into this directory. A fairly simple way to do this is use a mounted volume. With mounted volumes, we are able to link a directory on our local machine and map that directory into our running container.

docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx

docker    run   -v    /path/to/host/directory:/path/inside/the/container    image

When a docker container is deleted,
volume is not deleted by itself, atleast not by default.

Clean up space used by Docker

It is recommended to use the Docker command to clean up unused containers. Container, networks, images, and the build cache can be cleaned up by executing:

$ docker system prune -a

Additionally, you can also remove unused volumes by executing:

$ docker volumes prune

Docker Images : Where are they stored


Docker Desktop + WSL(Ubuntu-20)

\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\
\\wsl$\docker-desktop-data\version-pack-data\community\docker\overlay2

Docker images

The heaviest contents are usually images. If you use the default storage driver overlay2, then your Docker images are stored in 

/var/lib/docker/overlay2  - List of all images 


/var/lib/docker/image/overlay2/imagedb/content/sha256 List of kinda Top Images


docker run -it --rm --privileged --pid=host justincormack/nsenter1
\\wsl2$ - Special Shared Path on Windows - which hides complex Path of Windows 
Docker Desktop + Ubuntu(WSL2) --- linked
Docker commands run from both
but Images go in Docker Desktop only -  \\wsl$\docker-desktop-data\version-pack-data\community\docker
Volume should be uploaded from Ubuntu/WSL2
Volume upload from Windows wont work
You can upload Folders from Windows to \\wsl$ shared path -specfically here  - \\wsl$\Ubuntu-20.04\home\karankaw
and it will show up as ~ in WSL2/Ubuntu

Docker Command List - Full list

 Management Commands:

  app*        Docker App (Docker Inc., v0.9.1-beta3)

  builder     Manage builds

  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)

  compose*    Docker Compose (Docker Inc., 2.0.0-beta.1)

  config      Manage Docker configs

  container   Manage containers

  context     Manage contexts

  image       Manage images

  manifest    Manage Docker image manifests and manifest lists

  network     Manage networks

  node        Manage Swarm nodes

  plugin      Manage plugins

  scan*       Docker Scan (Docker Inc., v0.8.0)

  secret      Manage Docker secrets

  service     Manage services

  stack       Manage Docker stacks

  swarm       Manage Swarm

  system      Manage Docker

  trust       Manage trust on Docker images

  volume      Manage volumes


Commands:

  attach      Attach local standard input, output, and error streams to a running contain

er

  build       Build an image from a Dockerfile

  commit      Create a new image from a container's changes

  cp          Copy files/folders between a container and the local filesystem

  create      Create a new container

  diff        Inspect changes to files or directories on a container's filesystem

  events      Get real time events from the server

  exec        Run a command in a running container

  export      Export a container's filesystem as a tar archive

  history     Show the history of an image

  images      List images

  import      Import the contents from a tarball to create a filesystem image

  info        Display system-wide information

  inspect     Return low-level information on Docker objects

  kill        Kill one or more running containers

  load        Load an image from a tar archive or STDIN

  login       Log in to a Docker registry

  logout      Log out from a Docker registry

  logs        Fetch the logs of a container

  pause       Pause all processes within one or more containers

  port        List port mappings or a specific mapping for the container

  ps          List containers

  pull        Pull an image or a repository from a registry

  push        Push an image or a repository to a registry

  rename      Rename a container

  restart     Restart one or more containers

  rm          Remove one or more containers

  rmi         Remove one or more images

  run         Run a command in a new container

  save        Save one or more images to a tar archive (streamed to STDOUT by default)

  search      Search the Docker Hub for images

  start       Start one or more stopped containers

  stats       Display a live stream of container(s) resource usage statistics

  stop        Stop one or more running containers

  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

  top         Display the running processes of a container

  unpause     Unpause all processes within one or more containers

  update      Update configuration of one or more containers

  version     Show the Docker version information

  wait        Block until one or more containers stop, then print their exit codes

Docker-Learn3

 docker container run --detach --rm --publish 80:80 --name webserver nginx

docker container run --detach  --publish 80:80 --name webserver nginx

docker container rm <container_Name|container_Id_First3Digit_SHA>


Use -- flags, its a good practice in docker


docker image ls 


docker container logs webserver


docker container top ContainerID|containerName


docker run -d --name mongo mongo

-------------------------------------

$ docker run -it --rm --privileged --pid=host justincormack/nsenter1

/ #

-------------------------------------

ps aux | { head -1; grep -E 'mysql|mongo' ; }

ps -ef | { head -1; grep bash; }

ps aux | { head -1; grep 999 ; }

-------------------------------------


Docker proces is running on host 

docker top mongo 

//mongo is name of container and this command tells us processes running as a part of mongo docker container - top 10 Processes

ps aux | grep mongod


-------------------------------------

docs.docker.com

--help 

Our friends

-------------------------------------


docker container run -d -p 3306:3306 --name db --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql


-------------------------------------------------------------------------------------------------

Analyse Outside container

docker container top Container_Name

docker container inspect [OPTIONS] Container_Name

docker container stats [OPTIONS] [CONTAINER...]  //if no container_Name , it means all


-------------------------------------------------------------------------------------------------

Analyse Inside container - To know about whats happening in container

docker container start -ai Container_Name   //a means attach , i means interactive

docker container exec -it Container_Name   //t means tty , i means interactive

docker container run -it ImageName              //t means tty , i means interactive

-------------------------------------------------------------------------------------------------

apt-get update

apt-get install -y procps //Install "ps" in mysql - Its having debian

-------------------------------------------------------------------------------------------------

docker container port nginx


virtual network  ----is having a container's port

host port

only host port is mapped to only 1 Container...mapped


A container can talk to other container if they are on same virtual networks

-------------------------------------------------------------------------------------------------------

• Each Container is by default connected to - private virtual network "Bridge"

• Each PVN routes through NAT Firewall on host IP

• All containers on a virtual network can talk to each other without -p 

For example :

A network has 2 Containers :-> Mysql and httpd

httpd has 8080:80 

While Mysql has nothing

Mysql can talk to httpd

• 2 Different networks cannot talk to each other they have to go via NAT

• 1 host level port is mapped to 1 container only


• Make new virtual networks

• Attach containers to more than 1 virtual network

• Use docker network Drivers.

----------------------------------------------------------------------------------------------

ifconfig en0 // Linux based Actual Host machine

ipconfig // Windows based Actual Host machine

----------------------------------------------------------------------------------------------

docker container port ContainerID

docker container inspect ContainerID 

docker container inspect  --format  "{{ .NetworkSettings.IPAddress}}" nginx

----------------------------------------------------------------------------------------------

Why its called a bridge network ?

Its a type of "Driver"

because this vpn connects our container to outside physical network through  NAT  firewall

----------------------------------------------------------------------------------------------

:::::docker network commands :::::


• docker network ls      // Shows list of all private virtual networks with type of Drivers they possess

//bridge is called "bridge" or "docker0"



• docker network inspect bridge //shows containers attached to this network

Each Container has its own IP Address , although they are attached to same Network SHAid


• Network Type has a Subnet": "172.17.0.0/16" in IPAM Config

has many containers attached to it , Each having its own Ip Addresses

 "IPv4Address": "172.17.0.6/16"

 "IPv4Address": "172.17.0.3/16"

"IPv4Address": "172.17.0.2/16"

----------------------------------------------------------------------------------------------

172.17.0.0 ---- Default IP Address of Bridge Network


----------------------------------------------------------------------------------------------

another network is "host", gives up docker security

attaches directly to host interface

----------------------------------------------------------------------------------------------

docker network inspect bridge

docker network inspect host

docker network inspect none

----------------------------------------------------------------------------------------------

:::: Create a new network

docker network create my_app_net

docker network inspect my_app_net 

"Subnet": "172.18.0.0/16",

"Gateway": "172.18.0.1"

----------------------------------------------------------------------------------------------

docker network create my_app_net 

docker container run --name new_nginx --network my_app_net nginx:alpine //New Container

docker network inspect my_app_net // it has new_nginx attached to it

// --network network   flag on "run" command       Connect a container to a network

----------------------------------------------------------------------------------------------

//docker network 

// docker network connect [OPTIONS] NETWORK CONTAINER

docker network connect --help

docker network  connect  my_app_net nginx          //Attach network to container

docker container inspect nginx      //Inspect container --- It shows connection to 2 networks, now



 "Networks": {

                "bridge": {

                    "IPAMConfig": null,

                    "Gateway": "172.17.0.1",

                    "IPAddress": "172.17.0.6",

                },

                "my_app_net": {

                    "IPAMConfig": {},

                    "Gateway": "172.18.0.1",

                    "IPAddress": "172.18.0.3",


                }

            }

----------------------------------------------------------------------------------------------

docker network disconnect  my_app_net nginx    // Disconnect custom network from ContainerName

----------------------------------------------------------------------------------------------

if apps are on same host, then you should connect both apps to same network 

Explicit -p is very safe because all other ports are blocked!!!! so its very safe.

----------------------------------------------------------------------------------------------

docker network create --driver bridge my_app_net


----------------------------------------------------------------------------------------------

Containers should not rely on IP addresses for communication. DNS Should be used.


Custom Network have DNS Server built into them

default "bridge" network does not has DNS, use --link as workaround.


docker container exec -it my_nginx ping new_nginx


Container Names can be used as - DNS Hostnames , So, if there are 2 containes on same custom Network

They can ping each other using just "Container Names" which are DNS Names

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass