Docker - Package Management

Docker - Package Management

Docker or we can say containerization tool.

The container is like a Virtual Machine.

Docker is a tool that creates these Virtual Machines or you can say Docker is the advanced version of Virtualisation

1. What is Docker?

Docker is an open-source centralized platform designed to create, deploy and run applications.

Docker uses a container on the host O.S to run apps. It allows apps to use the same Linux kernel as a system on the host computer, rather than creating a whole virtual O.S.

2. Advantages of Docker?

  • No pre-allocation of RAM.

  • light in weight and less cost.

  • It can run on physical hardware, virtual hardware and on the cloud.

  • You can reuse the image.

3. Architecture of Docker?

4. Docker Ecosystem:

Docker Daemon/Engine

Docker Daemon runs on the host O.S. It is responsible for running containers to manage docker services.

Docker Daemon can communicate with other Daemons. It is a Background process.

Docker Client:

Docker users can interact with the docker daemon through a client. [CLI]

Docker Client uses commands and rest API to communicate with Docker Daemon.

Docker Host:

It is used to provide an environment to execute and run applications. It contains the docker daemon, images, containers, networks and storage.

Docker Hub/Registry:

Docker Hub manages and stores the docker images.

There are two types of registries in the docker.

  1. Public - called Docker Hub

  2. Private - use to share images within the enterprise.

Docker Images:

Docker Images are the read-only binary templates used to create docker containers.

Ways to create :

  • Take image from Docker Hub

  • Create an image from the Docker file

  • Create Images from existing Docker Containers.

Docker Container:

A container holds the entire package that is needed to run the application.

The container is like a Virtual Machine, Images become containers when they run on the docker engine.

5. Docker Commands:

  1. First update system:

    sudo apt-get update

  2. Install Docker:

    sudo apt-get install docker.io

  3. To check Docker status:

    systemctl status docker

  4. Add Docker to the group to use it:

    sudo usermod -a -G docker $USER

  5. To see the active container list:

    docker ps

  6. To see all containers list (not active as well):

    docker ps -a

  7. To run an image as a container:

    docker run -it --name<container name> <image name> /bin/bash

  8. To see all images in Docker:

    docker images

  9. To start/stop Container.

    docker start <container name>

    docker stop <container name>

  10. To go inside a container.

    docker attach <container name>

  11. To check service started or not.

    service docker status

  12. To delete containers.

    docker rm <container name>

  13. To check the stats of a container, the memory consumed

    docker stats

To build MySQL image:

  • docker pull mysql -----> it will pull image from docker-hub

  • docker run -d -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest----> to run the image as a container.

  • docker ps -----> to see it in the container list

  • docker exec -it <conatiner id> bash -----> to execute MySQL and use it

  • mysql -u root -p -------> add password

6. PROJECTS:

1. [Dockerizing REACT_DJANGO_DEMO_APP and running it]

  1. create a directory

    mkdir projects

    cd projects/

  2. git clone https://github.com/LondheShubham153/django-notes-app.git -----> to pull code to the local

  3. go inside react_django_demo_app

  4. Create a Docker file

    vim Dockerfile

    FROM python:3.9

    COPY . .

    RUN pip install -r requirements.txt

    CMD ["python","manage.py","runserver","0.0.0.0:8000"]

  5. docker build .

  6. docker images ----> to see the image

  7. docker run -d -p 8000:8000 react-django-app:latest ------> to run as a container

  8. Now go to your EC2 instance, copy the Public IPv4 address and bind it with the port number 8000, and paste it into your browser.

2.[Dockerizing Node-todo-cicd-app and running it]

  1. git clone https://github.com/LondheShubham153/django-notes-app.git

  2. go inside node-todo-cicd

  3. create a Docker file

    FROM node:12.2.0-alpine

    COPY . .

    RUN npm install

    RUN npm run test

    EXPOSE 8000

    CMD ["node","app.js"]

  4. docker build .

  5. docker images ----> to see the image

  6. docker run -d -p 8000:8000 node-todo-cicd:latest

  7. Now go to your EC2 instance, copy the Public IPv4 address and bind it with the port number 8000, and paste it into your browser.

3. [Dockerizing Django-notes-app and running it]

  1. cd/projects

  2. git clone https://github.com/LondheShubham153/django-notes-app.git

  3. Vim Dockerfile

  4. docker build . -t django-notes-app

  5. docker images

  6. docker run -d -p 8000:8000 django-notes-app:latest

  7. Now go to your EC2 instance, copy the Public IPv4 address and bind it with the port number 8000, and paste it into your browser.