Reverse Proxy (load balancing ): forward requests from clients to backend servers which handle the requests.
API Gateway(distributing traffic): Acts as a single entry point for all API requests and distributing traffic
SSL/TLS Termination: it decrypts incoming traffic, inspects it, and then re-encrypts it before sending it to the backend servers.
Caching: NGINX can also be used as an HTTP cache, which can improve website performance by caching frequently requested content and serving it directly from memory
load balancing
All the above can be set in Config: etc/nginx/nginx.conf
Pre-Req:
Docker is already installed in the system.
Note:By default nginx runs on port 80
Example 1
-------------------------- nginx.sh ----------------------------
cd ~/Desktop
mkdir nginx
cd nginx
echo '\<html\>\<body\> hi\</body\>\</html\>' >> test2.html
--------------------------------------------------------
docker run -it --rm -d -p 8080:80 --name web -v $PWD:/usr/share/nginx/html nginx
Snapshot of the application containing all the libraries , supporting files etc.,
$docker pull python # download new image
$docker images #list all active images saved
$docker rmi -f image_name #delete image , -f force
$docker system prune -a #delete all images + containers
Container
Containers are similar to virtual machine
Separate IP Eg: ping container_ip #Talk to host / outside world
Separate file system
It has 2 states
Start
Stop
containers share kernel with the host machine.
Data gets removed if the container is exited / removed.
START and STOP
$docker run -it --name test python:3.7 bash #download + start + go inside
$docker run nginx #download+start
$docker start -i container_id/name #start+go in
$docker exec -it container_id/name bash #go inside
$docker stop container_id/name #STOP
$docker kill container_id/name # STOP
$docker rm container_id/name # Delete
$docker system prune #remove unused images and containers
$docker ps -a #running continaers
$docker cp file_name container:/home/app
$docker logs containeir_id/name
$docker port containeir_id/name
$docker inspect containeir_id/name
$docker attach container_id/name #running container
$docker stats ContainerName/ID #mem,cpu,nw of running container live
Basic Commands Command Line
$docker version
$docker -v
$docker info
$docker command_name --help
$docker login
$docker pause ContainerName/ID #$docker unpause ContainerName/ID
$docker commit Container new_image_name #convert running container to image
Bind Mounting / Volume Mounting / Folder Mounting
Independent storage outside of container inside
echo "print('hi')" >>test.py
docker run -it --name test -v $PWD:/home/app/ -w /home/app/ python:3.8-slim python test.py #hi
now change the contents of test.py to "print('hello')"
save
docker start -i container_id #hello
Docker Execution Types
1. Command Line docker :
Create a folder
echo "print('hi')" >>test.py
$docker run -it --name test -v ${PWD}:/home/app -w /home/app python:3.9-slim python test.py
2. dockerfile
FROM ubuntu
ARG build_no # input arg
ENV env_var=$build_no # Env var
RUN apt-get update # restarts after installation
RUN apt-get install -y python3
COPY . /home/apps # Copy files to container
WORKDIR /home/apps/ # Default working Directory
EXPOSE 8080/tcp # expose to other containers only , use run/compose for port mapping to outside world
CMD echo $env_var # both CMD and ENTRYPOINT can be used only once. (ENTRYPOINT has higher priority)
3.docker-compose.yml
Create another file "docker-compose.yml" (make sure step 2 is done)
echo "print('hi')" >>test.py
docker run -it --name test -v $PWD:/home/app/ -w /home/app/ python:3.8-slim python test.py #hi
now change the contents of test.py to "print('hello')"
save
docker start -i container_id #hello
2. Redis : Has only Databases from 0-15 (data moved to next db after previous is filled)
docker run --name my-first-redis -d redis #detached mode
docker exec -it my-first-redis sh
$redis-cli
$ping #pong
$INFO #notice 'KEYSPACES'
$ SELECT 0 #select db
$ SET A 1
$ KEYS *
$ GET A
quit
exit
docker run -p 8080:8080 jenkins/jenkins:lts-jdk11
Open localhost:8080
password - in logs\
can also be be inside Jenkins_Home/secrets/initialAdminPass
4. Flask:
#-----flask.py------
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world() -> str: return "Hello world"
if __name__ == '__main__':app.run(debug=True, host='0.0.0.0')
#----------
docker run -it --name myflask1 -p 5000:5000 -v ${PWD}:/app python:3.7 bash
pip install flask==1.1.1
cd /apps
export FLASK_APP=flask.py
flask run --host=0.0.0.0
open link : localhost:5000
5. Django
- Create an empty folder in local machine
- cd to that folder
- docker run -it --name mydjango1 -p 8000:8000 -v ${PWD}:/app python:3.8 bash #open docker terminal
- pip install django==2.2.6 #
- cd /app
- django-admin startproject testsite
- Notice the testsite is created locally
- Under settings.py , add ALLOWED_HOSTS = ['0.0.0.0']
- cd testsite
- python manage.py runserver 0.0.0.0:8000 #ctrl+C to exit
- Now run all steps again and instead of replace #4 with pip install django
$docker swarm init --advertise-addr <copy_ip addr from above >
copy "docker swarm join --token xx"
Open New Instance / Node
Paste $docker swarm join --token xx"
Go back to Master
$docker service create -d --name nginx -p 8080:80 nginx
$docker service update nginx --replicas 3
Test
Goto each Node
docker ps -a
docker logs
Commands :
$docker service ps nginx
$docker service ls
$docker service ps nginx
$docker service rollback nginx
$docker node ls
$docker service ls
$docker network ls
$docker inspect node1