- If you don't have Flask already installed, it's easy to do so with a single command: $ pip install flask After Flask has been installed, create a project folder, named FlaskApp for an example. In this folder, create a base file, named something like app.py. Within app.py import the Flask module and create a web app using the following.
- Feel free to swap out virtualenv and Pip for Poetry or Pipenv. Next, let's create a new Flask app. Add an init.py file to the 'project' directory and configure the first route.
- Dockerize A Flask App Mac Os
- Dockerize A Flask App Mac Download
- Dockerize A Flask App Machine
- Dockerize A Flask App Mac Store
- Dockerize A Flask App Mac Download
- Dockerize A Flask App Mac Pro
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 Docker|Creating the Flask Application|Dockerize the Flask Application|Run the Flask Application
To build we use this Docker CLI command: docker build -t vmdockerflask. The command docker build will build our image, with the flag -t it put the tag vmdockerflask in our image and, finally, the last part of the CLI is. (dot) that inform to Docker that the Dockerfile is in the current directory.
After this tutorial, you'll understand what the benefits of using Docker are and will be able to:
- 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.
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:
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.
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.
.dockerignoreLet'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 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.
.envThis 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.
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.
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:
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.
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.
.dockerignoreLet'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 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.
.envThis 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.
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.
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.
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.
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.
Or, if you're ready to master Docker, then check out the Dive Into Docker course.
Dockerize A Flask App Mac Os
Docker Compose simplifies multi-container Docker environments on a single host. Let's put together a basic web app with Docker Compose and Flask using Redis (we end up with a Python/Flask container and a Redis container all on one host). We do not need custom networking or to design our own networks to run on; Compose does everything for us!
Set Up Your Host
Install Docker Engine and Docker Compose (unless you have them already); review Introduction to Docker Compose if you need help.
Dockerize A Flask App Mac Download
You'll need a directory for your project on your host machine:
Add the following to
requirements.txt
in your directory: https://tanttechkaesor1982.mystrikingly.com/blog/change-mac-os-bark-mode-app.Copy and paste the following code into a new file called
app.py
in your project directory:
Your Docker Image
Create your
Dockerfile
in thecompose_flask
directory and add the following:# Design and Print Labels and Inserts for CD, DVD, MiniCD/DVD and More. Free neato cd label software for mac.
Build it:
docker build -t compose-flask .
Define Your Services
Add the following code to a new file, docker-compose.yml
, in your project directory:
How to Read the Docker Compose File
- We define two services,
web
andredis
. - The
web
service builds from theDockerfile
in the current directory… - Forwards the container's exposed port (5000) to port 5000 on the host…
- Mounts the project directory on the host to
/code
inside the container (allowing you to modify the code without having to rebuild the image)… - And links the web service to the Redis service.
- The
redis
service uses the latest Redis image from Docker Hub.
Note this composition documents your application's requirements!
Build and Run with Docker Compose
Start the application from your directory:
If you have localhost access to your host (i.e., you do not use a remote solution to deploy Docker), point your browser to http://0.0.0.0:5000
, http://127.0.0.1:5000
, or http://localhost:5000
. On a Mac, you need to use docker-machine ip MACHINE_VM
to get your Docker host's IP address (then use that address like http://MACHINE_IP:5000
to access your web page). If you do use a remote host, simply use that IP address and append :5000
to the end.
You should see:
When you refresh, you should see:
Each time you refresh, the number should increment.
Stop the application with CTRL+C
(read below under 'Common Issues' for more information) and refresh the page. You should receive something to the effect of This site can't be reached
.
Restart the application with docker-compose up -d
. Redis resets the count and you should see:
Use docker-compose ps
and you should get similar results:
Dockerize A Flask App Machine
Run docker-compose stop
to stop the containers:
Dockerize A Flask App Mac Store
Common Issues
Dockerize A Flask App Mac Download
Starting and Stopping
Dockerize A Flask App Mac Pro
If you run docker-compose up -d
, you need to run docker-compose stop
to stop the services when you finish. If you did not, you can stop the service with CTRL+C
(hit once to gracefully stop and twice to force kill the containers). If you do not run in the background, you can view the calls made to your container.