Example – Python/Flask container image

To walk through the details of how to use Kubernetes, I have created two sample applications that you can download, or replicate to follow along and try out these commands. The first of these is a very simple Python application using the Flask library. The sample application is directly from the Flask documentation (http://flask.pocoo.org/docs/0.12/).

You can download a copy of this code from GitHub at https://github.com/kubernetes-for-developers/kfd-flask/tree/first_container. Since we will evolve these files, the code referenced here is available at the first_container tag. If you want to use Git to get these files, you can run the following commands:

git clone https://github.com/kubernetes-for-developers/kfd-flask

Then, go into the repository and check out the tag:

cd kfd-flask
git checkout tags/first_container

Let's start with looking at the contents of a Dockerfile, which define what gets built into a container and how that happens.

The goals we have for creating this Dockerfile are:

  • Get and install any security patches for the underlying operating system
  • Install the language or runtime that we need to use to run our code
  • Install any dependencies for our code that are not included directly in our source control
  • Copy our code into the container
  • Define how and what to run
FROM alpine
# load any public updates from Alpine packages
RUN apk update
# upgrade any existing packages that have been updated
RUN apk upgrade
# add/install python3 and related libraries
# https://pkgs.alpinelinux.org/package/edge/main/x86/python3
RUN apk add python3
# make a directory for our application
RUN mkdir -p /opt/exampleapp
# move requirements file into the container
COPY . /opt/exampleapp/
# install the library dependencies for this application
RUN pip3 install -r /opt/exampleapp/requirements.txt
ENTRYPOINT ["python3"]
CMD ["/opt/exampleapp/exampleapp.py"]

This container is based on Alpine Linux. I appreciate the small size of the containers, and there is less extraneous software residing in the container. You will see several commands that may not be familiar, specifically the apk command. This is the command-line tool that helps install, update, and remove Alpine Linux packages. These commands update the package repository, upgrade all installed and pre-existing packages in the image, and then install Python 3 from packages.

If you are already familiar with Debian commands (such as apt-get) or Fedora/CentOS commands (such as rpm), then I recommend you use those base Linux containers for your own work.

The next two commands make a directory on the container at /opt/exampleapp to house our source code and copy it all into place. The COPY command adds everything from the local directory into the container, which is probably more than we need. You can create a file called .dockerignore in the future that will ignore a set of files based on patterns so that some common files that we don't want to be included will be ignored in the COPY command.

Next, you'll see a RUN command that installs the application dependencies, in this case from the file requirements.txt, which is included in the source repository. It is good practice to maintain your dependencies in a file like this, and the pip command was made to support doing exactly that.

The last two commands leverage ENTRYPOINT and CMD separately. For this simple example, I could have used just one or the other. Both are included to show how they can be used together, the CMD being essentially arguments passed to the executable defined in ENTRYPOINT.