Skip to content

Commit e96c8d8

Browse files
committed
almost done with next post
1 parent 03ae79e commit e96c8d8

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

content/posts/180309-flask-docker-macos.markdown

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ On your Mac,
4949
[download the Docker Community Edition (CE) for Mac](https://www.docker.com/community-edition#/download)
5050
installer.
5151

52-
<img src="/img/180309-flask-docker/docker-ce.jpg" width="100%" class="shot rnd" alt="Download the Docker Community Edition for Mac.">
52+
<img src="/img/180309-flask-docker/docker-ce.jpg" width="100%"
53+
class="shot rnd" alt="Download the Docker Community Edition for Mac.">
5354

5455
Find the newly-downloaded install within Finder and double click on the file.
5556
Follow the installation process, which includes granting administrative privileges
@@ -70,7 +71,8 @@ Docker version 17.12.0-ce, build c97c6d6
7071

7172
Note that Docker runs through a system agent you can find in the menu bar.
7273

73-
<img src="/img/180309-flask-docker/docker-agent.png" width="100%" class="shot rnd" alt="Docker agent in the menu bar.">
74+
<img src="/img/180309-flask-docker/docker-agent.png" width="100%"
75+
class="shot rnd" alt="Docker agent in the menu bar.">
7476

7577
I have found the Docker agent to take up some precious battery life
7678
on my Macbook Pro. If I am not developing and need to max battery time I will
@@ -115,15 +117,54 @@ The above `docker build` file uses the `-t` flag to tag the image with
115117
the name of `flaskdock`.
116118

117119

120+
## Coding A Simple Flask app
121+
Time to put together a super simple "Hello, World!" Flask web app to test
122+
running Python code within our Docker container. Within the current
123+
project directory, create a file named `app.py` with the following
124+
contents:
125+
126+
```python
127+
from flask import Flask, Response
128+
129+
130+
app = Flask(__name__)
131+
132+
133+
@app.route("/")
134+
def hello():
135+
return Response("Hi from your Flask app running in your Docker container!")
136+
137+
138+
if __name__ == "__main__":
139+
app.run("0.0.0.0", port=80, debug=True)
140+
```
141+
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.
145+
146+
Save the file and we can give the code a try.
147+
148+
118149
## Running the Container
119-
Now that we have our image in hand we can run it as a container with the
120-
`docker run` command. Execute the following command, making sure to replace
121-
the absolute path for the volume to your own directory.
150+
Now that we have our image in hand along with the Python code in a file
151+
we can run the image as a container with the `docker run` command. Execute
152+
the following command, making sure to replace the absolute path for the
153+
volume to your own directory.
122154

123155
```
124156
docker run -p 5000:80 --volume=/Users/matt/devel/py/flaskdocker:/app flaskdock
125157
```
126158

159+
If you receive the error
160+
`python: can't open file 'app.py': [Errno 2] No such file or directory` then
161+
you likely forgot to chance `/Users/matt/devel/py/flaskdocker` to the
162+
directory where your project files, especially `app.py`, are located.
163+
164+
165+
<img src="/img/180309-flask-docker/flask-app-response.png" width="100%"
166+
class="shot rnd" alt="Flask app responding to requests from within a Docker container.">
167+
127168
## What's Next?
128169
We just installed Docker and configured a Flask application to run inside a
129170
container. That is just the beginning of how you can integrate Docker into
30 KB
Loading

0 commit comments

Comments
 (0)