|
| 1 | +title: Developing Flask Apps in Docker Containers on macOS |
| 2 | +slug: develop-flask-web-apps-docker-containers-macos |
| 3 | +meta: Learn how to set up and develop a new Flask web application within a Docker container. |
| 4 | +category: post |
| 5 | +date: 2018-03-09 |
| 6 | +modified: 2018-03-09 |
| 7 | +newsletter: False |
| 8 | +headerimage: /img/180309-flask-docker/header.jpg |
| 9 | +headeralt: Flask, Docker and Apple logos, copyright their respective owners. |
| 10 | + |
| 11 | + |
| 12 | +Adding [Docker](/docker.html) to your [Python](/why-use-python.html) and |
| 13 | +[Flask](/flask.html) [development environment's](/development-environments.html) |
| 14 | +workflow can be a bit confusing when you are just getting started working with |
| 15 | +containers. Let's quickly get Docker configured for developing Flask web |
| 16 | +applications on your local system. |
| 17 | + |
| 18 | + |
| 19 | +## Our Tools |
| 20 | +This tutorial is written for [Python 3](/python-2-or-3.html). It may work with |
| 21 | +Python 2 but I have not tested it with the |
| 22 | +[soon-to-be deprecated 2.7 version](https://pythonclock.org/). |
| 23 | + |
| 24 | +[Docker for Mac](https://docs.docker.com/docker-for-mac/install/) is necessary. |
| 25 | +I recommend the stable release unless you have an explicit purpose for the edge |
| 26 | +channel. |
| 27 | + |
| 28 | +Within the Docker contain we will use: |
| 29 | + |
| 30 | +* Python 3, specifically the |
| 31 | + [slim-3.6.4 version](https://hub.docker.com/r/library/python/tags/3.6-slim/) |
| 32 | + from Docker Hub |
| 33 | +* [Flask](/flask.html) version 0.12.2 |
| 34 | + |
| 35 | +All of the code for the Dockerfile and the Flask app are available open source |
| 36 | +under the MIT license on GitHub under the |
| 37 | +[docker-flask-mac directory](https://github.com/fullstackpython/blog-code-examples/tree/master/docker-flask-mac) |
| 38 | +of the |
| 39 | +[blog-code-examples](https://github.com/fullstackpython/blog-code-examples) |
| 40 | +repository. Use the code for your own purposes as much as you like. |
| 41 | + |
| 42 | + |
| 43 | +## Installing Docker on macOS |
| 44 | +We need to install Docker before we can spin up our Docker containers. If you |
| 45 | +already have Docker for Mac installed and working, feel free to jump to the |
| 46 | +next section. |
| 47 | + |
| 48 | +On your Mac, |
| 49 | +[download the Docker Community Edition (CE) for Mac](https://www.docker.com/community-edition#/download) |
| 50 | +installer. |
| 51 | + |
| 52 | +Install Docker. |
| 53 | + |
| 54 | +Open Terminal. |
| 55 | + |
| 56 | +Test your Docker installation with the `docker --version` command. |
| 57 | + |
| 58 | +``` |
| 59 | +Docker version 17.12.0-ce, build c97c6d6 |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +## Dockerfile |
| 64 | + |
| 65 | +``` |
| 66 | +FROM python:3.6.4-slim |
| 67 | +
|
| 68 | +WORKDIR /app |
| 69 | +
|
| 70 | +ADD . /app |
| 71 | +
|
| 72 | +RUN pip install --trusted-host pypi.python.org -r requirements.txt |
| 73 | +
|
| 74 | +EXPOSE 80 |
| 75 | +
|
| 76 | +ENV DEBUG True |
| 77 | +
|
| 78 | +CMD ["python", "app.py"] |
| 79 | +``` |
| 80 | + |
| 81 | +``` |
| 82 | +docker build -t flaskdock . |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +## Running the Container |
| 87 | +Make sure to replace the absolute path for the volume to your own directory. |
| 88 | +``` |
| 89 | +docker run -p 4000:80 --volume=/Users/matt/devel/py/flaskdocker:/app flaskdock |
| 90 | +``` |
| 91 | + |
| 92 | +## What's Next? |
| 93 | +We just installed Docker and configured a Flask application to run inside a |
| 94 | +container. That is just the beginning of how you can integrate Docker into |
| 95 | +your workflow. Next up take a look at the [Docker](/docker.html) and |
| 96 | +[deployment](/deployment.html) pages for more related tutorials. |
| 97 | + |
| 98 | +Questions? Let me know via a GitHub |
| 99 | +[issue ticket on the Full Stack Python repository](https://github.com/mattmakai/fullstackpython.com/issues), |
| 100 | +on Twitter |
| 101 | +[@fullstackpython](https://twitter.com/fullstackpython) |
| 102 | +or [@mattmakai](https://twitter.com/mattmakai). |
| 103 | + |
| 104 | +Do you see a typo, syntax issue or just something that's confusing in this |
| 105 | +blog post? Fork |
| 106 | +[this page's source on GitHub](https://github.com/mattmakai/fullstackpython.com/blob/master/content/posts/180309-flask-docker-macos.markdown) |
| 107 | +and submit a pull request with a fix or |
| 108 | +[file an issue ticket on GitHub](https://github.com/mattmakai/fullstackpython.com/issues). |
0 commit comments