From git push to a Running Container on Cloud Run

A line-by-line walkthrough of a real Dockerfile and Cloud Build file: what a container image is, how the four build steps work, why images are tagged with the commit, and what Cloud Run does with the result.

Free preview · 2 of 3 free left
Topic: Cloud Guide intermediate 8 min read

Why this matters

What Is a CI/CD Pipeline, Really described the idea: a push triggers a build and a deploy. This chapter opens the two files that make it real for a Node.js website, a Dockerfile and a cloudbuild.yaml, so that "container" and "deploy to Cloud Run" stop being magic words. Both files are short enough to read in full.

What you'll learn

What a container image is, what each line of a real Dockerfile does and why the order matters, the four steps of a real Cloud Build file, why every image is tagged with the commit, and what Cloud Run does with the result.

What a container image is

An application needs more than its own code: a particular version of Node.js, its libraries, sometimes system fonts or tools. On a server, any of these can be missing or different from your laptop. A container image fixes that by packaging the application together with everything it needs. Whoever runs the image gets the same environment you tested.

The Dockerfile, line by line

This is the shape of the Dockerfile for this site (comments shortened):

FROM node:22-slim
RUN apt-get update && apt-get install -y --no-install-recommends fonts-dejavu-core fontconfig && rm -rf /var/lib/apt/lists/*
WORKDIR /app
RUN chown node:node /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --chown=node:node . .
USER node
EXPOSE 8080
CMD ["node", "server.js"]

The pipeline: four steps in cloudbuild.yaml

The Cloud Build file runs these in order. A failure at any step stops everything after it.

  1. Test. In a clean Node.js 22 container, run npm ci --omit=dev --ignore-scripts && npm test. If any test fails, the build stops here and nothing is deployed. This is the gate that protects the live site.
  2. Build. Run docker build with the Dockerfile above, producing the image.
  3. Push. Upload the image to Artifact Registry, Google's storage for container images, in the same region as the site.
  4. Deploy. Run gcloud run deploy for the service, pointing at the image that was just pushed. The --quiet flag stops the command from asking questions, which matters when no human is watching.

Why the image is tagged with the commit

The image name ends with :$SHORT_SHA, the first seven characters of the commit hash. Every build therefore gets a unique, traceable name: given a running version, you can find the exact commit it came from, and given a commit, you can find its image. A tag like latest would hide that, because it points at something different each time.

What Cloud Run does next

Each successful deploy creates a new revision of the service and, by default, sends traffic to it. Older revisions are kept, which is what makes rolling back quick: you point traffic at an earlier revision instead of rebuilding anything. Cloud Run starts containers when requests arrive, gives each one the PORT variable, and starts the command in CMD.

Try it yourself

Open your own project's Dockerfile, or any small one, and answer: (1) Which line would make the slow install re-run on every build if you moved it? (2) Which folder does COPY . . copy, and does it contain anything secret? (3) If a deploy broke the site, which single identifier would tell you the commit to look at? If you have Docker installed, docker build -t demo . followed by docker run -p 8080:8080 demo runs the same image on your own computer.

What's next

To make the scripts around deployments safe to re-run, read Writing Idempotent Deployment Scripts. To see how a change reaches the branch that triggers this pipeline, read Git for a Real Website.

Continue learning

Guide

What Is a CI/CD Pipeline, Really

CI/CD explained without the buzzwords — what continuous integration and continuous deployment actually automate, and what a real pipeline looks like end to end.

Guide

Writing Idempotent Deployment Scripts

What idempotent actually means for a deployment or automation script, why it matters when a script might be run twice by accident, and concrete patterns for writing scripts that are safe to re-run.

Guide

Secrets Management: Don't Commit That Password

Practical credential hygiene for real projects — why secrets don't belong in source control, how .gitignore and environment variables actually protect you (and where they don't), and what to do if a secret is committed by mistake.

← Back to Guides

Next → Git for a Real Website: The iamravi.com Workflow A structured, practical course in Git and GitHub, taught through the actual workflow used to build and maintain a production Node.js site — not a shallow command dump.