?? Publish a Docker image to GCP's Docker repository! ????
Rahul Udayshankar Sinha
FinTech | Cloud Architecture | Site Reliability Engineering | DevOps | Infrastructure as Code | Kubernetes | Data Analytics | Linux | Security & FinOps | Ultra Low Latency Trading Tech
Source Code
Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/html
index.html
<!DOCTYPE html>
<html>
<body style="background-color:rgb(153, 204, 255);">
<h1>Welcome to Sara GKE</h1>
<p>Powered by Google Kubernetes Engine</p>
<p>Application Version: V1.0</p>
<p>Color code is suggested by Mr. Anirudh Sinha</p>
</body>
</html>
Create Docker Image
docker build -t sara-simplywebpage .
C:\Users\Rahul\docker_folder>docker build -t sara-simplywebpage .
[+] Building 8.7s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 86B 0.0s
...
=> => extracting sha256:5fb9a81470f3644c474192baf0827a34749286cb6d933091d4d4463ea4f9c495 0.0s
=> => extracting sha256:9b1e1e7164db75ad0f64e8deeb33e771d455fa590126b2e16d25e5a75fc6f517 0.0s
=> [2/2] COPY index.html /usr/share/nginx/html 0.3s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:647cfe00450fdabca548a6a5baefb189dc2d2d3f5842cc3745731ee37a2f6a43 0.1s
=> => naming to docker.io/library/sara-simplywebpage 0.0s
What's Next?
1. Sign in to your Docker account → docker login
2. View a summary of image vulnerabilities and recommendations → docker scout quickview
C:\Users\Rahul\docker_folder>
Verify the local image
C:\Users\Rahul\docker_folder>docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
sara-simplywebpage latest 647cfe00450f About a minute ago 187MB
Launch docker container locally
C:\Users\Rahul\docker_folder>docker run --name sara-web1 -p 8084:80 -d sara-simplywebpage
45eea22d2db340f25aada51aaa7bcf9b1433b900eae8ca995fb83d108ac1fdab
C:\Users\Rahul\docker_folder>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45eea22d2db3 sara-simplywebpage "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 0.0.0.0:8084->80/tcp sara-web1
Enable docker repo in GCP and Configure gcloud sdk from command line
C:\Users\Rahul>gcloud init
.....
Create artifact repository for docker
gcloud artifacts repositories create sara-gke-docker-artifact-repo \
--repository-format=docker \
--location=us-central1 \
--description="sara-gke-docker-artifact-repo"
List gcloud artifact repository
$ gcloud artifacts repositories list
Listing items under project sara-project-401601, across all locations.
ARTIFACT_REGISTRY
REPOSITORY: sara-gke-docker-artifact-repo
FORMAT: DOCKER
MODE: STANDARD_REPOSITORY
DESCRIPTION: sara-gke-docker-artifact-repo
LOCATION: us-central1
LABELS:
ENCRYPTION: Google-managed key
CREATE_TIME: 2023-10-10T23:12:27
UPDATE_TIME: 2023-10-10T23:34:37
SIZE (MB): 67.214
Setup gcloud auth
C:\Users\Rahul>gcloud auth configure-docker us-central1-docker.pkg.dev
WARNING: Your config file at [C:\Users\Rahul\.docker\config.json] contains these credential helper entries:
{
"credHelpers": {
"us-central1-docker.pkg.dev": "gcloud"
}
}
Adding credentials for: us-central1-docker.pkg.dev
gcloud credential helpers already registered correctly.
C:\Users\Rahul>
Tag the docker image and push it to GCP Docker Repository
C:\Users\Rahul>docker tag sara-simplywebpage:latest us-central1-docker.pkg.dev/sara-project-401601/sara-gke-docker-artifact-repo/sara-simplywebpage:v1.0
C:\Users\Rahul>docker push us-central1-docker.pkg.dev/sara-project-401601/sara-gke-docker-artifact-repo/sara-simplywebpage:v1.0
The push refers to repository [us-central1-docker.pkg.dev/sara-project-401601/sara-gke-docker-artifact-repo/sara-simplywebpage]
104e45c9a17a: Pushed
d26d4f0eb474: Pushed
a7e2a768c198: Pushed
9c6261b5d198: Pushed
ea43d4f82a03: Pushed
1dc45c680d0f: Pushed
eb7e3384f0ab: Pushed
d310e774110a: Pushed
v1.0: digest: sha256:32bed7c85ddc386cd49c8f920f46c3c73e6331e8f80add3fa34686c9deb53b20 size: 1985
C:\Users\Rahul>
Deploy the docker image to GKE using below deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sara-deployment
spec:
replicas: 3
selector:
matchLabels:
app: sara-app
template:
metadata:
name: sara-pod
labels:
app: sara-app
spec:
containers:
- name: sara-container
image: us-central1-docker.pkg.dev/sara-project-401601/sara-gke-docker-artifact-repo/sara-simplywebpage:v1.0
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: sara-lb-service
spec:
type: LoadBalancer # ClusterIp, # NodePort
selector:
app: sara-app
ports:
- name: http
port: 80 # Service Port
targetPort: 80 # Container Port