2020-03-06 18:25:16 +00:00
## Compose sample application
2022-09-01 16:39:33 +00:00
### Use with Docker Development Environments
You can open this sample in the Dev Environments feature of Docker Desktop version 4.12 or later.
[Open in Docker Dev Environments <img src="../open_in_new.svg" alt="Open in Docker Dev Environments" align="top"/> ](https://open.docker.com/dashboard/dev-envs?url=https://github.com/docker/awesome-compose/tree/master/nginx-flask-mongo )
2020-03-06 18:25:16 +00:00
### Python/Flask application with Nginx proxy and a Mongo database
Project structure:
```
.
2022-05-10 09:59:25 +00:00
├── compose.yaml
2020-03-06 18:25:16 +00:00
├── flask
│ ├── Dockerfile
│ ├── requirements.txt
│ └── server.py
└── nginx
└── nginx.conf
```
2022-05-10 09:59:25 +00:00
[_compose.yaml_ ](compose.yaml )
2020-03-06 18:25:16 +00:00
```
services:
web:
build: app
ports:
- 80:80
backend:
build: flask
...
mongo:
image: mongo
```
The compose file defines an application with three services `web` , `backend` and `db` .
2022-05-10 09:59:25 +00:00
When deploying the application, docker compose maps port 80 of the web service container to port 80 of the host as specified in the file.
2020-03-06 18:25:16 +00:00
Make sure port 80 on the host is not being used by another container, otherwise the port should be changed.
2022-05-10 09:59:25 +00:00
## Deploy with docker compose
2020-03-06 18:25:16 +00:00
```
2022-05-10 09:59:25 +00:00
$ docker compose up -d
2020-03-06 18:25:16 +00:00
Creating network "nginx-flask-mongo_default" with the default driver
Pulling mongo (mongo:)...
latest: Pulling from library/mongo
423ae2b273f4: Pull complete
...
...
Status: Downloaded newer image for nginx:latest
Creating nginx-flask-mongo_mongo_1 ... done
Creating nginx-flask-mongo_backend_1 ... done
Creating nginx-flask-mongo_web_1 ... done
```
## Expected result
2020-07-28 03:22:17 +00:00
Listing containers must show three containers running and the port mapping as below:
2020-03-06 18:25:16 +00:00
```
$ docker ps
2020-03-23 23:36:46 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0f4ebe686ff nginx "/bin/bash -c 'envsu…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-flask-mongo_web_1
2020-03-06 18:25:16 +00:00
dba87a080821 nginx-flask-mongo_backend "./server.py" About a minute ago Up About a minute nginx-flask-mongo_backend_1
d7eea5481c77 mongo "docker-entrypoint.s…" About a minute ago Up About a minute 27017/tcp nginx-flask-mongo_mongo_1
```
After the application starts, navigate to `http://localhost:80` in your web browser or run:
```
$ curl localhost:80
2021-05-21 16:53:49 +00:00
Hello from the MongoDB client!
2020-03-06 18:25:16 +00:00
```
Stop and remove the containers
```
2022-05-10 09:59:25 +00:00
$ docker compose down
2020-03-06 18:25:16 +00:00
```