Start your Kubernetes journey with Learning Paths today!

for

No results found.

Choose language

Lesson 2 6:54

Building Container Images

In this lesson, you’ll learn how to write a Dockerfile, and build and push your container image.

Download

Hello everyone. Welcome to the second lesson in the KubeAcademy course, building apps for Kubernetes. In this lesson, we will cover building container images. My name is Rich Lander. I'm a senior Kubernetes architect at VMware. So in this lesson, we'll go through the process of writing a basic Dockerfile, building container image based on that Dockerfile, and finally pushing that image to a public repository.

But before we get started, let's touch on just a couple of basic concepts on this subject. So a Dockerfile consists of a set of instructions that Docker will use to build a container image. And most instructions will result in a layer being added to the container image. So therefore, a container image consists of a series of file system layers stack atop one another. These layers contain the files that will be used to run the application.

When the container is run, it will contain the files built into the image and will run the process as you instruct it to run. The container will share the host Kernel with all other containers on that virtual or physical host, but will be isolated and will not have access to assets or processes in other containers. When the container spins up an extra writable layer is added on top of the image.

But it's important to keep in mind that when the container is deleted, so is that writable layer so don't store anything there that you want to keep. With that context, let's jump into building a container image. In order to build a container image, you need something to build into that image. And here is the source code for a very simple Go Program that we'll use to build and install into a container image.

So all this Go Program does is, when it starts up, it logs that it's starting up, it listens on port 8,000 and when it receives a request, it logs that event and then it returns this message back to the client. So in order to understand what happens when you perform a Docker build, to build a container image, it helps to just build and run that app locally and see what that looks like.

So if we run go build and we output to an executable binary called server, and then we run that server program, you'll see that it outputs our starting log message. And then if we go to a browser and we visit local host at port 8,000, we see that we get that message back that we expected. So now that we've seen what that looks like building and running it locally, let's open our Dockerfile and see what that looks like.

So in this case, first of all, we start with the FROM instruction and we're building it from the golang base image, which has all the files to install the Go Program Language. And the reason we do that is we're going to compile this from the source code, so we need the Go Programming Language installed. So then the next instruction is to actually copy the source code into the container, which in this simple case is just our main Go file.

Next, we're going to run that, Go build command. Just the same as we did over here, when we were building it locally, we're now doing that with the run instruction inside of our container, and that's going to output to an executable cold server. And then the final instruction is to actually just run that the same way we did on our local machine, to when the container starts. Every time the container starts, that will get run.

So now we have our Dockerfile, let's actually go ahead and build that image. In order to do so, we simply use the Docker build command. We're going to add the tag flag to give it a name, and I'm going to use my Docker Hub, public repo and we'll give it a name called building Apps. We also have to give it the build context which is just the path to the build context, which is the directory that I'm in, where my Dockerfile is.

So you issue that command, Docker goes through the process of building that image based on the Dockerfile that it found in this build context and you see that it's successfully built. You also see that by default, it gave it the latest tag and it's not good practice to use the latest tag, so we'll give it our own tag. We use the Docker tag command for this, we pass in the image ID, and then we'll give it a version of 0.1. So here we go.

It's now tag, now it's ready to push. In order to push to your repository, lets take the example of Docker Hub, you'll need to be logged in. And in order to do that, you can just issue the Docker login command. I'm already logged in. When you issue that command, it will prompt you for your username and password. And then once you're logged in, you can go ahead and push. And since I already am, I can push and away it goes.

Now I would like to point out that in this particular example, while it's a working example but will run just fine, it does contain the Go Programming Language and the source code for our program, which is actually not necessary for the final container image in order to run. All that's necessary is this binary file in order to run.

In order to accomplish that, you can use what's called a multi-stage build whereby you use a Go lane base image and your source code to build that executable and then as a final step, copy that executable into another container that contains just that binary file. But that's for another day. We're not going to cover that out of scope for this particular lesson.

But what I would like to touch on really quick is the documentation for a Dockerfile, because this is a working example, it only touches on or only uses a small subset of the instructions that you can use in a Dockerfile. So if you go to docs.docker.com, click on this reference link right here, and then on the top left, you'll see a file formats drop down with a Dockerfile reference under.

Visit that page and you'll find extensive documentation on all of the various instructions that you can use in a Dockerfile. If we scroll down on this panel on the right, we can find our copy instruction right here that we used in our Dockerfile. It goes through examples and documentation on how to use it. So that's it for this particular lesson.

If you'd like to learn more about containers, check out our containers, one on one course on Kube Academy, which goes into the subject in some more depth. But that's it for now, so thanks for watching and we'll see you in lesson three.

Instructor

Rich Lander

I am an independent consultant and Platform Field Engineer. I was an early adopter of Docker and Kubernetes and have spent the past few years helping enterprises adopt cloud native technology.

View Profile

Links

Have feedback about this course or lesson? We want to hear it!

Send your thoughts to KubeAcademy@VMware.com.

Lesson Completed

Advancing to the next lesson in seconds…

Next Lesson
What did you think of this lesson?

Share