Skip to the content.

Docker-Exploration

Docker used for documentation : Docker CE (Community Edition)

logo

concept1

Some Basic Docker Commands

Command Description
docker version Get the version information of docker.
docker info Get info.
docker images Get all available images in local repo.
docker container ps / docker container ps -a get running containers (-a all stopped & running)
docker container run -p 80:80 -d –name test_container nginx Run a container with nginx at port 80. bridge host IP 80 and container IP 80.
docker container run –rm -it image_name run container and automatically remove upon close
docker container logs test_container get logs for mentioned container
docker container top test_container Get process/daemons running in the container
docker container rm ... Remove stopped container. Containers to be removed should be stopped.
docker container rm -f Remove forcefully.
docker container inspect test_container details of container config
docker container stats show stats mem usage, cpu usage etc.
docker container run -it –name test_name image_name bash run container (-i –> interactive,-t –> pseudo tty/ssh) and opens bash(changed default commands)
docker container start -ai container_name starts existing (-ai start with given starting command) container
docker container stop container_name stops existing container
docker container exec -it container_name bash open bash in already running container
docker history image_name:tag layer information of the image

Port

    -p 8080:8080

    [host_os_port : docker_container_port]

What happens behind docker run

Image

Points to Notice

concept2

Examples

nginx

mongo

mysql

Docker Networks

concept3

Command Description
docker container port container_name get port info
docker container inspect –format “” container_name get IP
docker network ls show networks
docker network inspect net_name inspect a network
docker network create –driver create a network
docker network connect net_id container_id attach
docker network disconnect net_id container_id detach
docker container run –name c_name –network net_name image_name specifying network name in container while starting
docker container run –name c_name –net net_name –net-alias alias_name image_name specifying network name and alias in container while starting (same alias containers can be called with same DNS name)

DNS Naming (inter container communication)

try this

IMAGE

Image Layers

image
env
apt
ubuntu
image1 image2  
port other operation only diff is added in runtime container
copy copy common till here
apt apt  
Debian jessie Debain jessie  

example of layers:

imagelayers

Image representation

    <user>/<repo>:<tag>

DOCKERFILE

Dockerfile is a recipe for creating image.

Command Description
docker image build -f some-dockerfile build image from a dockerfile
docker image build -t custom_nginx . build docker image with tag custom_nginx from current working directory
Keyword Description
FROM All dockerfile must have to minimal distribution. want to go completely from scratch use “FROM scratch”
ENV Setting up environment variables. inject main key/values for image.
RUN Run shell commads
EXPOSE Expose ports on docker virtual network still need to use -p / -P on host os
CMD Final command to be run every time container is launched/started
COPY Copy from local(host) os to docker(guest/virtual) os
ENTRYPOINT Entrypoint for a container at runtime
WORKDIR is prefered to using “RUN cd /some/path”
VOLUME Create a new volume location and assign it to the directory in the container will outlive the container when container is updated. (requires manual deletion)
ADD  
    It is adviced to keep least changing things in the
    docker images to keep on top(initial steps) and more
    variable things in later steps so that whenver any step changes or updates till that step cache will help to
    speed up the process of building the image.

PRUNE

Command Description
docker image prune remove all dangling images
docker system prune remove everything

Container lifetime and persistent data

  1. immutable (unchanging) and ephemeral (temporary/ disposable).
  2. “immutable infrastructure” : only re-deploy containers, never change.
  3. But if there is some data that has to be present (like database or unique data).
  4. data can be preserved when container is getting updated with latest version. docker gives us feature to ensure “separation of concerns”.
  5. This is called as “Presistent data”.
  6. 2 solutions for this - Volumns and Bind Mounts.
  7. VOLUMES : make special location outside of container UFS(union file system).
  8. BIND MOUNT : link container path to host path.

PERSISTENT DATA

  1. Create a new volume location and assign it to the directory in the container
  2. will outlive the container when container is updated.
  3. requires manual deletion

volumeInfo

Command Description
docker volume ls list of volumes
docker volume inspect volume_name information about volume
docker volume create volumne_name create volume

volumes1

    docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql:latest

volumes2

  1. Maps a host file or dir to container file or directory.
  2. basically two locations pointing to same file.
  3. Skips UFS, host files overwrite any in container.
  4. Cant use Dockerfile, has to be mentioned in docker container run command.
  5. -v [/host/fs/path]:[/container/fs/path]

  6. Try

    docker container run -it -d -p 3000:80 --name nginx -v /home/nishant/Desktop/Docker-Exploration/htmlexample:/usr/share/nginx/html nginx:latest
    

Docker Compose

docker-compose CLI

Command Description
docker-compose up setup volumes,networks and start all containers
docker-compose up -f file_name setup volumes,networks and start all containers with a custom file_name
docker-compose down stop all containers and remove containers/vols/nets
docker-compose up -d setup volumes,networks and start all containers and detach
docker-compose ps get services running
docker-compose run  
docker-compose stop  

docker-compose versioning

There are three legacy versions of the Compose file format:

Containers Everywhere

Some major tasks

Docker Swarm - container orchestration

swarm5

swarm1 swarm2 swarm3 swarm4

docker swarm init

Command Description
docker swarm init initialize
docker node ls list down nodes
docker service create creating a container service
docker service ls list down services
docker service ps service_name process information
docker service update service_id –replicas number update replicas
docker service rm service_name remove service and delete all containers one by one

docker-service1

docker-service2

PLAYGROUND

Steps

dokcer-swarm1

docker-swarm2

docker-swarm3

docker-swarm4

docker-swarm5 docker-swarm6

Overlay Multi Host Networking

Command Description
docker network create –driver overlay network_name create a overlay network
docker-network1 creating a network
docker-network3 creating two services on one network
docker-network2 accessing them by their service name (look at host)

Routing Mesh (Internal Load Balancer)

docker stack

Production Grade Compose

Command Description
docker stack deploy -c compose_file app_name queue deploy services from a compose file
docker stack ls list all the apps in the stack
docker stack ps app_name list down services in the app
docker stack services app_name gives important info about services like replicas,mode etc.

docker secrets

Command Description
docker secret create secret_name secret_file.txt put value in secret by a file
echo “some_value” | docker secret create secret_name - put value in secret by echoing
docker secret ls list down secrets
with service  
docker service create –name service_name –secret secret_name create a service with a secret mentioned that can be used by container
docker service update –secret-rm secret_name remove secret

Swarm App LifeCycle

Three important things in this trilogy is swarm, stack and secrets

$ docker-compose up #for development env
$ docker-compose up #for CI env
$ docker stack deploy #for production env

Kubernetes

sandbox

Other flavours

Cloud providers

Terminologies

in play with k8s

Snaps Description
kubectl get nodes get nodes connected to the cluster
kube1 starting master node (command already provided with k8s playground)
kube2 getting version (one client and one server )
kubectl run my_nginx –image nginx kube3 run a pod
kubectl get pods kube4 get pods
kubectl create deployment my-nginx –image nginx kube6 kube7 create deployment
kube5 get all contents
kubectl delete deployment my-nginx delete the deployment
    Pods --> ReplicaSet --> Deployment

kube6

Scaling ReplicaSets

kube9 kube10

Snaps Description
kube11 logs
kube12 logs follow changes and tail last 1 line logs
kube13 describe pod/deployments etc
kube14 watch

Service Types

ClusterIP (default)

NodePort

LoadBalancer

ExternalName

Snaps Description
kube15 create service expose port with cluster IP
kube16 create service NodePort. different than docker as left port if internal port and right one is node port for outside cluster
kube17 create service with LoadBalancer
kube18 namespaces

Kubernetes Management Techniques

Generators (Automation behind commands)

Snaps Description
kube19 Get Generator info for deployemnt
kube20 Get Generator info for job
kube21 Get Generator info for expose
Imperative Decalarative
how program operates what a program should accomplish
ex.- making your own coffee ex.- give instructions to a barista
not easy to automate automation is good
know every step dont know current state, only final result is known
- requires to know all yaml keys

Management approaches

Kubernetes Configuration YAML

info Snaps Description
cluster kube22 cluster info
kind kube23 api resources (kind will give info for yaml file)
apiVersion kube24 api versions
metadata - only name of the service is required
spec - all the action
explain services recursively kube25 explain services get keywords
explain services description kube26 explain services get keywords
explain deployments description kube27 explain services get keywords
Snaps Description
kube28 find the difference between running service and updated yml

Labels and Annotations

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Label Selectors

in above example the resources are going to match labels from selectors to classify nodes and apply things.

Storage in K8s

Initial idea behind containers to be immutable, distributed and replaceable (in hindsight statefulness came later on as feature to have something stored to be used if container instance changes like database)

Ingress Controller

Custom resources

Reference

Simply just additional API extensions that are not default in k8s but they can be part of k8s functionality once added.

Higher Deployment Abstractions

New things CNAB and docker app

Namespaces

user@user~/$ kubectl get namespaces
user@user~/$ kubectl get all --all-namespaces
user@user~/$ kubectl config get-contexts

Docker Security

Reference

https://docs.docker.com/engine/security/

https://sysdig.com/blog/20-docker-security-tools/

Docker Bench Sceurity

https://github.com/docker/docker-bench-security

in a bunch of docker official images available online, there are users created groupadd & useradd. Our job while using those images is use the user mentioned and not run the image with root previleges.

WORKDIR /app
USER <user_name>