This guide describe different ways of running the subprojects in this repository. Every project builds upon the previous one, each one adds new more complex features:
-
00_HelloWorld: Test the connection to Mongo DB.
-
01_CRUD: Implements a CRUD API on a single file using Flask and Mongo DB.
-
02_CRUD_Stragegy: Implements a CRUD API using the Strategy design pattern and split the implementation in several file and classes.
cd <path/to/subproject>❗ This can be run locally with python app.py if an mongo db server is already running in the host machine and all dependencies are already installed. Is highly suggested to use a virtual environment (venv).
To tie these containers together, use Docker Compose. Here's a docker-compose.yml file to run both services:
docker-compose.yml:
version: '3.8'
services:
flask-app:
build: .
container_name: flask-app
ports:
- "5000:5000"
networks:
- app-network
environment:
FLASK_APP: app.py
FLASK_RUN_HOST: 0.0.0.0
FLASK_ENV: development
working_dir: /00_HelloWorld
command: ["flask", "run"]
mongodb-server:
image: mongo
container_name: mongodb-server
ports:
- "27017:27017"
networks:
- app-network
networks:
app-network:
driver: bridgeThis file defines two services:
webfor your Flask app, building the Dockerfile in the current directory.mongofor MongoDB, using the official image.
The depends_on clause ensures that the mongo service is started before the web service. The volumes directive for MongoDB persists its data across container restarts.
Finally, start both containers with Docker Compose:
docker-compose -d upNow, your Flask app should be able to communicate with MongoDB. The Flask app is accessible on localhost:5000, and it will connect to MongoDB running in another container.
Tips and Considerations:
- Networks: Docker Compose sets up a default network where services can discover each other by service names (like
mongoin this case). - Security: For production, consider security aspects. Don’t expose MongoDB port unless necessary, use environment variables for sensitive data, and set up proper authentication.
- Debugging: If there are connectivity issues, check the logs and ensure the services are on the same network.
- MongoDB Configuration: Depending on your use case, you might want to customize the MongoDB configuration.
Lastily to stop the containers
docker-compose downTo simplify you execution you can run the following scripts instead of running the arguments manually in the command line
build.py:
- Build mongo image
- Build flask app image
- Creates the required docker network to communicate the containers
python build.pyrun.py:
- Run a temporary mongo container in deattached mode
- Run a temporary flask container in deattached mode
python run.pyclean.py:
Forcefully:
- Removes mongo container
- Removes flask container
- Removes created network
python clean.pyNotes
- Flask app container cannot communicate with mongo server either locally or in a separate container if the containers are not configured to use the same network
Setting up a Flask app and MongoDB using docker build and docker run instead of Docker Compose involves a few more manual steps, but it's definitely doable and provides a good understanding of how containers interact.
First, you need to build a Docker image for your Flask application. Assuming you have the same Dockerfile and app.py from the previous example, you can build the image like this:
docker build -t flask-app . -f ../DockerfileThis command builds the Docker image with the tag flask-app based on the Dockerfile in the current directory.
For the Flask app to communicate with MongoDB, both containers need to be on the same network. You can use the default bridge network, but creating a custom network provides better control:
docker network create app-networkIf is the mongo container is already running you can connect the MongoDB container to this network:
docker network connect app-network mongodb-serverNext, run the MongoDB container. You can pull the official MongoDB image and run it:
docker run --rm --name mongodb-server --network app-network -d -p 27017:27017 mongoThis command runs MongoDB in a container named mongo, exposes port 27017 (the default MongoDB port), and runs it in detached mode.
Now, run your Flask app container and connect it to the same network:
docker run --rm --name flask-app --network app-network -d -p 5000:5000 flask-appThis command runs your Flask app in a container named flask-app, exposes port 5000, and connects it to the app-network.
At this point, your Flask application should be running and able to communicate with MongoDB. You can verify by accessing http://localhost:5000 in your browser or using a tool like curl:
curl http://localhost:5000This should return a message confirming the connection to MongoDB.
If you encounter any issues, check the logs of your containers:
docker logs flask-app
docker logs mongodb-serverWhen you're done, you can stop and remove the containers:
docker stop flask-app mongodb-server
docker rm flask-app mongodb-serverAnd if you created a custom network, you can remove it too:
docker network rm app-network- Data Persistence for MongoDB: If you need to persist data, you can mount a volume to the MongoDB container.
- Environment Variables: You can pass environment variables (like database URL, credentials, etc.) to your Flask app using
-eflag withdocker run. - Security: Be cautious with port exposure and security configurations, especially if you're planning for a production deployment.
This approach gives you a more granular control over your containers and helps in understanding the networking and communication between different Docker containers.