Deploy Flask Restful Api Nginx Docker image on Docker | Pull the Docker image | Execute the Docker image

Following are steps to Deploy the Flask Restful API on Docker:


Once you have connected to EC2 instance using PuTTY then follow these instructions.

1) Install Docker

[ec2-user@ip-172-31-20-251 ~]$ sudo yum install -y docker

2) Start the services of the Docker

[ec2-user@ip-172-31-20-251 ~]$ sudo service docker start

[ec2-user@ip-172-31-20-251 ~]$ sudo usermod -a -G docker ec2-user

3) Run the Superuser command to get all the permission to perform other task

[ec2-user@ip-172-31-20-251 ~]$ sudo su

4) Verify that the ec2-user can run Docker commands without sudo.

[root@ip-172-31-20-251 ec2-user]#  docker info

  • In some cases, you may need to reboot your instance to provide permissions for the ec2-user to access the Docker daemon. Try rebooting your instance if you see the following error:
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Sign up for a Docker Hub Account

Docker uses images that are stored in repositories to launch containers with. The most common Docker image repository (and the default repository for the Docker daemon) is Docker Hub. Although you don't need a Docker Hub account to use Amazon ECS or Docker, having a Docker Hub account gives you the freedom to store your modified Docker images so you can use them in your ECS task definitions.
For more information about Docker Hub, and to sign up for an account, go to https://hub.docker.com.


[root@ip-172-31-20-251 ec2-user]#   docker login -u ghewadesumit
Password:
Login Succeeded

Download an image

  • Search for a base docker
[root@ip-172-31-20-251 ec2-user]# docker search "nginx python flask"

  • Download or Pull the docker image
[root@ip-172-31-20-251 ec2-user]# docker pull tiangolo/uwsgi-nginx-flask

Run the Docker Container with the downloaded image

  • Run the docker with port Mapping.
[root@ip-172-31-20-251 ec2-user]# docker run -p 80:80 tiangolo/uwsgi-nginx-flask

  • Check the browser if the site is up by entering only the public DNS.
  • Use ctrl -c to shutdown the container
  • Run the container with a user friendly name and in background
[root@ip-172-31-20-251 ec2-user]# docker run -d --name "hello_world"  -p 80:80 tiangolo/uwsgi-nginx-flask

Get Status of Containers

  • Use **docker ps ** to query the state of containers
[root@ip-172-31-20-251 ec2-user]# docker ps 
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                         NAMES
62fc645d32a8        tiangolo/uwsgi-nginx-flask   "/usr/bin/supervisord"   7 seconds ago 
  • Get the log generated by container
[root@ip-172-31-20-251 ec2-user]# docker logs hello_world

[root@ip-172-31-20-251 ec2-user]# docker stop hello_world

  • Create a docker file for generating our own Image


[root@ip-172-31-20-251 ec2-user]# vim Dockerfile

And paste following code into the file, 

FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY ./app /app

  • Create a uwsgi.ini file for Nginx - uwsgi inside the Flask application Folder
[root@ip-172-31-20-251 ec2-user]# cd app

[root@ip-172-31-20-251 app]# vim uwsgi.ini

copy and paste bellow code into the file

[uwsgi]
module = main
callable = app

  • Note: In the variable module = main,  main represents the python file which executes the Flask application. So put the name of the python file in front of module.
  • Callable variable holds the name of the Folder where the flask application files and folders are stored

In my case the uwsgi file will look like.

[uwsgi]
module = flask_cloud
callable = app

Check the directory structure

[root@ip-172-31-20-251 app]# cd app
[root@ip-172-31-20-251 app]#  ls
flask_cloud.py  static templates  uwsgi.ini
  • Build the docker image
[root@ip-172-31-20-251 app]# docker build -t webserver .

[root@ip-172-31-20-251 app]# docker run -d --name flask_demo  -p 80:80 webserver

To check the Docker image is running use command
[root@ip-172-31-20-251 app]# docker ps


Pushing or committing back to repository for reuse

After running the docker ps command copy the CONTAINER ID  and use it push the docker image onto the docker account that you have created on https://hub.docker.com with the help of profileid.

In my case my docker id is ghewadesumit

[root@ip-172-31-20-251 app]# docker commit 2b36d97a7943 ghewadesumit/2b36d97a7943

[root@ip-172-31-20-251 app]# docker push ghewadesumit/2b36d97a7943


To stop the docker image from running use this command

docker stop <your_container_id>

In my case

[root@ip-172-31-20-251 app]# docker stop 2b36d97a7943

Pulling the image from the hub.docker.com and executing it.

[root@ip-172-31-20-251 app]# docker pull ghewadesumit/2b36d97a7943:latest

To check the docker images present in the system use this command:

[root@ip-172-31-20-251 app]# docker images

[root@ip-172-31-20-251 app]# docker run -p 8080:8080 ghewadesumit/2b36d97a7943




Comments

Post a Comment

Popular posts from this blog

Making of Nike: Phil Knight's Leadership Style,Traits and Skills

My Experience of undergoing Bi Jaw Surgery

Every Thing wrong with AVENGERS ENDGAME