Cloud native applications are packaged, distributed, and run in containers. Docker is a key container technology. This video shows how an improved Docker image can be built and how Docker Compose can be used to build and run the image in combination with other services.
- [Instructor] In this final video of this section, I'm going to show you how you can write improved Docker images and how you can use Docker Compose to build and run those images in addition to other services. Let's get started. In this video we're going to take a look at writing improved Dockerfiles for even smaller images and then we're going to use Docker Compose to build and run this image in addition to other services.
Let's switch to our integrated development environment. The Dockerfile, all this is, the Dockerfile we've written so far. We based it on "golang: 1.7.4-alpine", we copied the sources over into the image and we build our microservice within the image. The result was a quite huge Docker image of 250MB in size. How can we improve this? Well, the basic idea being that we use a different base image, a smaller base image, we build the final executable on our development machine and we simply copy this executable into the image during build time.
Now, we use a different base image here. We use alpine, which is nice and small. We copy the Cloud-Native-Go executable from our development machine... into a directory called "app" in our Docker image. Now remember, we need to make this executable, so we're going to use a "run" command and change mod our executable.
So that's all. The default port... Now remember, we said that we need to build the binary locally on our machine first, OK? Luckily, you can do this with Go even though we are on a Windows machine. We specify the "GOOS", that, OK.
Now, Go now compiles a Linux executable, because we base our Docker base image on alpine, we obviously cannot copy the Windows executable into this Docker image, right? Now, as you can see, is, we build the Docker image again.
So we see here... we have a 15 MB in size Docket. Once again, we can tag this image and push this to Docker.
So let's... And create a file called Docker Compose. And this is what it looks. I'm going to use version two, let's call Microservice, maybe I specify environment variables like "9090" in the port. You define the port folders.
What I can do now already is use the Docker Compose build command. This is building the microservice, as does before. Our microservice usually doesn't run alone. It needs some more services, maybe. Maybe we want to add in as a reverse proxy for our microservice. This is how we do it.
I add it! Again, after some port mapping, to our microservice. Let's save that one. Now, if you're working with Docker Compose, you use Docker Compose up to run everything, and minus D to run it as it. So let's... Create service, and it has been creating the nginx container.
So if you do "docker ps" now you se that one for the nginx container and one for our Go Microservice. So we see here on port "8080"... So, this was Docker Compose and that very easy Docker Composed file.
Maybe I remove any intermediate containers with "docker-compose rm". Yes and sure... And we're done. What did we... First of all, we introduce (mumbles) containers. Images being the resting representation and containers being the running representation.
I introduced you to the basic Docker workflow and so... We wrote some Dockerfiles to containerize our Go microservice, We ran our microservice locally using Docker. Now finally, we used Docker Compose to build and run the images. So I hope you enjoyed this course, and in the next video it's all about Kubenetes and how we can use it to orchestrate a Go Microservice.
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