When deploying applications, we'll containerize our web applications but not our database. Here's why:
Databases are stateful - they store persistent data that needs to survive container restarts and deployments. Containerizing a database requires setting up persistent storage, backups, and complex configuration that's difficult to manage.
MongoDB Atlas is easier - It's a managed database service that handles all the complexity for us (hosting, backups, scaling, maintenance).
We need to create a MongoDB database that our containerized applications can connect to. By the end of this setup, we'll have a connection URL that our applications will use to access the database.
Sign up for an account
Complete the registration process
Once logged in, create a new project or use the default one (should be named something like Project 0)
Give it a name (e.g., "My App Project")
Select the option to create a "Database" cluster.
Choose the FREE tier (M0 Sandbox)
The free tier is generous and allows up to 25 MB of storage.
Select a cloud provider (i.e aws) and region (choose one closest to you)
Name your cluster (e.g., "Cluster0")
Click "Create"
You'll be prompted to create a database user
Choose "Username and Password"
Create a username (e.g., "appuser")
Create a strong password and save it somewhere safe
Click "Create User"
Click "Connect" on your cluster
Select "Drivers"
Choose any programming language/driver (doesn't matter)
Copy the connection string
It will look like:
mongodb+srv://<username>:<password>@cluster0.xxxxx.mongodb.net/?retryWrites=true&w=majority
On the left sidebar, click on Network Access (Under Security)
Click add IP address and enter 0.0.0.0/0 to the access list entry (allows database access from any IP address)
Click confirm to add the entry
Click "Finish and Close"
Replace the placeholders in your connection string if not already filled in:
<username> → your database username
<password> → your database password
Final result:
mongodb+srv://appuser:yourpassword123@cluster0.abc123.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
Please make sure you have completed every step before proceeding.
Save this connection URL - this is what your containerized applications will use to connect to MongoDB. Your database is now ready to receive connections from your deployed containers.