app.run(host="0.0.0.0", port=8000, debug=True)
```
3. Create a file called `requirements.txt` and paste these two lines in:
```none
flask
redis
```
4. Create a file called `Dockerfile` and paste this in:
```dockerfile
# syntax=docker/dockerfile:1
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
```
5. Create a file called `compose.yaml` and paste this in:
```yaml
services:
web:
image: 127.0.0.1:5000/stackdemo
build: .
ports:
- "8000:8000"
redis:
image: redis:alpine
```
The image for the web app is built using the Dockerfile defined
above. It's also tagged with `127.0.0.1:5000` - the address of the registry
created earlier. This is important when distributing the app to the
swarm.
## Test the app with Compose
1. Start the app with `docker compose up`. This builds the web app image,
pulls the Redis image if you don't already have it, and creates two
containers.
You see a warning about the Engine being in swarm mode. This is because
Compose doesn't take advantage of swarm mode, and deploys everything to a
single node. You can safely ignore this.
```console
$ docker compose up -d
WARNING: The Docker Engine you're using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in
a swarm. All containers are scheduled on the current node.
To deploy your application across the swarm, use `docker stack deploy`.
Creating network "stackdemo_default" with the default driver
Building web
...(build output)...
Creating stackdemo_redis_1
Creating stackdemo_web_1
```
2. Check that the app is running with `docker compose ps`:
```console
$ docker compose ps
Name Command State Ports
-----------------------------------------------------------------------------------
stackdemo_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
stackdemo_web_1 python app.py Up 0.0.0.0:8000->8000/tcp
```
You can test the app with `curl`:
```console
$ curl http://localhost:8000
Hello World! I have been seen 1 times.
$ curl http://localhost:8000
Hello World! I have been seen 2 times.
$ curl http://localhost:8000
Hello World! I have been seen 3 times.
```
3. Bring the app down:
```console
$ docker compose down --volumes
Stopping stackdemo_web_1 ... done
Stopping stackdemo_redis_1 ... done
Removing stackdemo_web_1 ... done
Removing stackdemo_redis_1 ... done
Removing network stackdemo_default