From the course: Automating Kubernetes with GitOps

Using Dockerfile

- Right, a very important component in your GitHub strategy is a Dockerfile because a Dockerfile is a way to automate container builds. And a Dockerfile might be the desired goal of your pipeline. Just about terminology. In Red Hat environments, they talk about Containerfile. Containerfile is used instead of Docker because Docker is brand and containers are not about brand. But if you use Dockerfile, you'll be all right. Now, the Dockerfile contains all the instructions that are required to build a container image. And on the Dockerfile, you can use commands like docker build -t docker.io/yourname/myapp:1.0 which will build the container image based on the Dockerfile in the current directory. When you build images, you should tag them for storage on docker.io. That's what -t option is doing. That's adding the tag as well as the version number. If you build images, they will be stored on the local system. And if while building you have used the appropriate tech, they will be ready for uploading to Docker hub, as I will show you in just a bit. Now, one tip. Many images on hub.docker.com have a link to the Dockerfile. If you want to learn more about Dockerfiles, you might want to check it out and have a look at a couple of these links to see advanced-use information about Dockerfile. Now, let me demonstrate. So I have demo Dockerfile in the course Git Repository. Let's use git clone. Clone it. So there we go, dockerfiledemo. That's where I need to be. And in dockerfiledemo, we can see Dockerfile. So let's check it out. What do we see? Well, we see from alpine. Just funny fact: These keywords don't have to be in uppercase. Many times, you will see them in uppercase. I've used from in lowercase just to show you that it also works. from alpine, because alpine is a real, small container image, that is a generic system image. And it's one of my favorites to build my own stuff. MAINTAINER, that is politeness towards the community. That should contain your contact details. So if ever anyone wants to reach out, this is where you do it. Then I'm adding a random file, so ADD ./README.txt to /README.txt. What is that doing? That is checking for a file in the current directory with the name README.txt, and we add it to the container image as /README.txt. And then we have the important thing: install cool software. The RUN command is running commands inside the Dockerfile. So the result of RUN will be in the container image that you are creating. So RUN is how you can actually build your container image by including commands to be executed. In this case, RUN has just one command, apk add bash and nmap. If you want to run multiple commands, then you should use the double ampersand. And probably even better, use double ampersand, bring it to a new line, and outline it the nice way so that you can see what is going on. This double ampersand is making sure that the commands are connected to one another. So, first, you run the first command, then you run the next command, et cetera, until everything is done. And everything is in one run, and that is very important because if you have multiple runs, then you create multiple layers. And multiple layers make your image less efficient. Then we have the ENTRYPOINT. The ENTRYPOINT is the default command. If you use it as an entrypoint, it's difficult to overwrite it. The CMD is the argument to the default command. Instead, you can also put everything in the CMD so then it would look like this: command, and then in the array style we write the entire command, so /usr/bin/nmap, and every single elements should be between double quotes and comma-separated, and this is how you can also use it. Once you're done, and then we can build. So docker build -t docker.io/sandervanvugt/gitopsmap. Now, what is this? This is fully qualified image name. And in this fully qualified image name, I'm referring to the container registry in my accounts to facilitate uploading this image to the Docker environment. So here we can see that it is running through the build process. And this build process is, oh no, is throwing an error. Well, it's pretty obvious what the error is. nmapi, we have one I too many. Nice to see an error. Now, let me fix this error and run it again. It will be faster this time because it picks everything it has successfully done before from a cache. And now we can see that it is done. So docker images is showing me what? It's showing me sandervanvugt/gitopsmap, latest to be there. Now, if I want to run it, docker run sandervanvugt/gitopsmap, and that should do it, and yeah, there we go, it's starting nmap. That's what this container image is supposed to do. I don't care about this application at all. What I care about is Dockerfile procedure. Now, in the end, we need to log in first. So let me use docker login to log in with my username. Login succeeded, and now we can use docker push to docker.io/sandervanvugt/gitopsmap. And that is uploading the container image to my docker.io account. We'll see that as we move forward. So, as you can see, it's now available, and we can do quick test. Quick test is docker images where we can see that the image exists. docker rmi, which is short word for remove image, sandervanvugt/gitopsmap:latest, and, oh boy, it's in use. So I need docker ps -a. So here we can see the stuff container that is creating the problem. I need to remove it, docker rm on musing_galileo. Now it's gone, and now I can use my rmi again to remove the image. And all of that's because I want to show you that if I use docker pull or even docker run, sandervanvugt/gitopsmap:latest, it's telling me, "Can't find the image locally. It's going to fetch some stuff." And there we go. It's starting nmap, and it has fetched the image from the Docker Hub registry. So we now know enough about basic use of a Dockerfile. Now, let's create a Dockerfile that is really going to be useful for what we want to do in this course.

Contents