Deploying a Mongo Database with Docker

David Guerrero
3 min readJul 15, 2021

--

Deploying droplets and connecting them with Caprover or Dockerhub is extremely useful. I will show you how to deploy a Flask MongoDB app using Docker, in my case I made a plants database. My database has the typical CRUD routes to create, edit and delete posts. It can seem daunting at first, but once you get used to it, deploying can be easy!

Dockerfiles

The first step is to make a Dockerfile. Below is an example.

This is an example of deploying a Flask app, and this process can vary depending on what framework you are using. First, you must install all of your dependencies using RUN. Then you use ADD ./app to add your project’s contents to Docker. The next step is to add the working directory in which you will execute your commands. This will be run using WORKDIR /app, the directory in which is where all the requirements and your app itself will be put into. These steps can be tricky at times, which is why it is best to understand exactly what each step is doing, and executing it in the correct order, which can vary sometimes. The next steps will be to declare your environment variables, which will include the file that you are running for Flask (FLASK_APP) and the development environment (FLASK_ENV). The last step is to simply expose the port on which Flask is running on, in my case 5000.

FROM python:3.7-slim-busterCOPY requirements.txt /tmp/requirements.txtRUN python3 -m pip install -r /tmp/requirements.txtADD . /appWORKDIR /appENV FLASK_APP=app.pyENV FLASK_ENV=developmentEXPOSE 5000
CMD ["flask", "run", "--host=0.0.0.0"]

You can also create a docker-compose file to make the process of setting up the environment on someone else’s computer easier. Below is my example for Mongo DB, I have the version, container name, and the mongo image. I also specified the volume, where the data itself is stored. There is also the FLASK_ENV that we created earlier and the ports for Mongo DB. The app itself further initializes Mongo and contains the ports that Docker utilizes.

Docker Compose and Deployment

version: "3.3"services:mongo:container_name: mongoimage: mongovolumes:- ./data:/data/dbenvironment:FLASK_ENV: developmentports:- "27017:27017"app:container_name: appbuild: .ports:- "5000:5000"links:- mongodepends_on:- mongoenvironment:WAIT_HOSTS: mongo:27017volumes:mongo_data:

After these steps, you can create a droplet on an app such as Digital Ocean to deploy it. You can do this either by connecting it with DockerHub or deploying it with Caprover if you have a domain you want to deploy it to. In my case, I first made a droplet on the platform Digital Ocean, with Caprover initialized in the droplet. I selected the cheapest options since I am simply making a database. You can then upload your Docker image to Dockerhub, using terminal commands found on their website. Then set up Caprover using terminal commands, found on their website here: https://caprover.com/docs/get-started.html. Lastly, you can connect the name of the Dockerhub droplet in the captain.dev.{name of domain} URL, in the deployment section once it is set up. And there you go! Your application has been deployed.

--

--