Creating a Docker image

So I wrote a simple network/IP calculator program that runs under Flask. Running multiple Flask apps on a single server can be a pain and since I thought would share the calculator I thought it only made sense to dockerize it. The best explanation I’ve heard on how to docker an application is layers. Let’s take a look at the Dockerfile for the application:

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y python3 python3-dev python3-pip git nano
RUN pip3 install netaddr Flask requests
RUN git clone .
ENTRYPOINT [“python3”]
CMD [“”]

Let’s break each line down. The first layer, FROM ubuntu:latest is the base layer. What we are asking docker to do is build a container from a copy of the ubuntu image with the tag of latest. So now that we have our OS, lets do some more. EXPOSE 80 opens port 80 to the host from the container. This is needed so we can get to the web app. Next the RUN command instructs the container to run a command. Pretty straight forward there. We run apt update (no sudo needed in a container). Then we install python3, python3-dev, python3-pip, git and nano. Now that we have python and pip installed we can install the modules for out python application. The modules we are installing are netaddr, Flask and requests. With WORKDIR we set our working directory. This is where we will install out application. That leads to the next part, another RUN. In this run we will be using GIT to download the application. ENTRYPOINT creates a place to start in the container and CMD is simply a command that we want to run ( That is all we should need for this application. When we build a container from this image, there is one import thing to notice. We exposed port 80 from the container, but we did not PUBLISH it to a specific port. Using EXPOSE we allow Docker to generate the published port. After building a container from this image, you can see Docker decided to use port 32776 to map to port 80 in the image.

To access the application, open a browser and enter the IP of the Docker host and the generated port. In my example,

I user Portainer to manage Docker and create images. It makes everything significantly easier IMO. Also if you are interested in the application it can be found on my Github page or in DockerIO.