Tuesday, February 1, 2022

Day4 - Docker , docker swamp, docker compose

Day4 - Docker  - class notes
Docker Network
  - Bridge
  - Host
  - None ->
Volumes
  - Mountpoints
  - Persistent data
How to run docker
  - as a job
  - As a service
Ports
  -p
  -P
Image -> From running containers
Push -> Registry
Docker Hun
ECR
Quay
Jfrog artifactory
Nexus repo
Image creation
Image: docker commit containerID
Developer code/release/microservices
  - Avoid manual process
SHould be able to perform
  - pull
  - Run
  - copy
  - configure
  - save (commit)
We want to avoid these above step perform not manually but automatically. How can we do it?
- By using image build as a code concept. 
We use Dockerfile and specify all the stuffs there.
Dockerfile is going to help us to build the image. We will specify the base image, specify the packages needed. what files you need to cpoy, what ports, what env, what packages you want to download, what script do you want to execute.
Docker file contents,
  - Image
  - packages
  - copy, download, copy files, certificates
  - expose (ports such as 8080, 5000)
  - Env
  - initscript
Dockerfile
FROM ubuntu # what image do you want to specify, here we specify ubuntu, so it will download ubuntu image
RUN apt install default-jdk* -y # install jdk packages
ADD . /app   # copy everything from current directory to /app on the container
WORKDIR /app  # /app is going to be home directory for the user login.
EXPOSE 8080  # exposting the port 8080
CMD [java -jar abc.jar]  # start a service, when system boots up, service starts automatically. Run initial command. (You can call it as a boot strap script)
so the dockerfile content is as floows
# cat Dockerfile
FROM ubuntu
RUN apt install docker-jdk* -y
ADD . /app
WORKDIR /app
EXPOSE 8080
CMD [java -jar abc.jar]
$ docker build -t web . # [ docker build -t (tag)
when building the image, it will install ubuntu and at the build time, it will install something (jdk in our case) at the time of build. Run command helps
cmd command runs the java -jar abc.jar.
-----------------------
LAB
# start your VM, and login to the VM.
github.com/qfitsolutions/docker/


FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py]


git clone github.com/qfitsolutions/docker.git
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
========================
open new session and monitor
# docker ps -a  # watch, 
===========================
# docker --help
# docker build --help
-t - tag
-f - name of the file path
# docker build -t web:latest .
. means it will refer the dockerfile from the current directory.
observe the output. 
dockerfile is analyzed and proceed...
it performs 5 steps,
- it is pulling the image
- added the content. /code
- running in
- removing intermediate container (docker build command when downloaded the image, starts a container, add content, and it removes it self,
it starts a new instance and starts next step -> app.py
installing packages, removing .. just look at the output. 
# docker ps -a # command shows the imtemediate container. keep running
Run the commands inside requirements.txt 
# cat requirements.txt
flask
redis

Review this file,
https://spring.io/guides/gs/spring-boot-docker/
# cat Dockerfile
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Google,
Entry-point docker example
# cat Dockerfile
FROM ubuntu-trusty
CMD ping localhost
if you build this image, it will give you ping output
# docker run -t demo 
# docker run demo hostname
# docker run --entrypoint hostname demo
here hostname was run in place of ping command. It is not executing ping command but hostname is overwriting the default command.
so default entry poing is replaced.
$ docker run --entrypoing hostname demo
overwride the default cmd command.

# docker run -d demo
# docker ps -l
# docker exec 

FROM ubuntu:trusty
cmd can overwide default behaviour.

CMD you can override, on ENTRYPOINT, you can't overwride.
Google for
Dockerfile example
https://docs.docker.com/get-started/02_our_app/
https://docs.docker.com/samples/dotnetcore/
There are multiple repositories, modules available.
When you have multiple micro services, you have single application, you have multiple microservices, how do you start whole environment. 
Even, on dev, Test,Prod env?
Microservices
- Build all images


Google microservice architecture
https://microservices.io/
https://microservices.io/i/Microservice_Architecture.png
on the image above, there are 7 microservies.
4 micro-services have 3 dbs, different images.
Here, we are going to run Docker run command.
we have to run 'Docker run' command multiple time.
we will define image, run docker run multiple time and variable img
such as 
Array:[img1 img2 img3]
For loop:
  Docker run $img
but the problem is say we will us port, network, volume, name or more.
Some case some option may needed and some may not needed.
it becomes complecate.
rather than that, we use yaml code.
we will specify dynamic input
for eg,
nexus
 img
 port
Jenkins:
 img
 port
 Volume
 network
Maven:
 img

So, what is yaml file?
its like an xml file. 
xml, json, yml -> these all keep data. carry data.
we have Map and list
in yaml, we us map and list
kep pair
Map:
  Key: value
  course: devops
  City: DC
List # multiple values, not only key pair, muitiple values
  Cities:
   - DC
   - NY
   - LA
Fruits:
  Banana
  Apple
  Orange

List, specify multiple values...
services:
  jenkins
  nexus
  maven
  spring

services:
  jenkins:
    img: jenkins
    port: 8080
    network: mynw
  nexus:
    img: nexus
    Port: 8081
  maven:
    img: maven
  spring:
    img: mysprint
    Port: 8443
Docker will read it.
We will use docker-compose can read this yml file. 
docker-compose -f abc.yml/docker-compose.yml up
 
to start multiple containers
$ docker-compose -f abc.yml/docker-compose.yml up
 
How can we execute?
login to your system 
# cat docker-compose.yml
version: '3'
services:
  web: # web application, which we name as 'web'
    build: . # build a new image, 
    ports:
     - "5000:5000"
  redis: # backend application, pulls the image and builds
    image: "redis.alpine"
# docker-compose
no command found
apt install docker-compose
we need to install it. 
# apt install docker-compose -y
# docker-compose
you will see options
you see, start/stop, rm, run, build and much more options
# docker compose -f docker-compose.yaml up -d
up is sub command
-d -> run in background, deatach mode.
it will successfully builds first and after 2nd service.
two containers at a time.
# docker-compose ps
rather than docker run, use docker-compose command.

ip:port
if page is not displayed, 
go to instance, security group and add 5000 port - anywhere.
and refresh the page, you should be able to see the page.
======================================
try this one too and see what happens.
# docker run -itd --name web --image redis:alpine -p 5000:5000
docker is used to run single container at a time where as docker-conpose is sued to run multiple containers at a time.

- docker swarm
- Kubernetes
- Mesos
- tanzu
docker swarm is implemented by docker while k8s is by kubernetes by google.
google docker swarm atchitecture.
- internal distributed state sotres
- Manager manager manager
worker worker worker worker worker
its very simple to bring it into 
there are few commands
# docker
look at under management commands.
# lets make current machine as a maanger
# docker swarm
read the output help content.
# docker swarm COMMAND --help
# docker swarm init
it initilizes.
current node is a manager.
to join other nodes.
$docker swarn join --token <token id> 192.168.10.20:2377
Create a new aws instance as swarm-worker node
use t2 large to create new instance.
install docker command
$ apt update
$ apt install docker.io
run the join 
$ docker swarm joing --token <tokern no> 192.168.10.20:2377
you may get error, 
add inbound rule and add port all traffic / all traffic
now should be able to work without issue. if you get error, keep adding the error based on the error or take the corrective actions.
# docker swarm leave
# rejoin using the above command - docker swarm join...
# cd docker
# vi docker-compose.yaml
web:
  image: "devopsjuly22017/web:latest"
  ports:
   - "5000-5000"

# docker stack
# docker stack deploy -c docker-compose.yml mystack



# docker node ls
read about k8s at kubernetes.io

service
x

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...