Table of contents
- Create an EC2 instance of Linux ,Ubuntu flavour:
- So adding Volume to the container:
- This is the command to create a volume:
- Now here we come across with the Docker-Compose concept and then the installation process in easy steps and the uses of it.
- 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?
- Now to send any image of any application created to your docker hub account the steps are:
Creating a container from an image
How to add volume to the container for backup
How to push Image to the docker-hub from your local machine
What is Docker compose and docker-compose installation and what are advantages of docker compose.
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
sudo apt-get update && Sudo apt-get install docker.io -y
sudo usermod -aG docker $USER (here we will add docker to the group)
clone the repo: #git clone
https://github.com/uzair3399/django-todo-cicd.git
-
The perform cmd #ls and vim Dockerfile (This is the file we will create an image from )
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)
mkdir volumes/
cd volumes/ mkdir django-todo
It will look a like this
Then type the command of present working directory
pwd
It will give output like this ,we will require this path forward
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:
Create two instances of Ubuntu.
Denote one instance as master node and the other instance as the worker node.
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)
Now enter the command to start the docker swarm
docker swarm init
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
-
This will show the node and also the connected node to it the master node is called as leader
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
-
docker service create --name mysql-service --replicas 3 --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7
You can also scale the services up and down by command
docker service scale mysql-service=4
-
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:
Type the command to login to the docker account from your'e vm machine
docker login
Enter youre specific docker-hub ID and Password.
-
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!!!