This lesson is locked. Login or Subscribe for more access!

What is Docker & Containerising Go Services

Duration: 11 mins

Docker and Go are a match made in heaven. Learn why here.

Instructor

Chris Shepherd

Share with a friend!

Transcript

Hey folks, so in this module we're going to look at how we can build our gRPC applications and package them up using Docker, then deploy them to different environments like Kubernetes. There are many different deployment strategies, but containerization is one of the most common methods.

Docker is an open source platform for building applications into lightweight deployable containers. These containers are completely portable and self-contained - they have everything required to run the application. For this example, we'll work with a simple Hello World gRPC service we've built previously.

First, we create a Dockerfile. We'll start with a multi-stage build using golang:1.22.1-alpine as our base image for building. Our first stage, labeled 'build', will compile the Go application. After copying our project files and setting the working directory, we'll run go build with flags to remove debugging information for a smaller binary.

Instead of using this full golang image to run our application, we'll create a second stage using a minimal alpine image. We'll copy just our compiled binary from the build stage, making our final image much smaller. We'll expose port 50051 for our gRPC service and set our binary as the entrypoint.

To build our image, we use docker build -t hello-server . Run it with docker run -p 50051:50051 hello-server. The port forwarding is crucial - without it, the gRPC service wouldn't be accessible from our local machine. When running the client against our containerized server, make sure you've forwarded the ports correctly.

For deployment, we'll push our image to a container registry. Using GitHub packages, we tag our image with ghcr.io/username/grpc-course/hello-server and use docker push. You can specify versions using tags like :v1.0 instead of just using latest. Remember to authenticate with your container registry first.

Once the image is in the registry, we can pull and run it anywhere with docker pull and docker run. In the next video, we'll look at deploying this containerized application to Kubernetes for a more robust production environment.