Deploying a Custom Website on Google Cloud Run with Docker

Gauri AgrawalGauri Agrawal
4 min read

Step 1: Clone the Git Repository

  • Open Google Cloud Shell.

  • Create a directory

    mkdir my-app

    cd my-app

  • Clone your Git repository inside my-app:

  1.   git clone [REPO-URL]
    

    https://github.com/bradtraversy/50projects50days.git

  2. Navigate to the project directory:

     cd [REPO-DIRECTORY]
    

Step 2: Create the Nginx Configuration File (nginx.conf)

In your project directory (my-app/50projects50days/3d-boxes-background), create a custom Nginx configuration file if needed:

  1. Create the nginx.conf file inside which website you want to deploy:

     touch nginx.conf
    
  2. Add the following content to your nginx.conf file:

     server {
         listen 8080;
         server_name localhost;
    
         location / {
             root /usr/share/nginx/html;
             index index.html;
         }
     }
    

    This configuration tells Nginx to serve the static files in /usr/share/nginx/html (where your website files will be located) and to use index.html as the main file.

Step 3: Create a Dockerfile

  1. Navigate to my-app

  2. Create a Dockerfile inside my-app:

     touch Dockerfile
    
  3. Add the following content to the Dockerfile:

     # Base image
     FROM nginx:alpine
    
     # Copy the website files to the Nginx default directory
     COPY 50projects50days/3d-boxes-background/ /usr/share/nginx/html
    
     # Copy the custom Nginx config file
     COPY 50projects50days/3d-boxes-background/nginx.conf /etc/nginx/conf.d/default.conf
    
     # Expose port 8080 for Cloud Run
     EXPOSE 8080
    
     # Start Nginx server
     CMD ["nginx", "-g", "daemon off;"]
    

  4. Confirm Directory Structure: Before proceeding, check that the Dockerfile and 50projects50days directory are in the current folder. Run:

     ls
    

    You should see both the Dockerfile and the 50projects50days folder in the output.

Step 4: Build the Docker Image

Now that the Dockerfile and nginx.conf are set up, build the Docker image:

docker build -t my-custom-website .

Step 5: Tag and Push Docker Image to Artifact Registry

5.1 Enable Artifact Registry

If you haven’t enabled Artifact Registry:

gcloud services enable artifactregistry.googleapis.com

5.2 Create a Repository in Artifact Registry

If you haven’t already created a Docker repository in Artifact Registry:

gcloud artifacts repositories create [REPOSITORY] \
    --repository-format=docker \
    --location=[REGION] \
    --description="Docker repository"

You can create a repo manually also:

  • Search Artifact Registry

    • Click on ‘+’ icon

    • Select the format, name the repo, select the region and click on create

5.3 Tag Your Docker Image

Tag the Docker image for pushing to Artifact Registry:

docker tag my-custom-website [REGION]-docker.pkg.dev/[PROJECT-ID]/[REPOSITORY]/my-custom-website:v1.0

docker tag my-custom-website us-central1-docker.pkg.dev/moonlit-poetry-436210-r1/my-repo/my-custom-website:v1.0

5.4 Push the Image to Artifact Registry

Push your Docker image:

docker push [REGION]-docker.pkg.dev/[PROJECT-ID]/[REPOSITORY]/my-custom-website:v1.0

docker push us-central1-docker.pkg.dev/moonlit-poetry-436210-r1/my-repo/my-custom-website:v1.0

Step 6: Deploy to Cloud Run

Once the image is pushed to Artifact Registry, deploy it to Cloud Run:

gcloud run deploy my-custom-website \
  --image=[REGION]-docker.pkg.dev/[PROJECT-ID]/[REPOSITORY]/my-custom-website:v1.0 \
  --platform=managed \
  --region=[REGION] \
  --allow-unauthenticated
  • [SERVICE-NAME]: Name of your Cloud Run service (e.g., my-custom-website).

  • [PROJECT-ID]: Your GCP project ID.

  • [REGION]: The region where you want to deploy the service.

  • [REPOSITORY]: Name of your Docker repository.

gcloud run deploy my-custom-website1
--image=
us-central1-docker.pkg.dev/moonlit-poetry-436210-r1/my-repo/my-custom-website:v1.0
--platform=managed
--region=us-central1
--allow-unauthenticated

Step 7: Access the Cloud Run Service

Once deployed, Cloud Run will provide a URL for your service. You can access your website using this URL.

Click on Service URL

Directory Structure

Your project directory should look like this:

your-root-directory/
├── my-app/
│   └── 50projects50days/
│       ├── 3d-boxes-background/
│       │   ├── index.html           # Your main website file
│       │   └── nginx.conf           # Your custom Nginx configuration file (optional)
│       └── Dockerfile                # Your Dockerfile

Step 8: Check Logs if Something Goes Wrong

If the deployment fails or the site doesn’t load correctly, check the Cloud Run logs to diagnose the issue:

gcloud logs read --limit 50 --project=[PROJECT-ID]

Summary

  1. Clone the Git repo to Cloud Shell.

  2. Create an Nginx configuration file (nginx.conf) if needed.

  3. Create a Dockerfile to containerize your website.

  4. Build and tag your Docker image.

  5. Push the image to Google Artifact Registry.

  6. Deploy the image to Cloud Run.

  7. Access your website using the provided URL.

This process will help you deploy a custom website to Cloud Run.

Deploying your custom website on Google Cloud Run with Docker makes hosting easy and scalable. With Nginx, you can efficiently serve your site in the cloud. Give it a try, and stay tuned for more tips and tutorials!

0
Subscribe to my newsletter

Read articles from Gauri Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gauri Agrawal
Gauri Agrawal