@@ -3,21 +3,21 @@ slug: develop-flask-web-apps-docker-containers-macos
3
3
meta: Learn how to set up and develop a new Flask web application within a Docker container.
4
4
category: post
5
5
date: 2018-03-09
6
- modified: 2018-03-09
6
+ modified: 2018-03-14
7
7
newsletter: False
8
8
headerimage: /img/180309-flask-docker/header.jpg
9
9
headeralt: Flask, Docker and Apple logos, copyright their respective owners.
10
10
11
11
12
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 with
15
- containers. Let's quickly get Docker installed and configured for developing
16
- Flask web applications on your local system.
13
+ [ Flask] ( /flask.html ) [ development environment] ( /development-environments.html )
14
+ can be confusing when you are just getting started with containers. Let's
15
+ quickly get Docker installed and configured for developing Flask web
16
+ applications on your local system.
17
17
18
18
19
19
## Our Tools
20
- This tutorial is written for [ Python 3] ( /python-2-or-3.html ) . It may work with
20
+ This tutorial is written for [ Python 3] ( /python-2-or-3.html ) . It will work with
21
21
Python 2 but I have not tested it with the
22
22
[ soon-to-be deprecated 2.7 version] ( https://pythonclock.org/ ) .
23
23
@@ -116,12 +116,29 @@ docker build -t flaskdock .
116
116
The above ` docker build ` file uses the ` -t ` flag to tag the image with
117
117
the name of ` flaskdock ` .
118
118
119
+ If the build worked successfully we can see the image in with the
120
+ ` docker image ls ` command. Give that a try now:
121
+
122
+ ```
123
+ docker image ls
124
+ ```
125
+
126
+ We should then see our tag name in the images list:
127
+
128
+ ```
129
+ REPOSITORY TAG IMAGE ID CREATED SIZE
130
+ flaskdock latest 24045e0464af 2 minutes ago 165MB
131
+ ```
132
+
133
+ Our image is ready to load up as a container so we can write a quick
134
+ Flask app that we will use to test our environment by running it within
135
+ the container.
136
+
119
137
120
138
## Coding A Simple Flask app
121
139
Time to put together a super simple "Hello, World!" Flask web app to test
122
140
running Python code within our Docker container. Within the current
123
- project directory, create a file named ` app.py ` with the following
124
- contents:
141
+ project directory, create a file named ` app.py ` with the following contents:
125
142
126
143
``` python
127
144
from flask import Flask, Response
@@ -139,9 +156,10 @@ if __name__ == "__main__":
139
156
app.run(" 0.0.0.0" , port = 80 , debug = True )
140
157
```
141
158
142
- The above 7 lines of code (not counting blank PEP8-compliant lines) allow our
143
- application to return a simple message when run with the Flask development
144
- server.
159
+ The above 7 lines of code (not counting blank PEP8-compliant lines) in
160
+ [ app.py] ( https://github.com/fullstackpython/blog-code-examples/blob/master/docker-flask-mac/app.py )
161
+ allow our application to return a simple message when run with the
162
+ Flask development server.
145
163
146
164
Save the file and we can give the code a try.
147
165
@@ -165,10 +183,19 @@ directory where your project files, especially `app.py`, are located.
165
183
<img src="/img/180309-flask-docker/flask-app-response.png" width="100%"
166
184
class="shot rnd" alt="Flask app responding to requests from within a Docker container.">
167
185
186
+ Everything worked when you see a simple text-based HTTP response like what
187
+ is shown above in the screenshot of my Chrome browser.
188
+
189
+
168
190
## What's Next?
169
191
We just installed Docker and configured a Flask application to run inside a
170
192
container. That is just the beginning of how you can integrate Docker into
171
- your workflow. Next up take a look at the [ Docker] ( /docker.html ) and
193
+ your workflow. I strongly recommend reading the
194
+ [ Django with PostgreSQL quickstart] ( https://docs.docker.com/compose/django/ )
195
+ that will introduce you to Docker Swarm as well as the core Docker container
196
+ service.
197
+
198
+ Next up take a look at the [ Docker] ( /docker.html ) and
172
199
[ deployment] ( /deployment.html ) pages for more related tutorials.
173
200
174
201
Questions? Let me know via a GitHub
0 commit comments