Introduction
- Docker is a Container platform tool designed to make it easier to deploy and run applications by using containers
- Makes the process of application deployment very easy and efficient and resolves a lot of issues related to deploying applications
- Gives you a standard way of packaging your application with all its dependencies in a container
- Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
https://kodekloud.com/p/docker-labs
Architecture
Workflow of docker
Developer > Dockerfile > Docker Image > Docker Hub > Pull Image
Dockerfile
- describes steps to create a docker image - Docker images are stored in online repository called Docker hub
- Docker Hub - repository of publicly available images
- Docker images are pulled from Docker Hub to create Docker Container
- Docker Container are run-time instance of docker image
Docker has a client server architecture
Docker Engine = Docker CLI (Client)+ Daemon(Server)
Docker CLI
> commands > Docker Daemon
Docker server / daemon receives the commands from CLI /REST API
Advantages of Docker - Simplifies DevOps
- Build app only once
- Portability
- Version Control
- Isolation
- Productivity
Docker Installation
Docker Toolbox - Installer for quick setup ( Oracle VirtualBox )
Installed Components -
- Kinematic - GUI for Docker
- Oracle VM VirtualBox
Docker Client
for running Docker Engine to create images and containersDocker Machine
so you can run Docker Engine commands from Windows terminalsDocker Compose
for running the docker-compose
command
Docker Commands
Basic
docker version
- displays docker client and server versionsdocker info
- displays detailed info on containers(running, paused, stopped) and imagesdocker --help
- displays help on a command eg docker images --helpdocker login
- Connects with hub.docker.com with account
Images
docker images
- gives list of installed images with <imageid>docker pull <image>
- downloads an image from docker hubdocker rmi <imageid>
- Remove images
Container
docker ps
- List running containersdocker ps -a
- List all the scontainers- docker run --name MyUbantu -it ubantu > Run a command in a new container in foreground
- docker run -d nginx > detach the commandline in background
- docker start <containerid> - Start the container
- docker stop <containerid> - Stop the container
System
- docker stats - Details the memory usage,
- docker system df - Checks disc usage
- docker system prune - Remove all the unused/dangling images and stopped container
Docker Images
- Docker Images are templates used to create Docker containers
- Container is a running instance of image
- A single image can be used to create multiple containers
- Where are Images Stored
- Registries (e.g. docker hub)
- Can be stored locally or remote
- Docker image commands
- docker images --help
- docker pull <image>
- docker images -q - Shows the image id quietly
- docker images -f “dangling=false” > Filter images with condition
- docker run image > Create container out of an image
- docker rmi image
- docker rmi -f image > Remove image forcefully
- docker inspect
- docker history imageName
Docker Container
Containers are running instances of Docker Images
COMMANDS :
- docker ps
- docker run ImageName
- docker start ContainerName/ID
- docker stop ContainerName/ID
- docker pause ContainerName/ID
- docker unpause ContainerName/ID
- docker top ContainerName/ID > displays top process in the container
- docker stats ContainerName/ID > displays starts - memory
- docker attach ContainerName/ID
- docker kill ContainerName/ID
- docker rm ContainerName/ID
Jenkins on Docker
1. How to start Jenkins on Docker Container
- docker pull jenkins
- docker images
2. Start and Stop Jenkins Container
- docker run --name MyJenkins1 -p 8080:8080 -p 50000:50000 jenkins
3. How to set Jenkins home on Docker Volume and Host Machine (Persistent)
- docker run --name MyJenkins2 -p 9090:8080 -p 50000:50000
-v /User/Desktop/Jenkins_Home:/var/jenkins_home jenkins
- docker volume create myjenkins
- docker volume ls
- docker volume inspect myjenkins
- docker run --name MyJenkins3 -p 9090:8080 -p 50000:50000
-v myjenkins:/var/jenkins_home jenkins
- docker inspect MyJenkins3
Dockerfile
- A text file with instructions to build image
- Automation of Docker Image Creation
- sample
From <baseimage>
MAINTAINER sunil chandra <sunilchandra007@gmail.com>
RUN apt-get update
#run with container only
CMD ["echo", "Hello World.. from my first docker image"]
- docker build -t myimage:tag .
- docker run myimageID
Docker compose
- tool for defining & running multi-container docker applications
- Create a new file docker-compose.yml to configure application services
version: '3'
services:
web:
image: nginx
database:
image: redis
- Check the validity of yaml
- can start all services with a single command : docker compose up
- can stop all services with a single command : docker compose down
- can scale up selected services when required
- docker-compose up -d --scale database=4
Docker Volume
- Mechanism for persisting data generated by and used by Docker containers
- helps to decouple container from storage
- share volume(storage/data) among different containers
- attach volume to container
- deleting container does not delete volume
Volume drivers helps to share volume on remote hosts / cloud providers
Bind mount - a file /directory on a host machine is mounted into a container
docker volume version
docker volume create <myvolume>
docker volume ls
docker volume rm <myvolume>
docker volume prune - Remove all the volume not in use by any container
docker volume inspect <myvolume> - Display the mount point
Docker Swarm
- A swarm is a group of machines that are running Docker and joined into a cluster
- Docker Swarm is a tool for Container Orchestration
- You want to do
- - Health check on every container
- - Ensure all containers are up on every system
- - Scaling the containers up or down depending on the load
- - Adding updates/changes to all the containers Orchestration
- - managing and controlling multiple docker containers as a single service
- Tools available - Docker Swarm, Kubernetes, Apache Mesos
Create Docker machines
- docker-machine create --driver virtualbox manager1
- docker-machine create --driver virtualbox worker1
- docker-machine create --driver virtualbox worker2
Displays list of docker machine and its status
SSH Connect to docker machine(manager)
- docker-machine ssh manager1
Declare a machine as swarn/manager(work only in swarm manager and not in worker)
- docker swarm init --advertise-addr <MANAGER_IP>
Display the list of nodes in the swarn
- docker node ls
- docker info
Join workers in the swarm
- docker swarm join-token worker > provide command to run in worker node with token
SSH Connect to worker1 and run the token command to join swarn
- docker-machine ssh worker1
Run containers on Docker Swarm
- docker service create --replicas 3 -p 80:80 --name <serviceName> nginx
- docker service ls ---- List services
- docker service ps <serviceName> --- Lists the tasks of services
Scale service up and down
- docker service scale <serviceName>=2
Inspecting Nodes (this command can run only on manager node)
- docker node inspect <nodename>
Shutdown node
- docker node update --availability drain worker1
Update service
- docker service update --image <imagename>:version <serviceName>
Remove service
- docker service rm <serviceName>
docker swarm leave : to leave the swarm
docker-machine stop machineName : to stop the machine
docker-machine rm machineName : to remove the machine
Containers - wrap an application and all of its dependencies, such as libraries and configuration files, into its own isolated 'box' that contains everything needed to run the software.
Service Fabric Cluster
Docker Hub provides a central repository to store and retrieve container images.
Docker Build > Image > Docker Host
Play with docker
Docker for windows
- Docker Toolbox -
- Docker Engine - runs
docker
commands - Docker CLI client
- Docker Compose - runs
docker-compose
commands - Docker Machine - runs
docker-machine
commands