DOCKER DAY 4 (Project :-How to deploy 3 Tier Architecture on Top of Docker)
Run the instance and connect with it
docker info command to check wether the docker is runnning
For launching an application we need an environment and here we are going to depploy our application on the docker container (OS)
For that we will go to check the docker image of WORDPRESS ON DOCKER HUB .
For having WordPress we need MySQL
Docker duty is not to maintain the database, docker is not there to develop applications.
DOCKER is there to give you platform or i can say environment to set up a 3 Tier Architecture.
If user want to make vlog you need wordpress web app , if u want to deploy it you need an os (which would be docker container) for that we have docker image to download Wordpress.
Wordpress only support MySQL Database so we need to launch another os for MySQL deployment and check out image on docker hub for downloading.
Check wether its downloaded or not by docker images command
And we can see we downloaded it!!
Now launch the container by the command
docker run -dit — name db -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=mydb -e MYSQL_USER=Prateek -e MYSQL_PASSWORD=redhat mysql:latest
-d
: Detached mode. It runs the container in the background and prints the container ID.
So, altogether, this Docker command creates a detached, interactive container named “db” based on the latest MySQL image. It sets up the MySQL root password, creates a database named “mydb”, and creates a MySQL user with the username “Prateek” and password “redhat”.
Let Launch the container for WordPress , this time i want this application is accessed by the client and the client is sitting somewhere outside or anywhere on the internet.
Containers are isolated nobody from outside can access them they have local connectivity like one container can access another which isa by default behavior and its good for confidential applications.
But now we want to expose this Wordpress container to outside users
-p 8080:80
: This option maps port 8080 of the host machine to port 80 of the container. This means that any traffic sent to port 8080 on the host will be forwarded to port 80 inside the container. Port 80 is the default port for HTTP traffic, so this setup allows you to access the web server running inside the container from the host machine's port 8080.In short by this outside user can connect with this container.
now copy the public Ip address and tpe it in the url with:8080 and u can see the wordpress page
Now we can set up everything according to database we launched on docker container.
Now we can see the interface of WordPress and can publish a vlog
http://52.70.254.174:8080/2024/02/19/hello-world/ this the URL i can copy and check the vlog from anywhere but actually it hosted on my container !!
Challange with 3 tier architechture (data Loss)
If our Database container deleted by default then what we do how we use WORDPRESSS so what we have to do it make it persistent by store it in /var/lib/mysql
command : docker run -dit — name db -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=mydb -e MYSQL_USER=Prateek -e MYSQL_PASSWORD=redhat -v /mydata:/var/lib/mysql mysql:latest
its usually called as volume mounted -v so that we can launchour container in a single click if it goes down
The Issue of IP linking The Issue of Data Loss can be partially solved with the volume mounting, but here there will be another issue in this architecture, that is the issue of IP Linking.
When docker creates a new container, it automatically assigns some unique ip to the container dynamically based on some specific range.
in the above example, as one container deleted and again launched, docker gave the same ip and hence there was no problem.
But this will not be the case every time, as if there are intermediate container launched before the dbSERVER container, then the dbSERVER container will get another ip which is not linked with the wordpress webSERVER container configuration settings.
To deal with the issue there is one old way of doing it that is to not to rely on the dynamic ip address, but it does not solve the issue completely.. As the containers are under the same network they can connect through the container names instead of ip address, for that we have to use the option — link.
IP address may change, but we can keep the container name unique every time,
we can use this container names to connect the container together instead of ip address,
— link node-two
Here, we are linking the node-two container with the node-one container while launching it. So, node-one can ping the node two directly but we can not ping the node-one from node two directly.
Create node-one
Create node-two and link them and attach and for checking it ping node from other node.
This Concept is known as container linking
Docker by default will not allow the pinging via container names, but we achieve this by using this concept. Here the problem is internally the container name is decoded into ip address at the time of its creation, so even if we link and dbSERVER and give the host name as the container name, the same problem of dynamic ip will occur, in case if the container goes down and intermediate container launched in between will give the same dbSERVER container a new unique changed ip address, which the wordpress container can not resolve to.
Thank you!!