From the course: DevOps Foundations: Your First Project

Writing your first Dockerfile

The life of a Docker container image starts with a single file: the Dockerfile. Dockerfiles are manifests that describe the image that your container will use and what will run within it. When we create the container that will run our website, Docker will do four things with it. First, it will read and parse the Dockerfile. Second, it will fetch the parent image that the Dockerfile requested or the scratch base image if you're starting from a blank slate. Third, it will run any commands that are defined within the Dockerfile on top of that base image or parent image. And finally, if defined, Docker will set the image's entry point or the process that will run when this container runs, along with any arguments provided to it. Now, unlike traditional configuration management tools like Chef, Puppet or Ansible, all of the configuration, dependencies and other environment settings that your application needs to run itself can be defined within the Dockerfile. Additionally, while you can write scripts that perform the installation steps that are going to be noted within the Dockerfile, those are not required. So, with that, let's open up our editor and write a really simple Dockerfile for our app. Now, before we dive in, let's take a look at the exercise files bundle that you downloaded for the course. Your exercise file should look something like this. You'll see two folders for each chapter that come with an exercise file: a "Before" folder, and an "After" folder. Our website is going to be in the "02_03_before" folder, as you can see here. We're going to use the terminal to copy this directory into a working directory. That working directory will contain our Dockerfile that we're going to create, along with everything else that we will be creating throughout this journey. Afterwards, we'll write our Dockerfile and build an image from it. Let's open the terminal by typing Command + Space, then typing "terminal" and hitting Enter. Now, let's first make sure that we're in the right place. Your terminal should automatically put you in your home directory, which looks like this tilde over here. If you're not in that directory or if you can't see that tilde, just type "cd ~" to take you right there. Cool. Now that we're all in our home directory, let's create our working directory by typing "mkdir explore- california-website". Finally, let's copy that website folder into it. We can do that by typing "cp -r", then the folder that you downloaded your exercise files into or, in my case, "~/Downloads/ exercise_files... ...then /02_03_before/website". Afterwards, put a space. Then "./ explore-california-website". "." is short for our current directory, which is our home directory right now. We need to specify "-r" over here to cp to tell it that we're copying a directory and want to copy recursively. So once you typed out the command, which should look like this, hit Enter. Once that's done, run "ls ./explore- california-website" to confirm that your website directory was copied over. You should see website like shown here. If you do then you're ready to move on. All right, let's clear the screen by typing Ctrl + L, then let's type "cd ./explore" + Tab to move into our working directory. The Tab autocompletes links, that saves us time so that we don't have to type the entire Explore California website into our terminal. From here, let's type "touch Dockerfile" to create a blank Dockerfile. Then, finally, open Dockerfile in your preferred editor. While I'm using Vim here, any editor like Visual Studio Code, IntelliJ or Eclipse will also work. All Dockerfiles start with a "FROM" command. "FROM" tells Docker which image to use as a base for the commands that we're about to run. While most Dockerfiles will probably use a pre-existing image, like Ubuntu or Debian, you don't always have to. If you want to start from a completely blank slate, you'll want to use from scratch here. Note that blank slate means blank slate. The scratch image is completely empty. You'll need to configure everything yourself, so I don't recommend doing this, but if your situation requires it, then knock yourself out. Let's remove scratch here and make this image "FROM hello-world". That's the same image that we used earlier to confirm that Docker is working. Next we can also add a label underneath FROM. Labels help describe your image to other users. A popular label that gets added is the "maintainer" label. This tells people who to contact if they have any questions or run into trouble. Finishing this line, I'm going to add an equal sign, then a double quote. Then you can add your name here like Carlos Nuñez. Then an ankle bracket and then your email address. Then a closing angle bracket and then a closing double quote. Putting that all together, you should have "LABEL maintainer = your name". And then, in angle brackets, your email address. The next thing that we're going to do is copy that website directory that we created earlier into our Docker image. We can do that by typing "COPY website /". COPY, as the name implies, copies stuff into your image. Very handy for us since we want our image to have a website in it. And there you have it. You wrote your first Dockerfile. As you can see, it doesn't do very much. But don't worry, our Dockerfile will become much more useful as our journey with Explore California continues. Now what I'm going to do is I'm going to save this file and quit my editor. So for those following along with what I'm doing in Vim, what I'm going to do is type Escape or Control + [. Then I'm going to type ":w" to save my file. And then I'm going to type ":q" to exit my editor. Right, now that we're back in our terminal let's clear our screen and type "docker build --tag explore- california-website", and then a dot. Let's break this command down "Docker build" tells Docker that we are going to build a new Docker image. The "tag" option tells Docker that we want to give this image a name, or "explore-california-website", in our case,. The dot at the end is not a typo. This tells Docker where to find its context, or the files it needs to locate everything needed by the Dockerfile. Like we learned earlier, the dot is short for our current working directory, so that will just tell Docker to use explore-california-website, since that's the directory that we are currently in. Now let's run this and watch Docker do its thing. Cool. It looks like this image was successfully built and tagged. Let's verify that by running Docker images to make sure that it exists in the list of images on my computer. As you can see, explore-california-website is in the output that Docker image is generated. There it is, we successfully built our app's first container image. Now, if you ever want to find out more about what any particular command does, you can always run "docker", then the command like "build --help". That will give you a help menu like we saw earlier when we tested Docker. Finally, let's run it. This part's really easy. Let's clear our screen to organize, and then all we have to do here is run "docker run explore-california-website". This will create a container from our explore-california-website Docker image. Since Docker does not delete containers by default after they're done doing their thing, I like to add "--rm" before the image name, or "explore-california-website" in this case, to have Docker do that. Let's hit Enter and see what happens. As you can see, we see a "Hello from Docker!" message over here. That confirms that our image was successfully built. So there you have it. We did it. We built a Docker container image.

Contents