update python and flask usage in dockerfile

* Uses the modern Python 3.7 image, as 3.4 is EOL.
* Separates copying and installing requirements from copying
  project, to make rebuilds more efficient.
* Uses the recommended `flask run` command. This is especially
  needed on Windows, where `app.py` incorrectly looks like an
  executable file when copying into Docker.
* Uses the `FLASK_ENV` env var to control development mode.
This commit is contained in:
David Lord
2019-04-07 02:02:29 -07:00
committed by GitHub
parent 2398cd9dca
commit 86f716623f

View File

@@ -86,19 +86,23 @@ itself.
In your project directory, create a file named `Dockerfile` and paste the
following:
FROM python:3.4-alpine
ADD . /code
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
COPY . .
CMD ["flask", "run"]
This tells Docker to:
* Build an image starting with the Python 3.4 image.
* Add the current directory `.` into the path `/code` in the image.
* Build an image starting with the Python 3.7 image.
* Set the working directory to `/code`.
* Install the Python dependencies.
* Set the default command for the container to `python app.py`.
* Set environment varialbes used by the `flask` command.
* Copy `requirements.txt` and install the Python dependencies.
* Copy the current directory `.` in the project to the workdir `.` in the image.
* Set the default command for the container to `flask run`.
For more information on how to write Dockerfiles, see the [Docker user
guide](/engine/tutorials/dockerimages.md#building-an-image-from-a-dockerfile)
@@ -115,7 +119,7 @@ the following:
web:
build: .
ports:
- "5000:5000"
- "5000:5000"
redis:
image: "redis:alpine"
@@ -158,13 +162,13 @@ Hub registry.
Compose pulls a Redis image, builds an image for your code, and starts the
services you defined. In this case, the code is statically copied into the image at build time.
2. Enter `http://0.0.0.0:5000/` in a browser to see the application running.
2. Enter http://localhost:5000/ in a browser to see the application running.
If you're using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for
Windows, then the web app should now be listening on port 5000 on your
Docker daemon host. Point your web browser to `http://localhost:5000` to
Docker daemon host. Point your web browser to http://localhost:5000 to
find the `Hello World` message. If this doesn't resolve, you can also try
`http://0.0.0.0:5000`.
http://127.0.0.1:5000 or http://0.0.0.0:5000.
If you're using Docker Machine on a Mac or Windows, use `docker-machine ip
MACHINE_VM` to get the IP address of your Docker host. Then, open
@@ -216,15 +220,19 @@ Edit `docker-compose.yml` in your project directory to add a [bind mount](/engin
web:
build: .
ports:
- "5000:5000"
- "5000:5000"
volumes:
- .:/code
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
The new `volumes` key mounts the project directory (current directory) on the
host to `/code` inside the container, allowing you to modify the code on the
fly, without having to rebuild the image.
fly, without having to rebuild the image. The `environment` key sets the
`FLASK_ENV` environment variable, which tells `flask run` to run in development
mode and reload the code on change. This mode should only be used in development.
## Step 6: Re-build and run the app with Compose