Docker: Deploying Containers to The Cloud

With Docker, you can package your application and its dependencies into a lightweight, portable container that can run seamlessly on any platform. In this blog post, we'll explore how Docker works and how you can use it to deploy containers to the cloud, enabling you to manage and scale your applications easily.

by
Marny Lopez
|
February 29, 2024

What is Docker

Software deployment in portable containers is automated with the help of the configuration management tool Docker. Applications can operate more effectively in various contexts thanks to these containers.

Features of Docker

·  Simple and quick configuration

·  Isolation of applications

·  Security oversight

·  Extremely productive

·  Extremely scalable

·  Infrastructure not required

Docker container

A "Docker container" software package has every dependency needed to run an application.

Dockerfile

A simple text file called a Docker file contains instructions for creating Docker images.

Syntax

# Comments

The argument for a command argument1...

Example

# Print "Get Certified. Get Ahead"

Run echo "Get Certified. Get Ahead"

Let's now examine the process for using a docker file to create a Docker image.

Project Setup

We keep our code under the name app.py in a GCP-API directory (label it whatever you want). In addition to our script, we need the following:

1. A collection of Python modules for our Docker file to install and a Docker file serve as the guide for the Docker requirements.txt file.

2. We need Docker because we're using it.

Build and Publish Container Images

We can create Docker images using Cloud Build; all we need are our project files (including a Docker file).

The following command creates our Docker image and pushes it to the Container Registry using Cloud Shell.

gcloud builds submit --tag gcr.io/<PROJECT_ID>/demo-image.

Replace <PROJECT_ID> with your actual project ID value.

It should be noted that you can pass a timeout parameter, for example: 

--timeout=600s

Deploy with Google Cloud Platform

To deploy our container to the Cloud, we must follow three steps. We start by:

  1. Download the Google Cloud SDK, which you'll need to utilize Cloud Build to create our container.

  1. The container is to the Container Registry of GCP.

  1. Use Cloud Run to deploy it.

GCloud SDK

The SDK installation can be found here. Once it has been set up, we must verify our GCloud SDK by opening the CMD prompt (or your equivalent CLI) and entering the following command:

gcloud auth login

With the help of this command, our web browser is opened, enabling us to log into Google Cloud as usual. We set up Docker to use our GCP login information with the following:

gcloud auth configure-docker

Set the active project to your project ID by doing the following:

gcloud config set project

Cloud-Based Container Assembly and Registry

We can use Docker containers as a launchpad for our deployments thanks to the Google Container Registry (GCR) service, which enables us to store Docker containers.

To launch our container, we require an already-running project—an image created by the Author.

Creating a project is a prerequisite for using GCR or other services GCP offers. To accomplish this, open the Google Cloud Platform dashboard, go to the project selector page, and click the Create Project button.

Getting a new project up and running is a straightforward process—an image created by the Author.

For us to use GCR in a project for the first time, the GCR API must be enabled. The Author took the photo.

Since we have our project up and running, we should be able to access the Container Registry. In the top bar, we should see the name of the newly formed project we are working on here.

To use GCR, we need to click the Enable Container Registry API button in the middle of the terminal window.

Finally, we can upload our container to GCR by submitting it to Cloud Build, a service offered by GCP that generates Docker containers.

To accomplish this, we open our command line interface (CLI) in the directory containing our project and type: 

Cloud builds submit --tag GCR.io/[PROJECT-ID]/GCP-API

GCloud builds and submits the Docker image currently linked to our working directory to Cloud Build so that it can be incorporated into a container there.

The location of our Container Registry is communicated to the flag --tag in the following manner:

·  gcr.io is the GCR hostname.

·  [PROJECT-ID] is our project ID

·  GCP-API is our image name.

The GCP-API project that we are working on in Container Registry. Image created by the Author.

If we reload the GCR window, we should be able to view the newly uploaded Docker image that we have been working on. 

If it isn't there yet, it is most likely still in the process of being built, which may be viewed on the dashboard of our Cloud Build environment.

Cloud Run

Since our Docker container is complete, we can deploy it using Cloud Run.

A procedure is broken down into individual steps for deploying containers using Cloud Run.

In the interface for Cloud Run, the deployment process consists of the following steps: (1) clicking the Create Service button, (2) configuring our deployment, (3)– (4) choosing the container, and (5) creating our deployment!

A deployment that is currently being worked on in Cloud Run. Image created by the Author.

Within what ought to be no more than a few minutes, we will be able to view the deployment status on the Cloud Run console.

After it is finished, a checkmark in green text and a URL will show next to the name of our deployment and the location. The Author took the photo.

When the process is finished, we will see a checkmark in green next to our deployment name, and the URL of our deployment will be next to that.

Conclusion

We successfully deployed our Python application to the web using Google Cloud by packaging it into a Docker container and then putting it online.

Docker and Google Cloud Platform (GCP) are two excellent tools that make the process simple and straightforward, which, in most cases, leads to perfect deployments time and time again.

Now, more than ever before in the history of humanity. We can take the thoughts and ideas floating in our heads and give them a physical form in the world around us, which can lead to the production of some incredible works of art.