Skip to content

Commit ac09901

Browse files
authored
Merge pull request docker#8609 from davidism/patch-1
Update Python and Flask usage in Compose tutorial
2 parents cbc9539 + f5da7af commit ac09901

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

compose/gettingstarted.md

+24-18
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Define the application dependencies.
3131
import redis
3232
from flask import Flask
3333

34-
3534
app = Flask(__name__)
3635
cache = redis.Redis(host='redis', port=6379)
3736

@@ -53,9 +52,6 @@ Define the application dependencies.
5352
count = get_hit_count()
5453
return 'Hello World! I have been seen {} times.\n'.format(count)
5554

56-
if __name__ == "__main__":
57-
app.run(host="0.0.0.0", debug=True)
58-
5955

6056
In this example, `redis` is the hostname of the redis container on the
6157
application's network. We use the default port for Redis, `6379`.
@@ -86,19 +82,25 @@ itself.
8682
In your project directory, create a file named `Dockerfile` and paste the
8783
following:
8884

89-
FROM python:3.4-alpine
90-
ADD . /code
85+
FROM python:3.7-alpine
9186
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
9291
RUN pip install -r requirements.txt
93-
CMD ["python", "app.py"]
92+
COPY . .
93+
CMD ["flask", "run"]
9494

9595
This tells Docker to:
9696

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.
9998
* 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`.
102104

103105
For more information on how to write Dockerfiles, see the [Docker user
104106
guide](/engine/tutorials/dockerimages.md#building-an-image-from-a-dockerfile)
@@ -115,7 +117,7 @@ the following:
115117
web:
116118
build: .
117119
ports:
118-
- "5000:5000"
120+
- "5000:5000"
119121
redis:
120122
image: "redis:alpine"
121123

@@ -161,13 +163,13 @@ image pulled from the Docker Hub registry.
161163
Compose pulls a Redis image, builds an image for your code, and starts the
162164
services you defined. In this case, the code is statically copied into the image at build time.
163165
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.
165167
166168
If you're using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for
167169
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
169171
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.
171173
172174
If you're using Docker Machine on a Mac or Windows, use `docker-machine ip
173175
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
219221
web:
220222
build: .
221223
ports:
222-
- "5000:5000"
224+
- "5000:5000"
223225
volumes:
224-
- .:/code
226+
- .:/code
227+
environment:
228+
FLASK_ENV: development
225229
redis:
226230
image: "redis:alpine"
227231
228232
The new `volumes` key mounts the project directory (current directory) on the
229233
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.
231237
232238
## Step 6: Re-build and run the app with Compose
233239

0 commit comments

Comments
 (0)