Microservices in Go – Part IV Docker and Go Microservices

Hey everyone, Welcome to the fourth blog in our Microservices in Go series. Until now, we have seen how to use standard http package to create a REST API in Go. We have also seen how to add basic authentication to our REST API.

In this blog, we will dockerize the API we have been building so far. Keep in mind, this is an initial Docker image, we will update this along with new features added to our microservice in our future blogs. So, Let’s get started.

Multi Stage Docker Build for Go Service

We have seen how multi stage docker builds help us in reducing the image size in our previous blog. Do have a look if you are not sure about Multi Stage Docker builds.

Creating Dockerfile

We will take alpine with Go environment as our base image, build our service executable and then use a different stage to run this executable. Here’s how the Dockerfile looks like,

FROM golang:1.16.5 as builder

WORKDIR /go-microservice/

COPY . .

RUN CGO_ENABLED=0 go build -o microservice /go-microservice/main.go

FROM alpine:latest

WORKDIR /go-microservice

COPY --from=builder /go-microservice/ /go-microservice/

EXPOSE 9090

CMD ./microservice

Create a file called Dockerfile in the root of our codebase, and add the above contents. You can access the codebase from our GitHub.

Note: For detailed explanation of docker instructions, visit this blog.

Building Docker Image

Now that we have instructions ready, let’s build a docker image. Use the below command to create an image for our microservice.

docker build -t microservice .

You can see the microservice image being built with base images being pulled, and using the layers of different stages during our build. This is how, the output looks like.

Docker build process
Note: The initial image build will take long than the time shown in the above figure.

Let’s check if we have our image ready in our local repository. Run the below command to check this.

docker images

You should see the microservice image listed inside the output.

Images in local repo

Running the Docker Image

Now that, we have our docker image built. Let’s go ahead and run this.

docker run microservice

You should see the below log, if the container starts successfully.

Microservice running in a docker container.

But, Let’s test this. Go to your browser and send a request to http://localhost:9090/products/ . Wait, it fails, the resource is not accessible.

localhost not reachable

Yes, it happens because our microservice starts inside a container, which acts as a different machine altogether. So the port is exposed inside that container. Let’s map the 9090 port of the microservice container to the 9090 port of our host machine.

Run the below command to bind the port of container to our host machine.

docker run -p 9090:9090 microservice

Now, let’s go ahead and make a request to our microservice from the browser. This time, you should see the response from the browser.

We didn’t get any data, as we setup basic authentication in our previous blog. Go and check that out, if you’re interested in getting data from the API.

So, that’s it for this blog, you have built the tiniest possible docker image for our microservice with the help of multiple stages and you have seen how container works when it comes to exposing ports.

We will continue our exploration and learning in this series in our upcoming blogs. Until then, Stay safe. Cheers ✌✌

Blogs in this Series

2 responses to “Microservices in Go – Part IV Docker and Go Microservices”

  1. I tried to run the container without the -it flag. And somehow postman couldn’t perform operations. But with the -it flag on. It works just fine. Why so?

    Like

Leave a comment

Design a site like this with WordPress.com
Get started