Orchestrating docker containers
docker version
- check version and see if everythings working accordinglydocker container run --publish 80:80 nginx
- pull image nginx and run it, forwarding connections from port 80 to port 80 inside container--rm
remove container upon exit-it
run commands, commands should be at last like.... bash
--detach
or-d
to run it in background--name <name>
to name the container explicitly.--publish
or-p
to expose ports- exposing multiple ports
docker run -d --name some-rabbit -p 5672:5672 -p 5673:5673 -p 15672:15672 rabbitmq:3-management
- exposing multiple ports
-e PATH=XXX
for environment variables--network <network name>
- connects container to the said network--network-alias <dns name>
- dns name for lookup-v <mysql-db:/var/lib/mysql>
- assigning a name to a volume- mounting on windows
docker run -it -v "d:/data:/data" -p 6379:6379 --name redis -d redis
- mounting on windows
-v </Users/test:/var/lib/mysql>
- bind mounting container to a folder, do note the path starts with a/
e.g.docker container run -d --name db -p 3306:3306 -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
docker container ls
- list out running containers-a
can be used to list all containers available
docker container stop <container ID>
- stopping docker instancedocker container start -ai ubuntu
- starting existing containerdocker container logs <name>
- list logs of the container-f
follow command to watch logs
docker container top <name>
- process running inside the containerdocker container --help
- list all commandsdocker container rm <name> <name>...
- remove the containers, can take in multiple values. running containers will need to be stopped first-f
- force remove of container even if it's running
docker info
- display info of docker, use this to check swarm mode
docker run -it --rm --privileged --pid=host justincormack/nsenter1
- connecting to docker VM sourceps aux
- list all processes| grep <name>
can be used to filter process names
docker container inspect <name>
- to return the configuration of the containerdocker container stats
- return streaming lives tats of all containers
docker container run -it
- start new container interactively+psuedo TTYdocker container exec -it
- run additional command in existing containerdocker exec -it <container_name> bash
docker container port <name>
- see ports opened on containerdocker container inspect --format '{{.NetworkSettings.IPAddress}}' webhost
- see host IP of docker containerdocker network ls
- show all networks createddocker network inspect <network name>
- shows network related configuration like containers connected to the network, IP and gateway of the networkdocker network create <network name>
- create a new network--driver overlay
or-d overlay
to create a network that spans across clusters
docker network connect <network name> <name>
- connect network to containerdocker network disconnect <network name> <name>
- disconnect network to container
docker image inspect <name>
- contains metadata like ports exposed, environment variables, command to run when an image is run
docker image tag <source_image> <new_image_tag>
- will create a new tag from the source image
docker image push <image_tag>
- push to dockerhub
docker volume create
docker login
- login
docker logout
- logout
docker rm -f $(docker ps -a -q)
- delete all containers
docker volume rm $(docker volume ls -q)
- delete all volumes
docker image rm -f $(docker image ls -a)
- delete all images
removecontainers() {
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
}
armageddon() {
removecontainers
docker network prune -f
docker rmi -f $(docker images --filter dangling=true -qa)
docker volume rm $(docker volume ls --filter dangling=true -q)
docker rmi -f $(docker images -qa)
}
`docker system prune --volumes` Use the -f or --force option to bypass the prompt.
`docker system prune`
`docker network prune`
FROM
all images must have a from normally from a minimal linux distribution
WORKDIR
change working directory, preferred over RUN cd /path
ENV
optional environment variable that is used for container building and running container
RUN
execute shell commands
VOLUME
mounting a volume for persistent data, this needs manual delete for assurance
EXPOSE
ports that are being exposed
CMD
command that will be run everytime we restart the container or everytime a container starts
COPY
copy from local our build machine into containers
docker-compose ps
- show all running docker-composedocker-compose top
- show statsdocker-compose up
- start docker-compose-d
detach container
docker-compose down
- stop docker-compose
docker swarm init
- initiate docker swarmdocker swarm join-token manager
- getting token of a managerdocker node ls
- list out all nodes/host/instances/serversdocker node update --role manager node2
- escalating privilege of a docker nodedocker service create alpine ping 8.8.8.8
- create a docker swarmdocker service ls
- show running services, underREPLICAS
is how many is running on the left vs how many you specified it to run on the rightdocker service ps <service name>
- - show us the taask/containers for the servicesdocker service update <service name> --replicas 3
- scaling up the nodesdocker service rm <service name>
- removing a service
docker-machine create <node name>
- creates a nodedocker-machine ssh <node name>
- ssh into the container
docker stack ls
- list stackdocker stack ps <stack name>
- see all tasks in the stackdocker stack services <stack name>
- see all services in the stackdocker stack deploy -c example-voting-app-stack.yml <stack name>
- deploying a stack
docker secret create <key name> <file path to password>
- pass in a file into a secret- do note that this require you to store the file locally which is a security concern. we can pass in through a remote API to bypass this
echo "<password>" | docker secret create <key name> -
- pass in the secret through command line- do note that this require you to use bash commands, someone with root access can see the history
docker secret ls
- show all passwords and keysdocker service create --name psql --secret psql_user --secret psql_pass -e POSTGRES_PASSWORD_FILE=/run/secrets/psql_pass -e POSTGRES_USER_FILE=/run/secrets/psql_user postgres
- creating container and passing in secretdocker service update --secret-rm
- to remove password, however when a secret is removed, the containers are redeployed(immutable design)
- Look for image locally
- Look in remote image repository
- Download latest version (nginx:latest by default)
- Create new container based off on the image
- Customize networking, give a specific virtual IP address in Docker Virtual Network
- Open ports in
--publish <from>:<to port>
forward port 80 from host, to port 80 in the container - Start container by using cmd in a Dockerfile
e.g.
docker container run --publish 80:80 --name webhost -d nginx:1.11 nginx -t
- docker are started with their own virtual network
bridge/docker0
by default. - containers with the same virtual network can talk to each other without having ports exposed
- if they are started with port
80:80
then the ethernet interface will start listening to connections at port 80
- docker containers can be added to the same network
- they will be referenced through each other using the
container name
regardless of what their IP address is docker container exec -it my_nginx ping new_nginx
- default network bridge does not has DNS service
- security and automation, creates root certificate for swarm and join tokenes for nodes to join
- enables swarm api and create raft consensus database, ensure consistency across multiple nodes
dockerfile-sample-1
- sample, ordering of docker commands matter. least change code should be at topdockerfile-sample-2
- copy .html into container while buildingdocker image build -t nginx-with-html .
docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
- mapping a volume
dockerfile-assignment-1
- dockerizing a node app and pushing it onto the cloudebindmount-sample-1
docker container run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve
binding html changes for webapps
compose-sample-1
- moving from dockerfile to docker-compose
upgrading database
using volumesdocker container run -d --name psql -v psql:/var/lib/postgresql/data -e POSTGRES_PASSWORD=password postgres:12.2
docker container run -d --name psql2 -v psql:/var/lib/postgresql/data -e POSTGRES_PASSWORD=password postgres:12.3
docker container run --publish 80:80 nginx
- pull image nginx and run it, forwarding connections from port 80 to port 80 inside container--rm
remove container upon exit
serving a changing html
using bind mountdocker container run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve
binding html changes for webapps
creating a swarm connected by network
docker network create --driver overlay mydrupal
docker service create --name psql --network mydrupal -e POSTGRES_PASSWORD=mypass postgres
docker service ls
check servicesdocker service ps psql
check running individual servicedocker container logs psql.1
checking logs of psqldocker service create --name drupal --network mydrupal -p 80:80 drupal
swarm-app-1
- deploying a cluster appswarm-stack-1
- automatically create list of services deployed to clustersecrets-sample-2
- creating secrets and passing secrets into a stackupdate the image to a newer version
docker service update --image myapp:1.2.2 <service name>
adding env variable and removing a port
docker service update --env-add NODE_ENV=production --publish-rm 8080
change number of replicas of two services
docker service scale web=8 api=6
scaling web services + rolling updates
docker service create -p 8080:80 --name web nginx:1.13
docker service scale web=5
- scalingdocker service update --image nginx:1.13.6 web
- updating imagedocker service update --publish-rm 8080 --publish-add 9090:80 web
- removing and changing a portdocker service update --force web
- rebalancing the tasks/workload in your node
adding healthcheck to a nginx
docker container run --name p1 -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres
- without healthcheckdocker container run --name p2 -d -e POSTGRES_HOST_AUTH_METHOD=trust --health-cmd="pg_isready -U postgres || exit 1" postgres
- container with healthcheckdocker service create --name p2 -e POSTGRES_HOST_AUTH_METHOD=trust --health-cmd="pg_isready -U postgres || exit 1" postgres
- service with healthcheck
kubectl run
- running a podkubectl create
- creating resource for CLI/YAMLkubectl apply
- use to update differencekubectl version
- get version of client/serverkubectl run my-nginx --image nginx
- create and run an imagekubectl get pods
- get list of services running(hidden some)-w
- watch command, to watch a linux command
kubectl get all
- get all services running including networks etc.kubectl delete deployment my-nginx
- cleanupkubectl delete pod/my-apache-5d589d69c7-ps6sj
- deleting a podkubectl scale <image name>
--replicas <count>
- scale image
kubectl logs deployment/my-apache
logs command--follow
--tail 1
- return last line only
kubectl logs -l run=my-apache
specifying labels to view all logs of different node at once. this is not a replacement to production loggin, use "Stern tool" for better log tailingkubectl describe pod/my-apache-5d589d69c7-fkwvv
- similar to inspect command in swarm, describe specific pods
- Master Node:
- etcd - distributed storage system for key-values (Similar to Swarm RAFT algorithm)
- API - Talk to cluster and issue order
- scheduler - controller how and where containers are placed on the nodes on object call pods
- Controller Manager - look at state of whole cluster and everything running in it, taking order given to it and determine the difference and do what you ask it to do
- CoreDNS - control network
- Node:
- kubelet - kubernetes agent running on nodes
- kube-proxy - to control the networking
- Pod - one or more containers running together one one node, basic unit of deployment, containers are always in pods
- Controller - creating/updating pods and other objects(controller, deployment, replicaset, statefulset, daemonset, job, cronjob)
- Service - network endpoint to connect to a pod(provide a consistent endpoint similar to DNS)
- namespace - filtered on the view of the kubectl commandline
- When a run command is issued the following controllers are created
- Deployment Controller -> Replica Set(use to manage if pod started is correct) -> Pod
- When we type a scale command, we are updating the deployment specification
- deployment change replicaset to a set of 2 controllers
- relpicaset controller decided to change to 2 pods
- control plane assigns node to pod
- kubelet agent will be assigned to the pod and be executed on local docker agent
scaling replica sets
using volumeskubectl run my-apache --image httpd
kubectl scale deploy/my-apache --replicas 2
- scaling to 2 services