liveBook · Manning

Container image

As explained in gRPC Microservices in Go

A Docker image is a packaged, immutable filesystem and metadata template used to create Docker containers.

A container image packages a service for running as a container. For a Go gRPC service, the image can contain the compiled executable, which becomes the container’s entry point. The same artifact can move through testing and deployment to Kubernetes. section 8.1 section 8.0

Build each service image from its own Dockerfile. A multistage build keeps compilation separate from runtime: a Go image runs `go build`, then a `scratch` stage receives the resulting executable. The runtime stage does not need the Go distribution. section 8.1.1

FROM golang:1.18 AS builder                                           #1
WORKDIR /usr/src/app
COPY . .                                                              #2
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o order 
 ./cmd/main.go                                                      #3
 
FROM scratch
COPY --from=builder /usr/src/app/order ./order                        #4
CMD ["./order"]                                                       #5

The Order service Dockerfile compiles with `golang:1.18`, copies the executable into a `scratch` stage, and sets `./order` as the command. section 8.1.1

Build and distribute a service image

Place the Dockerfile in the Order service directory and run `docker build -t <image_name> .` there. A registry can store the tagged image for Kubernetes to pull; copying images directly to cluster nodes is an alternative for faster development feedback. section 8.1.1

In a CI/CD pipeline, generate and tag images after code checks, then deploy them to UAT and production. Rolling, canary, and blue-green deployments offer different release paths. Monitor error rates and user feedback so a problematic image can be rolled back or replaced with a hotfix. section 1.4.3

Database image for integration tests

A Docker Compose stack can run `mysql:8.0.30` for the Payment and Order services. Mount an initialization SQL file to create their databases, and use a MySQL ping health check before tests connect. section 7.4.4

How gRPC Microservices in Go covers it

How deep it goes
Works through a multistage Dockerfile, build command, and distribution choices; skips Docker fundamentals.
Examples
Uses an Order service Dockerfile and a MySQL 8.0.30 Compose service with database initialization and a health check.
What it assumes
Assumes Docker basics and an existing Go service; shows how to package its compiled executable.
What you can do after
Build it from scratch with a service Dockerfile and `docker build`, then prepare the image for Kubernetes deployment.
How it connects
Links container images to multistage builds, Docker Compose, integration testing, CI/CD, registries, and Kubernetes.

Also explained in

Compare how 25 books explain container image →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.