Join Arun Gupta for an in-depth discussion in this video Create your first Docker image, part of Docker for Java Developers.
- [Narrator] Alright, so we understood how to run a container, how to manipulate the container, how to do folds, how to do volumes, but those are somebody else's container. I want to run my own container. I want to build my own docker image. How do I play with that? So, this section will show you exactly what does it mean to build a docker image? What does it take to build a docker image? What is a docker file, and how do we get started with that? Well, in order to get started, for building a docker image, you need file, usually called Dockerfile, with a capital D.
The file could be whatever it's called, but that's out of the default convention, and I've seen people use that very consistently. In Dockerfile, you have a bunch of instructions. And those instructions define what will get packaged as part of the docker image. It is required to have the very first non-comment instruction to be from. What that means is that's going to be my base operating system, and pretty soon we will build the image, so you'll understand it more, but there are lots of other instructions that are shown over here. And you can see the link at the bottom of the screen, where you can see the complete set of instructions.
So, in the next few sections, we will go through some of these instructions and understand what they are used for, but for now, let's get started with a simple hello, world image here, okay? For now, we are in a terminal, and let's see, how can we build an image? Well, first of all, we need to create a directory. When you build an image, you always, always want to start with a brand new directory. There's a reason for that, which we'll get to in a second, but let's make a new directory here. Say, hello image, okay? Now, I'm going to go into hello image.
In hello image, what I need is a docker file, okay? So, what I'm going to do is I'm going to say vi Dockerfile. I Dockerfile it, and the very first non-comment instruction as we said, needs to be from, so I'm going to say from, and I'm saying Ubuntu. I want my base operating system to be Ubuntu. Remember the example we saw earlier, where we were talking about the difference between virtual machine and docker images? This is how the minimal base operating system here.
So, I'm going to say from Ubuntu, and in Ubuntu what I'm going to do is, I just want to run simple hello world. So, I'm going to give it a CMD instruction, CMD is basically the command that the docker container is going to run. Now, as we said earlier, docker containers can run only one, and one thing, and they do that really well. So, this is my CMD, which is basically my entry point to the docker, and here I'm saying echo hello world. That's it. That is a very simple hello world docker image.
Now you may wonder, I am saying from Ubuntu, where does Ubuntu come from? Well, go back to our docker hub. That's where our default Ubuntu image is available. If you don't say anything, if you don't specify the origin of Ubuntu, it'll download the latest Ubuntu image for you, and then it'll run the command over there. Before we get into the details, let's take a look at how do we build this image. So, let's save this docker file, and if I look at the listing here, in this directory I only have docker file, okay? Let's build our docker image here. So, I'm going to say, docker image.
That is the main management command that I need to use. Before I build the image actually, let's take a look at what kind of commands are available. Once again, we go back to dash dash help. You can build an image. You can see the history of an image, which will tell you exactly what other images are available, and how the image was created using different layers. You can inspect the image, get more detail about the image. There are capabilities by which you can save an image to a tar file, and then later on load an image from tar file, you can list the images, and there are other commands that you can take a look at as well.
But, from our perspective, we're going to say, docker image build, because we need to build the image, and we want to give the image a tag name, so we're going to call it hello world. And, we're going to say dot. Now, this is where creating a brand new directory is important. Because, essentially what's happening now is the docker CLI is giving a command to the docker host, build an image, and by saying dot, I am giving it a context. And I'm giving the current directory as the context. An entire context is sent from the client to the host.
Now, if you do that in a directory which is populated with lot of other files and directories, entire content is then sent over to the host, which is not really necessary. It is very important that you actually create a new directory, and put Dockerfile over there, so that's it's not (mumbles). Now we understand sometimes it may not be possible to do that, in which case, just like in a get directory, or in a get project, you have dot get ignore, similarly you can have a dot docker ignore. If you really have to do this in a directory which has a lot of other files and directories, then you can have dot docker ignore file, which follows very similar format as dot get ignore.
From our persepective, it's just docker image build dash t hello world dot, and it's an empty directory, so we're going to build this image. Now of course our ubuntu image is not there. So it's going to download the image for ubuntu first. And that is going to build the image. That was rather quick, so the image is ready for us now. Well, we can see the listing of the image. And we can see that our hello world image is here, and Ubuntu is here as well. And you can see the base operating system is 129, and there are no additional layers, so to say, that were added for the docker image, because it's just a command, so there is nothing additional that is sitting on top of Ubuntu over here.
Now, I can say docker history, hello world, and then it's showing me how exactly the image was being built. The latest one was 30 seconds ago, because we just added it. And here it's showing me the command here, but then prior to that is all Ubuntu commands. Basically saying, okay, this is the base operating system, and we need to do pseudo app get update, the usual Ubuntu commands that you will do, those are done by the Ubuntu image itself. Now, how do I run this image? Well, let's run it then.
Docker, container, run, I'm going to call it hello world. (cheers) So we got a simple hello, world image up and running over here. Now, we talked about dot docker ignore, so let's add another aspect of it, because your build context, which is a current directory, and everything in it, is sent to the docker host, so you can also specify when you are building your image, let's say we do dash dash help here, okay? There are other commands that are very, very useful to use. So, for example you can say dash dash compress, now in this case, the docker CLI, or the docker client, will compress the context and send it in a g zip format to the host.
So if your context is too big, this could really help. Another one is dash dash squash. Now, each command typically in a docker file would add a new layer to the docker image, because it's using augmented file system. So, if you want to reduce the number of layers, or compress all of those layers into a single layer, it's important that you use dash dash squash command. Now, these commands were introduced in docker 1.13, so they'll only work there, so you got to be a little aware about it, but in this section, essentially what we saw is, what does it take to build a simple hello world docker image, and how to run it as a container.
Arun Gupta explains how to run containers and build images, as well as how to deploy multi-container applications on multiple hosts. Arun also covers Docker clustering, demonstrates how to run stateful containers, shares strategies on how to persist the state of stateful containers, and goes into monitoring Docker containers.
- Getting started with Docker
- Running your first Docker container
- Creating your first Docker image
- Copying files in a Docker image
- Running JAR files from a Docker image
- Working with Docker Compose
- Creating a multi-node swarm mode cluster using Docker Machine
- Deploying services to swarm mode
- Creating a multi-node swarm mode cluster on AWS
- Creating a database cluster using Docker Service
- Monitoring Docker with Prometheus
- Monitoring Docker with cAdvisor