Docker in Action – Development to Delivery, Part 1

This is a guest post by Michael Herman from Real Python – learn Python programming and web development through hands-on, interesting examples that are useful and fun!

Let’s face it: If your development environment varies from production, you never really know how your application will perform once deployed.

Docker solves this problem. It lets you easily spin up isolated development environments (containers) that are near exact snapshots of the production environment in which it’s deployed. Then when you’re done developing and testing, you can rest assured that your application will behave the same in production since you deploy the entire snapshot (an image), not just the code.

This three part series will teach you everything you need to know about developing with Docker – from setting up your environments and utilizing Flask on Docker to detailing a powerful development workflow that covers setting up a fully functional development environment, on your Mac, and managing continuous integration and delivery.

Docker Setup

Since Darwin (the kernel for OS X) does not have the Linux kernel features required to run Docker containers, we need to install boot2docker – which is a lightweight Linux distribution designed specifically to run Docker. In essence, it starts a small VM that’s configured to run Docker containers.

Make sure you have VirtualBox installed then create a new directory called “flask-docker” to house your Flask project. Download the zip fle from the project repo. Unzip the file and add the contents to the “flask-docker” file.

Next follow the instructions from Docker to install both Docker and the official boot2docker package.

To get the boot2docker ISO image and set up the VM, initialize boot2docker:

sh $ boot2docker init

Again, make sure you have VirtualBox installed, and then run the following command to start the VM and run the Docker daemon:

sh $ boot2docker start

This command should return the DOCKER_HOST environment variables, which should look something like this:

Here we start with an Ubuntu base, install the necessary dependencies on top of it, and then build the Flask application. This file is literally all we need in order to “Dockerize” our app. For more info, check out the official documentation.

Build and run

Let’s get the app up and running!

Build the image

sh $ docker build --rm -t flask-docker-workflow .

Grab a cup of coffee. Or two. This will take some time to build. That said, since Docker caches each step (or layer) of the build process from the Dockerfile, rebuilding will be much quicker because only the steps that have changed since the last build are rebuilt.

Run the container

With the image built, you can now run an instance of your image (the container):

Open your web browser and navigate to the IP address associated with the DOCKER_HOST variable – i.e., http://192.168.59.103/, in this example. (Run boot2docker ip to get the address.) You should see the text “Flask is running on Docker!” in your browser. Try out the /data endpoint as well.

Conclusion and Next Steps

Once done, kill supervisor (Ctrl-C), then run boot2docker down to (gracefully) shutdown the VM. Commit your changes locally, and then push to Github.