25 – Dockerfile Basic Training

Date: 6/15/2020

Welcome and greetings

I’d like to say a heart-felt thank you my listeners in Clichy, France. Thank you so much for listening each week.

Recap of last episode

  • In our last episode, I discussed Docker, where it came from, some use cases, and a list of docker commands that are useful once you start working with the tool. Here is a link to the full episode:

24 – Tools – Docker

Summary of this episode

  • In this episode, I’ll discuss how to use a dockerfile to enhance your application deployment. We will dig into the structure of the file, what the major commands are and review a sample that builds a web server running apache.

What’s in it for you?

  • At the end of this episode, you will have a foundation level understanding of how dockerfiles are structured and how they are used. You will understand the basic commands and will see how they enable application automation.

Episode Content

  • Dockerfile Usage
    • What is a dockerfile?
      • A dockerfile is a configuration file that tells docker how to build the container that you want. They can be as simple or as complicated as you like, but I tend to think that simpler is better. It just makes it easier to re-build or re-create your container if something goes wrong.
    • How should it be stored?
      • You can put it anywhere that you want but optimally it should be store in the same repository as the application that will be hosted in it.
      • When you put it in a source code repository, it is versioned, you can create feature branches so that multiple team members can work with the same application at the same time, and it is a lot more difficult to “accidentally” delete it from the repository.
    • What do you do with it?
      • Dockerfiles are used to enable your automation.
        • Since it is store with your source code, your automation pulls it when it clones the branch to build. This way your container is no further away than a quick “docker build” command.
  • Dockerfile Use Cases
    • Build an empty container that you can customize
      • You can use them to build an empty test environment where you can deploy your container so that automated testing can be executed
    • Build a container that already has core components installed that your application needs
      • You can build the container that has core components, like Apache, already installed so all you need to do is build your application artifact and then deploy it into the new container.
    • Build a container that already has your indicated version of a custom application installed
      • You can build your application directly into your testing container so that test execution can be done quickly in a fresh environment every time the container is re-created.
  • Dockerfile Structure & commands
    • ARG – this is used to declare variables that can be used later in the dockerfile.
  • FROM – indicates with Operating System image it should be built off of.
FROM alpine:latest
  • VOLUME – creates a external mount point that points the filesystem from the base image
VOLUME /var/log
  • this results in an image that creates a new mount point for the “/var/log” file system when you issue the “docker run” command to create the container
  • RUN – this executes commands on top of the current image and then commits the result as a new image. Think of it like this → base image → update image packages → install your application. This would create 3 separate images which would appear as layers in the final image.
RUN apt-get update
RUN apt-get upgrade -y
  • EXPOSE – this lets the docker engine know that the container will be listening on a certain port. This only informs the docker engine. To enable the port for the container you will still need to use the -p flag when you issue the Docker Run command to start the container. Example: your container listens on port 5443
  • ADD – the add command will copy files, directories, and other items to the filesystem of the image at the given path. You can also add the owner:group permission to the file on the container image’s filesystem. There is also a COPY command perform similar functions as ADD. I will not be going into the differences here.
ADD /host/filesystem/file1 container/filesystem/directory/
  • CMD – there can be only one CMD in a dockerfile and it is used to set defaults for the running container
  • LABEL – this command adds meta-data to a container
label.vendor=”vendor name”
label vendor_version=”1.0”
  • Dockerfile Example – apache web server
# Import the base image
FROM centos:latest

# Install the apache package
RUN yum -y install httpd

# Copy into the default apache home directory any html files you need.
COPY /<host_file_system>/*.html /var/www/html/

# Run the server as soon as the container starts.
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]

# Ensure that port 80 is opened for communtications.
#### These commands are the simplest example that I could come up with
#### from several internet searches of a useful dockerfile that creates an image
#### that will become a container with a useful application already running in
#### it. Please note that Apache offers their own dockerfile in DockerHub.

Recap of this episode

  • To Recap, we discussed the following:
    • What a dockerfile is and where it came from.
    • Some use-cases for dockerfile usage.
    • Some best practices surrounding dockerfiles.
    • A quick reference of some of the commands available to use in dockerfiles.
    • A “simple” sample of a dockerfile to create a container running apache.

I’d like to thank you for joining me in this episode.

I hope that you found some value in what I covered and if you have suggestions for future topics, please feel free to drop me a message and I’ll be sure to review those to work into the schedule. I

f you enjoy this podcast and the topics I cover, you can help me out by sharing the link with your friends. You can also give us a like or a thumbs-up where-ever you listen.

Please leave a review on your favorite podcast service as this will help me become better and ensure that I’m providing the value that you are looking for.

Before you leave, don’t forget to subscribe to ensure that you are notified of future episodes.

Where you can find us!

Direct Messages:

  • @cs_everhart on Twitter
  • ScottTalksTech group on Facebook
  • ScottTalksTech.slack.com

Links to Podcast Providers: