Has VM's that this vm's is complete vm's that has cernel and ultimate virtual infrastructure(Resources like ram,cpu,.. and cernel files)
Has containers that using needed os infrastructure and have not complete infrastracture(Resources like ram,cpu,.. and cernel files) itself
sudo docker pull <docker name>
sudo docker images
sudo docker ps
sudo docker ps -a
sudo docker run <docker name> if doesn't exist it pulls it
sudo docker run busybox [collection of common linux commands(ls, cat, sleep,...)] ------ > like sudo docker run busybox ls
sudo docker rm <CONTAINER ID> ---> delete contaiers that exited in <sudo docker ps -a>
sudo docker container prune
sudo docker rmi <image name> ---> remove image
sudo docker container prune ---> delete all contaiers that exited in <sudo docker ps -a>
sudo docker system prune --all --volumes --force ----> It's very important it's for deleting all storage occuped by docker in the above approached your storage doesn't chages
sudo docker run --rm
-i = --interactive: Keep STDIN open even if not attached
-t = --tty: Allocate a pseudo-TTY
sudo docker run --rm -it busybox
sudo docker exec 9f14f56a57d1 touch /hi ------>
sudo docker exec -it f6ee306b43b7 bash (Go to the image running shell or connect to f6ee306b43b7(This is running redis container) by bash (This is like ssh))
sudo docker stop f6ee306b43b7 (stop running images)
sudo docker run --rm --name redis redis
sudo docker exec -it redis bash
sudo docker exec -it redis bash redis-cli (run command line directly)
When you run a docker image on special port(e.i 6379 --> redis) you con connect it to your lacal's ip
sudo docker run --rm --name redis -p6379:6379 redis
When you run a docker image on special port(e.i 6379 --> redis) you con connect it to your lacal's ip and use redis your local (e.i use it in your flask app)
sudo docker run --rm --name redis -p6379:6379 redis
Above methods had a problem, that is when you disconnect your redis, its data removed and when you run it again you haven't previous data of redis.
To resolve this type of problem you should read image on docker hub and find "persistent storage" section that creator told us how to use his image to maintain data, for example in redis we have this command:
docker run --rm --name redis -p6379:6379 redis redis-server --appendonly yes
When you run docker by above command after exitition of redis and go to bash you can see appendonly.aof is created in /data
But again we hava a problem that is data stored is in docker not in your local the solution is 'VOLUM' :
As you can see in this picture you can store piece of your stored data of docker in your local by -v or --mount(more information) this command connect your local /tmp/data/redis(your create it) to /data(from your redis conrainer):
docker run --rm --name redis -p6379:6379 -v /tmp/data/redis:/data redis redis-server --appendonly yes
Now to run our app.py by docker we shoul use ubunto or Alpine (light linux), python, flask, nginx, wsgi(for connect nginx to our flask) dockers but a docker to this, its name is tiagolo/uwsgi-nginx-flask tiangolo :)
# use this tiangolo image
FROM tiangolo/uwsgi-nginx-flask
# Add flask requirements to run flask
COPY ./requirements.txt /tmp/
# By RUN you can run any linux commands
RUN pip install -r /tmp/requirements.txt
# Copy our app.py to app/main.py(from tiangolo docker image that tiangolo made it)
COPY app.py /app/main.py
As you can see in below picture we have two containers myapp and redis and two contaiers can be seen by OS but thay can't see each gether and now we want to connect them by create a network:
sudo docker network create flask
# You can see your network by
sudo docker network ls
sudo docker run --rm --name redis -p6379:6379 --network flask -v /tmp/data/redis:/data redis redis-server --appendonly yes
sudo docker run --rm -p80:80 --name myapp --network flask myapp:v3
But it doesn't work because two container connect to OS that it's not necessary anymore and dockers can see each other by container names.
# removing p6379:6379
docker run --rm --name redis --network flask -v /tmp/data/redis:/data redis redis-server --appendonly yes
# Notic that we dont change p80:80 in our image
localhost ---> redis
r = redis.Redis(host='redis', port=6379, db=0)
And Done and you can run curl 0.0.0.0:80
and see your result also you can ping redis in your myapp bash like this:
sudo docker exec -it myapp bash
> ping redis
version: "3"
services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"