How to deploy two-tier application through Docker

How to deploy two-tier application through Docker

Step-By-Step guide to deploy two tier application with docker bridge networking

The First step is will go into the GitHub repository and clone the code “Two-tier flask app

We can use the git clone command to copy the code to my local machine.

Unfortunately, I have copied it to the /home directory then we moved the code with mv command the code in the Project/ directory with the mv command

& now the code two-tier flask app successfully clone into the directory /Projects.

Then go to the code in the Github repository and check the app.py file, click on that file & check the content. We have to connect MySQL database & we have to create MySQL docker container.

We have to give an environment variable while executing the mysql command, If you want to check more on environment variables then we can search for it on Google.

We can use different variables as per our requirement for example: to create a database and many more please refer below screenshot.

While creating a container we got an error as we didn't put any environment variables.

& the container has been created.

If we want to go inside the container we can use the command docker exec -it (interactive terminal) continued bash (shell name)

So here my database has been created inside the container.

Now the second step is to create a container for the flak-app ,So first we can create an image for flask-app.

Then we have to create a container with the docker run command & which port we have to expose this should be confirmed with the developers.

As we checked in the code the app is running on 5000 ports. but good practice is always communicating with developers.

Then we can go to the AWS Dashboard and in the security group we have to open port 5000.

We can try to access the application through the browser but we got an error Because we don't have a network to connect both containers, containers are isolated from each other.

Before proceeding further we have to place an environment variable for the flsk-app as well. Have a look at the code in the GitHub Repo:

& kill the container flask-app.

& again run & create container.

Again we got an error, It trying to connect with the local server its not connecting MySQL container.

We should have to connect with MySQL container

Then, first, we have to kill the existing container with the docker kill command cause we have to execute the docker run command again.

Post we can run the below command to give the name to the container –name "MySQL"

We can create a flask-app container and add the line —name flask-app given the name to the container.

We can try to connect through the browser with port 5000, now it's connecting somewhere.

We can use docker network ls command to check available networks

Again we have killed both the containers (MYSQL & Flask-app)

Then if we need to create communication between two containers, need to create a network between them. First, ensure that all the containers are killed and not in a running state.

Then create a bridge network with the docker network command.

The bridge network is the default network for the docker containers. The bridge network over here has been created -d flag is for the driver & A bridge network has been created. We have created the two-tier-app-nw.

Then we can run the following command to create a container & with the container name mysql & the bridge network has been created

We can check with the docker inspect command and the file name. We found that only MySQL container was there

In the same way, we have to deploy and create a container for flask-app as well.

& We can create th network “bridge” in that network our two containers are running & they can communicate with each other.

We have used --network two-tier-app-ne to add the container to the network.

We can check with the command “docker inspect two-tier-app-nw “ and confirm that two containers are running within that file.

Hurrey & this is the way to deploy two-tier applications through the docker.

So what we did was create one network and add two containers.

Happy Learning !!