@@ -31,7 +31,6 @@ Define the application dependencies.
31
31
import redis
32
32
from flask import Flask
33
33
34
-
35
34
app = Flask(__name__)
36
35
cache = redis.Redis(host='redis', port=6379)
37
36
@@ -53,9 +52,6 @@ Define the application dependencies.
53
52
count = get_hit_count()
54
53
return 'Hello World! I have been seen {} times.\n'.format(count)
55
54
56
- if __name__ == "__main__":
57
- app.run(host="0.0.0.0", debug=True)
58
-
59
55
60
56
In this example, ` redis ` is the hostname of the redis container on the
61
57
application's network. We use the default port for Redis, ` 6379 ` .
@@ -86,19 +82,25 @@ itself.
86
82
In your project directory, create a file named ` Dockerfile ` and paste the
87
83
following:
88
84
89
- FROM python:3.4-alpine
90
- ADD . /code
85
+ FROM python:3.7-alpine
91
86
WORKDIR /code
87
+ ENV FLASK_APP app.py
88
+ ENV FLASK_RUN_HOST 0.0.0.0
89
+ RUN apk add --no-cache gcc musl-dev linux-headers
90
+ COPY requirements.txt requirements.txt
92
91
RUN pip install -r requirements.txt
93
- CMD ["python", "app.py"]
92
+ COPY . .
93
+ CMD ["flask", "run"]
94
94
95
95
This tells Docker to:
96
96
97
- * Build an image starting with the Python 3.4 image.
98
- * Add the current directory ` . ` into the path ` /code ` in the image.
97
+ * Build an image starting with the Python 3.7 image.
99
98
* Set the working directory to ` /code ` .
100
- * Install the Python dependencies.
101
- * Set the default command for the container to ` python app.py ` .
99
+ * Set environment variables used by the ` flask ` command.
100
+ * Install gcc so Python packages such as MarkupSafe and SQLAlchemy can compile speedups.
101
+ * Copy ` requirements.txt ` and install the Python dependencies.
102
+ * Copy the current directory ` . ` in the project to the workdir ` . ` in the image.
103
+ * Set the default command for the container to ` flask run ` .
102
104
103
105
For more information on how to write Dockerfiles, see the [ Docker user
104
106
guide] ( /engine/tutorials/dockerimages.md#building-an-image-from-a-dockerfile )
@@ -115,7 +117,7 @@ the following:
115
117
web:
116
118
build: .
117
119
ports:
118
- - "5000:5000"
120
+ - "5000:5000"
119
121
redis:
120
122
image: "redis:alpine"
121
123
@@ -161,13 +163,13 @@ image pulled from the Docker Hub registry.
161
163
Compose pulls a Redis image, builds an image for your code, and starts the
162
164
services you defined. In this case, the code is statically copied into the image at build time.
163
165
164
- 2. Enter ` http://0.0.0.0 :5000/` in a browser to see the application running.
166
+ 2. Enter http://localhost :5000/ in a browser to see the application running.
165
167
166
168
If you're using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for
167
169
Windows, then the web app should now be listening on port 5000 on your
168
- Docker daemon host. Point your web browser to ` http://localhost:5000` to
170
+ Docker daemon host. Point your web browser to http://localhost:5000 to
169
171
find the `Hello World` message. If this doesn't resolve, you can also try
170
- ` http://0 .0.0.0 :5000` .
172
+ http://127 .0.0.1 :5000.
171
173
172
174
If you're using Docker Machine on a Mac or Windows, use `docker-machine ip
173
175
MACHINE_VM` to get the IP address of your Docker host. Then, open
@@ -219,15 +221,19 @@ Edit `docker-compose.yml` in your project directory to add a [bind mount](/engin
219
221
web:
220
222
build: .
221
223
ports:
222
- - "5000:5000"
224
+ - "5000:5000"
223
225
volumes:
224
- - .:/code
226
+ - .:/code
227
+ environment:
228
+ FLASK_ENV: development
225
229
redis:
226
230
image: "redis:alpine"
227
231
228
232
The new `volumes` key mounts the project directory (current directory) on the
229
233
host to `/code` inside the container, allowing you to modify the code on the
230
- fly, without having to rebuild the image.
234
+ fly, without having to rebuild the image. The `environment` key sets the
235
+ `FLASK_ENV` environment variable, which tells `flask run` to run in development
236
+ mode and reload the code on change. This mode should only be used in development.
231
237
232
238
## Step 6: Re-build and run the app with Compose
233
239
0 commit comments