Learn how to install and use Docker to run a multi-service Flask, Celery and Redis application in development with Docker Compose.

Quick Jump: What Is Docker and Why Is It Useful?Installing DockerCreating the Flask ApplicationDockerize the Flask ApplicationRun the Flask Application

After this tutorial, you’ll understand what the benefits of using Docker are and will be able to:

Redis is an open source key-value store that functions as a data structure server. Sep 17, 2020 Docker Compose is yet another useful Docker tool. It allows users to launch, execute, communicate, and close containers with a single coordinated command. Essentially, Docker Compose is used for defining and running multi-container Docker applications. This tutorial shows you how to install Docker Compose on Ubuntu 20.04 and how to run a container.

  • Install Docker on all major platforms in 5 minutes or less
  • Clone and run an example Flask app that uses Celery and Redis
  • Know how to write a Dockerfile
  • Run multiple Docker containers with Docker Compose

Also, there’s a free email course to learn a bit about Docker at the bottom of this post.

What Is Docker and Why Is It Useful?

Docker allows you to package up an application or service with all of its dependencies into a standardized unit. This unit is typically labeled as a Docker image.

Everything the application needs to run is included. The Docker image contains the code, runtime, system libraries and anything else you would install on a server to make it run if you weren’t using Docker.

To get a better idea of how Docker will affect you on a day to day basis as a software developer I highly recommend you read one of my previous blog posts which will save you from years of turmoil by using Docker.

There’s also another post I wrote which directly compares setting up a Python development environment with and without Docker. It focuses on developing a web app specifically.

Installing Docker

The code base we’ll be working with is compatible with all modern versions of Docker and Docker Compose. Feel free to install the latest stable release of Docker.

This guide expects you to have Docker already installed. If you’re at ground 0 then you may want to sign up for the free Docker email course at the bottom of this post, because it covers a myriad of ways to install Docker on Mac, Windows and Linux.

Otherwise, feel free to check out Docker’s documentation on installing Docker if you want to get going right now. To help you out, I’ve also written a comparison guide on Docker for Mac / Windows vs Docker Toolbox.

Ensure Docker and Docker Compose Are Working

Before continuing on you should see what I see, or something very similar:

Creating the Flask Application

We’re going to be using the open source version of the application in my Build a SAAS App with Flask course.

The open source version only covers a tiny fraction of what the course covers, but it will be more than enough to exercise how to use Docker in development.

Clone the Project

The reason we’re checking out that commit is because I’ve done many free updates to this course and the current release is way newer than this blog post.

So much has changed since then, and I wanted to make sure you can still follow along in this post. You can always upgrade afterwards.

Open the Project in Your Favorite Code Editor

Feel free to use whatever editor you want, but if you like Sublime Text 3 and you want to configure it for Python, Docker and more then check out my post on 25 Sublime Text 3 Packages for Polyglot Programmers.

Dockerize the Flask Application

There’s a few things we need to do to Dockerize the application.

Docker Install Redis

Logging

In order for logs to function properly, Docker expects your application or process to log to STDOUT. Lucky for us, Flask does this by default.

Docker Specific Files

The root of the project has a few files that are related to Docker:

Apple store sierra. The only file that’s necessary to add is the Dockerfile but you’ll find that most web applications that are Docker-enabled will have the others.

Dockerfile

Let’s start off with the Dockerfile because to talk about the other files will require having a little bit of knowledge about how Docker images get built.

You can think of this file as your Docker image blueprint or recipe. When you run the docker build command it will execute each line from top to bottom.

It’s going to run all of these commands in the context of the Docker image.

To get a better understanding of this file, then check out my shiny new Dive into Docker course (which is even more up to date than this article).

At this point we could build the image and you’d be able to access the Flask app, but let’s avoid doing that for now.

.dockerignore

Let’s first look at the next file which is the .dockerignore file.

When we copied in all of the files from our current directory into the Docker image with the COPY . . command, it’s going to copy literally everything.

That’s not the best idea in the world because if your project is a git repo you’re going to have a TON of extra data. You should strive to have the smallest Docker images you can within reason.

The .dockerignore file is very similar to a .gitignore file. It lets you black list certain folders or files from being included.

In our case, we’re ignoring the git folder but we’re also excluding the .dockerignore file itself because it’s not part of our Flask application.

Docker Install Redis Cluster

docker-compose.yml

Docker Compose is an official tool supplied by Docker. At its core, it’s a utility that lets you “compose” Docker commands and manage multiple containers in an easy way.

Let’s take a glance at the docker-compose.yml file:

It looks a lot more complicated than it is. All of those variables wrapped in ${} are coming in from the .env file which we’ll see below.

Dive Into Docker covers everything in great detail if you want to see how all of this ties together. Often times knowing the “why” is more important than seeing how it’s done. That is what enables you to apply things on your own.

.env

This file isn’t technically part of Docker, but it’s used by Docker Compose.

By default Docker Compose will look for an .env file in the same directory as your docker-compose.yml file.

We can set various environment variables here, and you can even add your custom environment variables here too if your application uses ENV variables.

By setting the COMPOSE_PROJECT_NAME to snakeeyes, Docker Compose will automatically prefix our Docker images, containers, volumes and networks with snakeeyes.

In addition to COMPOSE_PROJECT_NAME you’ll notice many other env variables. Each of them are commented, so feel free to check them out on your own.

Run the Flask Application

You can run everything by typing: docker-compose up --build. Docker Compose has many different sub-commands and flags. You’ll definitely want to check them out on your own.

After the up command finishes, open up a new terminal tab and check out what was created on your behalf.

Docker Images

Run docker images:

Docker Compose automatically pulled down Redis and Python for you, and then built the Flask (web) and Celery (worker) images for you.

Docker Containers

Run docker-compose ps:

Docker Compose automatically named the containers for you, and it appended a _1 because it’s running 1 instance of the Docker image. Docker Compose supports scaling but that goes beyond the scope of this tutorial.

Docker Install Redis

We can also see which ports the services are using. Only the web service has port 8000 published in such a way that you can access it in a browser. The other ports you see for Redis and the worker are acting as documenting for which ports it could technically publish if you wanted to.

There’s a lot more to go over but the above is enough to get rolling.

Viewing the Site

If you’re using Docker Desktop (Mac / Windows) or native Linux you can visit http://localhost:8000 in your browser and you should see the home page.

At this point you have a Dockerized Flask application running. Congrats!

If you installed Docker through the Docker Toolbox then you’ll need to make 1 change to the .env file. Check out the SERVER_NAME=localhost:8000 value.

You will need to change localhost to your Docker Machine IP address instead. Chances are that will be 192.168.99.100 but if it’s not, you can find your Docker Machine IP by running docker-machine ip and then visit http://192.168.99.100:8000 in your browser instead of localhost.

Docker Install Redis Mac

By the way, if you’re using Docker Toolbox and you tried to submit the contact form and you received a CSRF token error then check out how to fix this problem. Spoiler alert: it’s a bug with Chrome.

Shutting Things Down

You’ll want to goto your Docker Compose terminal tab and press CTRL+C. Most of the time that works but sometimes you get an ABORT error. If that happens you can run docker-compose stop to stop everything.

Docker Install Redis Dockerfile

Conclusion

Docker is awesome. Now you can run your projects on other platforms without having to worry about dependencies and platform specific gotchas.

You can even deploy your projects to production with minimal fuss.

Docker Install Redis Php

You can learn much more about Flask by checking out the Build a SAAS App with Flask course. I keep the course updated regularly so it’s never out of date.

Docker Install Redis Windows

Or, if you’re ready to master Docker, then check out the Dive Into Docker course.

Coments are closed
Scroll to top