Docker Containers
Containers allow the developer to package an environment for applications
The advantages are to have applications run uniformly and consistently on various machine configurations.
Standardization
Create independent and isolated user-space instances that can be managed in tandem.
Lightweight virtual machines environments that are Inexpensive to run
Docker containerization provides speed, efficiency, and scalability for application deployment.
Containers share the host's Linux kernel.
Install - Configure - Deploy -Maintain
The Dockerfile defines the container image for the application to run on.
It tells the application how to build the image environment, libraries, operating system, code and works anywhere that Docker is installed on a Linux machine.
The example below defines a docker app for “Hello World”.
Go to Linux shell and create director called hellodocker and cd into it
john_iacovacci1@cloudshell:~ (cloud-project-examples)$ mkdir hellodocker
john_iacovacci1@cloudshell:~ (cloud-project-examples)$ cd hellodocker
john_iacovacci1@cloudshell:~/hellodocker (cloud-project-examples)$
joh
It starts with the app.py python file.
Open editor to create file
======================================================
# app.py
from flask import Flask
import os
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello, Google Cloud Users!."
if __name__ == "__main__":
# Cloud Run listens on the port defined by the PORT environment variable
port = int(os.environ.get("PORT", 8080))
app.run(debug=True, host='0.0.0.0', port=port)
========================================================
The application “Listens” to activity on a specific PORT and executes the function hello_world() when the application link is clicked.
The Dockerfile is needed which will provide the instructions for the application to run on.
========================================================# #Use the official lightweight Python image
FROM python:3.11-slim
# Set the working directory in the container
WORKDIR /app
# Copy the local code to the container
COPY . .
# Install Flask
RUN pip install Flask
# Run the web service on container startup
CMD ["python", "app.py"]
========================================================#
Next we need to permission our account
To enable services, account needs the Service Usage Admin role roles/serviceusage.serviceUsageAdmin
The broader Editor/Owner role.
Check IAM & Admin > IAM.
roles/serviceusage.serviceUsageAdmin
roles/resourcemanager.projectIamAdmin
john_iacovacci1@cloudshell:~/hellodocker (cloud-project-examples)$ gcloud run deploy hello-python --source . --region us-central1 --allow-unauthenticated
Building using Dockerfile and deploying container to Cloud Run service [hello-python] in project [cloud-project-examples] region [us-central1]
Building and deploying...
Validating Service...done
Uploading sources...done
Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds;region=us-central1/d9b9a54c-
39c9-471c-84c5-87c7fba3cd84?project=517129368909]....done
Setting IAM Policy...done
Creating Revision...done
Routing traffic...done
Done.
Service [hello-python] revision [hello-python-00003-hgh] has been deployed and is serving 100 percent of traffic.
Service URL: https://hello-python-517129368909.us-central1.run.app
Click on the service URL link
john_iacovacci1@cloudshell:~/hellodocker (cloud-project-examples)$
No comments:
Post a Comment