You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Create app.py
> Place it inside another folder named *web* (or any other name of your choice)
> We can place our DB in another container by simply creating another folder *db* and then dockerize it as we do for *web*
> Now create a file **_Dockerfile_** and _requirements.txt_ (any name of your choice) in inside the web folder
> Create a file **_docker-compose.yml_** in main directory where you are having *web* and *db* folder (It's for managing the
different docker containers)
Directory Structure
.
├── db
├── docker-compose.yml
└── web
├── app.py
├── Dockerfile
└── requirements.txt
Inside Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
frompymongoimportMongoClientapp=Flask(__name__)
client=MongoClient("mongodb://db:27017") # Docker Handles it, here db comes from the folder name and 27017 is MongoDB portdb=client.aNewDB# Database creationUserNum=db['UserNum'] # CollectionUserNum.insert({
'num_of_users':0
})
@app.route('/visit', methods=['GET'])defvisit():
prev_num=UserNum.find({})[0]['num_of_users']
new_num=prev_num+1UserNum.update({}, {"$set":{"num_of_users":new_num}}) # Here Key also a stringreturnstr(new_num)
Now Running it
docker-compose build
docker-compose up
docker-compose rm # To remove the container, so to start from fresh DB