Microservices need to be packaged, distributed, and run in containers. Docker is the key technology.
- [Instructor] Hi, and welcome back to Advanced Cloud Native Go. My name is Leander Riemer and I'm happy to be your host. Containerization and composition with Docker. So, in this video we're going to take a look at how to write a Docker file for the previously implemented advanced Go microservice. We're going to build and run the Docker image using Docker Compose, and we're going to tag and push the Docker image to a remote registry. So let's get started then and get some coding done.
So, I'm going to use my IDE again. So, it all starts with a Dockerfile, and I've prepared an empty Dockerfile here. So, the first line is always the base image we're going to base our Docker image on. So we say from golang 1.8.1 alpine distribution. So next up, I want to install the gin framework within this Docker image.
So I run the following command: go get github.com gin gonic gin. So this will install the gin framework within this image. And because this command requires Git installed, I do need an additional run command. So what I need to do in here now, is basically, apk add bash git. So next up, I'm going to copy my local sources into this Docker image.
So I define an environment variable sources, so this is where it's going to go, go, source, get.com, Pack publishing, advanced cloud native go, frameworks, gin web. I'm going to copy everything from the local working directory into this target directory. Next up, I'm going to change into our sources directory, and I will call go build to compile our microservice. Next I define the work directory, and I will define the entry-point command.
Basically, after go build has been called, it is Gin-web executable will be there. And for some housekeeping, I will define port 8080 to be exposed. So that's how a Dockerfile done. Now, I could already build the Docker image for our microservice using this Dockerfile only, but I want to go one step further. And I want to use Docker compose to do the building and running of our microservice later.
So here, I have an empty Docker compose yml file. So let's add some content here. So I start with version 2. I define the services section and I give our service a name. I use the current directory as build context directory, find the image name, we can find some environment variables, remember, our service does support the port environment variable.
Let's go for 8080, well, it's the default, and I define some port mappings. So that's it! Docker compose file, done. So let's see if that already works. Let's build Docker image, using Docker compose. So I open my console now. So let's see if Docker is running, Docker version, so you see here we've got a pretty up-to-date Docker version.
Got a few Docker images here. So what I do now is Docker compose, and we call a build command. So you see the different steps involved in building the image. So step one is the from command, it installs bash and git, it runs the go get for the gin framework, it copies all of the sources, it builds everything, defines the work directory, defines the command, and everything's done.
So if I say Docker images, I now have here gin web, created about 20 seconds ago. So what do I have to do to run this? Well, that's pretty straightforward, I can say, Docker compose up. So you see here, it's created the container from our image now, and you see here the output of our microservice we've implemented in our previous video.
So I have those Get endpoints again, Book rest API, and for the static HDML files. So, you see here, I have the index page, I can call the ping, I can call the API books, the messaging point, my hello, and if you go back here to the console window, you see all the interactions with the rest of the endpoints of our service.
So, that was running in the foreground, of course, what you can do, is you can call Docker compose up minus d and it's starting everything in the background now. So here, we have Docker PS running in the background. Okay, so you can do Docker compose stop, to stop everything.
So everything gone again. So last thing missing is now tagging and pushing our image to a remote registry. Obviously, this image is only local now, so what you could do is, you can say docker tag gin web 1.0.1. And I'm going to use my Github username here, going to tag this.
You see here, it's got this newly created tag now. And I can now use Docker push. You see here I'm pushing this image to Docker up now, takes some while, and the base image library golang is already mounted, it's almost done.
So that's it, we've now pushed our Docker image to Docker hub, and it's there for us to use at some later stages. So, in the next video I'm going to show you how you can orchestrate the microservice using Kubernetes. Hope to see you there, bye-bye!
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Cloud native application platforms
- Go frameworks and libraries for microservices
- Using Docker for containerization
- Using Kubernetes for orchestration
- Using Consul for microservice discovery and configuration
- Registration and lookup
- Implementing service discovery using Kubernetes
- Microservice communication patterns: Sync and async
- Using circuit breakers for resilient communication
- Implementing message queuing with Rabbitmq
- Using Apache Kafka for publish/subscribe