Today we will learn topics related to Docker

Today we will learn topics related to Docker

  1. Creating a container from an image

  2. How to add volume to the container for backup

  3. How to push Image to the docker-hub from your local machine

  4. What is Docker compose and docker-compose installation and what are advantages of docker compose.

  5. What is Docker Swarm and the uses of it in Industry and how to connect it and run container's through Docker Swarm.

Here at first we need a repository link to clone it to the local :

Create an EC2 instance of Linux ,Ubuntu flavour:

The commands to use here are

  1. sudo apt-get update && Sudo apt-get install docker.io -y

  2. sudo usermod -aG docker $USER (here we will add docker to the group)

  3. clone the repo: #git clone https://github.com/uzair3399/django-todo-cicd.git

  4. The perform cmd #ls and vim Dockerfile (This is the file we will create an image from )

  5. FROM python:3

    WORKDIR /app

    RUN pip install django==3.2

    COPY . /app

    RUN python manage.py migrate

    EXPOSE 8000

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

docker build . -t django-todo-app

This will create an image from the docker file and then we will create an container from the image.

The type command to see the process state

docker ps

Then to run the container the command is

docker run -d -p 8000:8000 django-todo-app:latest

Open the port 8000 in the security groupd>inbound rules> edit inbound rules>port 8000>save changes.

Take the public ip of instance eg: http://54.196.54.117:8000 and press enter the application is live.

Done BOoooom in 3 to 4 steps we can make application live straight up from taking the code from repo to build an container from image and then running the container is this easy.

Now if we Add something to this docker file and if we kill the container the file is gone , So here we have to create a volume and attach it to the container ,so that if unfortunately we delete the container the information or backup is not vanished .

So adding Volume to the container:

The commands are

cd ../.. (The command to come directly the home screen)

  1. mkdir volumes/

  2. cd volumes/ mkdir django-todo

    It will look a like this

  3. Then type the command of present working directory

    pwd

    It will give output like this ,we will require this path forward

  4. This is the command to create a volume:

docker volume create --name django_todo_volume --opt type=none --opt device=/home/ubuntu/volumes/django-todo --opt o=bind

5.The command to inspect the created volume is

docker volume inspect django_todo_volume

Then after the volume is attached to the container you can check it by #docker ps

Here we will build a container from the image

docker run -d -p 8000:8000 --mount source=django_todo_volume,target=/app django-todo-app:latest

Now add anything to the docker file and then kill the container , The file will not be deleted it will be present in the volumes section

Now here we come across with the Docker-Compose concept and then the installation process in easy steps and the uses of it.

Definition: Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Compose works in all environments: production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:

  • Start, stop, and rebuild services

  • View the status of running services

  • Stream the log output of running services

  • Run a one-off command on a service

  • Advantages of Docker Compose:

    • Single host deployment - This means you can run everything on a single piece of hardware.

    • Quick and easy configuration - Due to YAML scripts.

    • High productivity - Docker Compose reduces the time it takes to perform tasks.

      Installation is very easy:

    • sudo apt install docker-compose

    • so we have to create a docker-compose.yml

    • This is a docker compose file we have to run this file

    • To run the docker-compose file the command is

    • docker-compose up

To run the docker-compose file in the background

docker-compose up -d

As now the MySQL service has been started:

Now we come across Docker Swarm and tackling the questions such as what is docker swarm? Advantages of docker swarm and most important how to connect through different instances through docker swarm?

Defnition: A Docker Swarm is a container orchestration tool running the Docker application. It has been configured to join together in a cluster.

Docker Swarm is a clustering and scheduling tool for Docker containers. With Swarm, IT administrators and developers can establish and manage a cluster of Docker nodes as a single virtual system.

Swarm mode also exists natively for Docker Engine, the layer between the OS and container images.

Advantages:

  • Docker Swarm is easy to install and set up.

  • A smooth learning curve makes the tool an excellent choice for beginners in container orchestration.

  • The tool has automated load balancing within Docker containers.

  • Lightweight and easy to use (especially if you are already familiar with Docker)

I'll show you the easiest way to connect the docker swarm

The steps first and then the slides:

  1. Create two instances of Ubuntu.

  2. Denote one instance as master node and the other instance as the worker node.

  3. Now type the command on both the instances

    sudo apt-get update && Sudo apt-get install docker.io -y

    sudo usermod -aG docker $USER (here we will add docker to the group)

  4. Now enter the command to start the docker swarm

    docker swarm init

  5. This will create a token in the master node and then enter the token in the worker node . This will connect the master and worker node.

    Now you can also check that the node is connected by command

    docker node ls

  6. This will show the node and also the connected node to it the master node is called as leader

  7. Now you can run any service in the master node and it will redirected to the worker node eg: I ran the below service and it was started at the worker node

  8. docker service create --name mysql-service --replicas 3 --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7

  9. You can also scale the services up and down by command

    docker service scale mysql-service=4

  10. Now by performing the cmd docker ps on the worker node we can see the services started by the master node and assigned to worker node as master node is declared as docker swarm

Now to send any image of any application created to your docker hub account the steps are:

  1. Type the command to login to the docker account from your'e vm machine

    docker login

  2. Enter youre specific docker-hub ID and Password.

  3. Then we have to tag the image we have built through a Dockerfile

    The command for it is

    docker tag django-todo-app uzairbagwan/django-todo-app:latest

  • Now push the image with the command

    docker image push uzairbagwan/django-todo-app:latest

  • Now check the image in your Docker-hub account

  • Done!!!

  • In this manner we can login>tag>push>docker image>To Docker-hub

  • Thank you

  • Till then Stay Tuned!!!