Whether for POCs, college assignments, or recruitment process challenges, giving the user the possibility to use the application is essential. With this post, I bring a simple and robust way to deploy your projects.
This walkthrough is programming language/framework agnostic, as soon as the backend runs on a TCP port and the frontend generates static files.
We will use the "Deploy from source code" option, where a Dockerfile is not really necessary, but in this guide, we will use one for more control over the container.
Docker is a containerization platform that helps ship applications in a consistent way regardless of the environment.
A Dockerfile is a recipe that Docker will follow to build an application image. Next, I’ll build one step by step:
You will need a Docker image for the language/framework you are using, find it on Dockerhub and insert it in FROM step. Always try to use official images and specific versions.
Define the location where the application will run and copy the dependency file.
For Python, it is a requirements.txt
Install dependencies as usual.
Copy the source code.
Note that two COPY commands were run: One for the dependencies and another for the source code.
The advantage of installing dependencies in a separate step is that since each line of the Dockerfile is a layer, Docker will cache the layers that haven’t changed.
An example is if only the source code changes, lines 4 and 6 will benefit from the cache and dependencies would not be installed again.
Finally, expose the port your application will use and set the CMD command.
Above is the result of the recipe needed to run the application’s container.
Create a new project.
Install and initialize the gcloud CLI.
Or follow this guide.
To build and deploy the container on Cloud Run, use it in the root of your backend project, where the Dockerfile is located:
Where cloudrunapp is the name you want to give the service.
Don’t forget to enable the Cloud Run API and a billing account for this project.
If prompted, select a region and allow “unauthenticated invocations”
If you need to add environment variables, click “Edit and Deploy New Revision”. In the “Container” tab, scroll down to “Environment variables” and add the necessary ones.
This should be ok for tiny projects, but if you need more security, please use Secret Manager.
Your application should be available at the indicated link.
For the frontend, we’ll use Firebase Hosting to serve static files.
Create a Firebase project in the console.
In this example, we’ll initialize a Vite project, build it, and serve it from Firebase Hosting.
The dist folder will be created.
Install firebase-tools, login and start a Firebase project.
Follow this way:
And finally deploy:
Your application will be served on the indicated link and even the custom domain can be configured easily.
Github repo with code: