Cloud native applications are packaged, distributed, and run in containers. Docker is a key container technology. This video shows how a Docker image for a Go microservice is built and distributed.
- [Instructor] This video is about building Naive Docker images for our little Go Microservice. So, on this video we're going to take a look at how to write a Dockerfile for our Go Microservice. I'm going to show you the basic commands you need to know to build the Docker image from this Dockerfile and then finally we're going to tag and push the image to Docker Hub. So, let's get started then. So, let's open our ID.
First of all we need to create a file called Dockerfile. It's a basic text file in the end. Now, the first line is always the base image we're going to base this image on, so we base this image on golang:1.7.4. The maintainer is basically only some metadata which tells it that I built this image. Now with this line I'm going to tell Docker that when building it should copy the content of the current directory into a directory to the Docker image and into the directory go/src/github.com/PacktPublishing/Cloud-native-Go/, so this is where all the files form my local directory will be copied into the Docker image.
And because we're going to need this value a couple of times I will extract an environment variable for it. So, we'll do a little refactoring, so that's done. Next up we want to change into the directory in the Docker image and we want to build our Go Microservice and we're going to use the run command for it, so first up we're going to change into our source directory, then we need to build the sources.
So, that's that. Now remember, our Go Microservice had an environment variable called port to specify the port. So, it's good practice to specify the default values in your Docker file, so we set 8080 as the default value for port. We add some additional metadata that this image will expose port 8080 and then finally, we will specify the entry point and the entry point is the main applications that should be run when this Docker image is run as a container.
So, in our case it's Cloud-Native-Go because this will be the name of the executable being built, so that should be it. Let's see if that works. So, I open up my console, we see I'm in my working directory, so I'm going to use the Docker build command with minus T for tag, I'm going to give my Docker image a name and a version which is Cloud-Native-Go:1.0.0 and I'm going to run it in the local directory, the current directory.
So, what you see here in the console now are the basic build steps that I now run, so step one from golang:1.7.4. You see here is the maintainer. Step three, setting the environment sources. Step four is copying over the sources. Step five is now changing into the source directory and building and installing our Go Microservice within this image.
So, let this run for a bit and we're done. So, if you say Docker images, you now see here that here locally we have our Docker image. Now, as you can see, the size of the image is quite big at the moment, so to slightly improve, we will use a smaller base image here, so it's always good that golang offers an alpine base image, so we use from golang:1.7.4-alpine instead.
So, all we do now is reissue this command. Well, maybe specify a different tag and we're going to build that again, so same here, we see that step one changed now. We have from golang:1.7.4-alpine, still the maintainer, the sources, we copy over and in step five it's now building the microservice again. So, we need to wait a little here.
And we're done. Now, you see the difference here now. Let's have a look at the first two lines. So, on the first line you see the improved version 101 which is now a lot smaller, so only 250 megabytes whereas the first version of our image is 683 megabytes, so quite a difference now.
So, next up we want to push our improved Docker image to Docker Hub, so how do we do that? Well, first of all you need to use the tag command and we're going to tag our image Cloud-Native-Go:1.0.1 and prefix it with the username I have on Docker Hub. So, all this does is basically produce this line here, so I have exactly the same image but on a different name.
So, I log into Docker Hub, well, that's my name, I log in and I can now use the Docker push command and push this image up to Docker Hub. So, you see that it's now pushing the different layers of our Docker image up to Docker Hub and this will take some time.
You see that it's recognizing that those layers are from an existing image, so this is why it's not pushing them and we're done. Now, if we go to our browser, and we go to hub.docker.com, we see here I have this image I just pushed a few seconds ago. So, in the next video I'm going to show you how you can run our containerized Go Microservice locally and how to play around with it a little.
Okay, hope to see you then. Bye bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Implementing Go HTTP Server
- JSON marshalling and unmarshalling of Go structs
- Implementing a simple REST API
- Using Docker workflows and commands
- Building a naïve Docker image
- Running a containerized Go microservice
- Kubernetes architecture and concepts
- Deploying a Go microservice to Kubernetes
- Implementing service descriptors
- Performing rolling updates