Session 4
Session 4
In the previous session, we have explored on how we can create an api using flask, now we will cover the following:
- Docker
- containerize our flask app
- run docker container locally
- Deploy our container on cloud run(GCP managed compute platform)
Docker
Before understanding docker, lets look into what is container
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
To run a container, we need to create an image of the container and run it in container runtime. Docker helps in creating, managing those container images and also provides container runtime.
If you do not have docker installed, install the docker from this link
Check if docker is installed in your system
1docker --version
Now list the container images present in your system
1docker image ls
Note: If docker daemon is not running, you will get an error like
Cannot connect to the Docker daemon at unix:///abc/asdfas/docker.sock. Is the docker daemon running?. For this you need to start your docker daemon in your local server
Containerize our flask app
To containerize our flask app, go to the folder where your application is present and create a file named requirements.txt and Dockerfile
1.
2├── Dockerfile
3├── main.py
4└── requirements.txt
Inside requirements.txt write flask==2.1.3 , as this is the dependency to execute our project
Write the below contents in Dockerfile
1FROM python:3.8-slim-buster
2
3WORKDIR /biman-docker
4
5COPY requirements.txt requirements.txt
6RUN pip3 install -r requirements.txt
7
8COPY main.py .
9ENV FLASK_APP main.py
10
11CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=8090"]
Run the docker build -t flask-app . to create docker image.
You can list your image by running docker image ls
Run your docker container locally
1docker run -p 8999:8090 flask-app
2
3# Below command to run it in background
4docker run -d -p 8999:8090 flask-app
You can list your container by running
1❯ docker container ls
2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e679404da113 flask-app "python3 -m flask ru…" 12 seconds ago Up 10 seconds 0.0.0.0:8999->8090/tcp, :::8999->8090/tcp determined_johnson
Docker container can be stopped using
1docker stop <container_id>
Deploy our container on cloud run(GCP managed compute platform)
Before deploying our application to Cloud Run, lets understand the term registry. Registry is a place where we keep our container images. To deploy your services to Cloud run, you need to create an account in GCP. Once you have your account in GCP, activate the following api
- Cloud Run API
- Artifact Registry API
After the above two apis are enabled, GoTo https://console.cloud.google.com/artifacts and create a new repository. Run the below commands to configure your docker environment so that it can push the container images to Google Artifact Registry
1gcloud auth login
2gcloud auth configure-docker us-central1-docker.pkg.dev
Next rebuild the image and push it to registry
1docker build -t us-central1-docker.pkg.dev/<project_id>/<artifact_registry_id>/flask-app .
2docker push us-central1-docker.pkg.dev/<project_id>/<artifact_registry_id>/flask-app:latest
After this GoTo https://console.cloud.google.com/run to deploy your application
- Click on Create Service
- Select the container image url from registry
- Give some service name
- Click on allow
unauthenticated invocationsfrom Authentication section - Do not change the default parameters
- Click on Create
And in few minutes your service would be up and running
After your service is up and running,
- click on add custom domains
- Click on Add Mapping
- Select the service for which you want to add custom domain
- Verify your specific domain
- Add subdomains
api - Update the DNS records