GCP DevSecOps Series (Part 2)

GCP for DevOps: Basic of Docker

Harshit Gupta
5 min readJul 3, 2024

Welcome back to the second part of our GCP for DevOps series! In the first part, we covered the fundamentals of getting started with GCP. Now, we’re going to dive into Docker, an essential tool for DevOps. Docker allows you to package applications and their dependencies into a container, ensuring consistency across multiple environments.

In this part, you will learn how to create a simple web application, containerize it using Docker, and deploy it on GCP. Let’s get started!

Why Docker?

Before we dive into the technical steps, let’s understand why Docker is a crucial tool in the DevOps toolkit:

  • Consistency: Docker ensures that your application runs the same way regardless of where it is deployed.
  • Isolation: Containers isolate applications from each other and the host system, improving security and stability.
  • Portability: Containers can run anywhere, from your local machine to any cloud provider, without changes.
  • Efficiency: Docker containers are lightweight and share the host OS kernel, making them more efficient than traditional virtual machines.

Create Simple WebApp on Cloud Shell

Let’s start by creating a simple web application. We’ll use Python for this example, but you can use any language you prefer.

1. Open Cloud Shell:
Access Cloud Shell from the GCP Console by clicking on the terminal icon in the top right corner.

2. Create a New Directory:
In the Cloud Shell terminal, create a new directory for your project and navigate into it:

3. Create a Simple Python Web Application:
Create a new Python file, main.py:

4. Install Flask.

5. Run main.py:

Create Dockerfile

Next, we’ll create a Dockerfile to containerize our web application.

  1. Create a Dockerfile:
  • In the same directory, create a file named Dockerfile:

Build Docker Image from Dockerfile

Now, let’s build the Docker image from the Dockerfile.

1. Build the Docker Image:

  • In the Cloud Shell terminal, run the following command to build the Docker image:
  • docker build -t docker-basic .

2. Run the Docker Container Locally:

  • Once the image is built, you can run it locally to test it:
  • docker run -p 8080:8080 docker-basic
  • Open a new tab in your browser and navigate to http://localhost:8080 to see the web application running.

Push Docker Image to Container Registry

After testing the image locally, we need to push it to Google Container Registry (GCR) so it can be deployed on GCP.

1. Configure Docker to Use GCR:

  • Authenticate Docker to the GCP Container Registry:
  • gcloud auth configure-docker

2. Tag the Docker Image:

  • Tag your Docker image with the GCR host name:
  • docker tag docker-basic gcr.io/YOUR_PROJECT_ID/docker-basic
ProjectID can be found on dashboard

3. Push the Docker Image to GCR:

  • Push the tagged image to your GCP project’s container registry:
  • docker push gcr.io/YOUR_PROJECT_ID/docker-basic

4. Verify the Image in GCR:

  • After pushing the image, it’s important to verify that it has been successfully uploaded to Google Container Registry (GCR).
  • Normally, you would go to the GCP Console, navigate to “Container Registry,” and check that your image is listed there.

Unfortunately, due to some issues with my Google account and billing, I can’t share the image of this process. However, I can assure you that if you follow the steps correctly, your Docker image will be successfully uploaded to GCR.

Conclusion

In this part, we covered the basics of Docker, created a simple web application, containerized it, and pushed the Docker image to Google Container Registry. Docker is a powerful tool that simplifies the deployment and management of applications in any environment.

⚠️ I must mention that these account and billing issues might cause a temporary pause in this series. If the issue isn’t resolved soon, I will continue the series with theoretical explanations and detailed guides based on best practices.

In the next part of this series, we will maybe explore deploying this Docker container on GCP services and setting up a CI/CD pipeline to automate the process. Stay tuned!

Connect to me, if you have any query, want to share or suggest me anything, here’s my Portfolio or dm me on LinkedIn. Stay tuned and follow me for more chapters of my odyssey, my projects, cybersec and devops articles, and write-ups and also thank you for being part of my story! 🚀

If you enjoy my posts and find them helpful, consider buying me a coffee! Your support helps me earn relevant certifications and continue sharing valuable insights. Buy Me a Coffee

https://buymeacoffee.com/harshit21

Let’s continue to strengthen our skills and build secure, scalable applications together!

--

--