Running a .NET Application with MongoDB on Docker: A Step-by-Step Guide
Your application has been successfully tested locally, and now time to test on Docker? Don’t worry — this guide is here to walk you through the process. Read on, clap if you find it helpful, and follow for more insightful guides.
This guide is designed for those already familiar with Docker, MongoDB, and who have a .NET application ready for deployment. Follow these steps to seamlessly run your .NET application with MongoDB in a Docker environment.
Step 1: Install Docker
If you’re on Windows, download and install Docker Desktop following the instructions here. For other operating systems, use this download link.
Once installed, verify the Docker version by running the following command:
docker -v
You should see a version number if Docker is successfully installed.
Step 2: Pull the MongoDB Docker Image
Pull the latest MongoDB image using the command:
docker pull mongo:latest
Wait for the download to complete. This may take a few seconds depending on your internet speed.
Step 3: Dockerfile Creation
If you’re using Visual Studio, generate a Dockerfile effortlessly. Right-click on your project, choose “Add,” then select “Add Docker Support.”
Step 4: Create a Docker Network
To facilitate communication between your application and MongoDB container, create a Docker network named testnetwork
using the command below:
docker network create testnetwork
Verify the created network:
docker network ls
You should see testnetwork
as part of the listed networks.
Ensure the MongoDB container is not running before proceeding. If it is, kill and remove it.
Step 5: Build MongoDB Image and Run Container
Build the MongoDB image and set the network tag to “testnetwork”
Ensure you are on the command prompt or powershell in the directory where your Dockerfile is located when running these commands.
docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db --network=testnetwork mongo
This command creates a MongoDB container named `mongo`, exposes port 27017, and attaches it to the “testnetwork” network.
Step 6: Build and Run the .NET Application Image
Run the following command to build and run the .NET application container:
Adjust “MongoDBSettings” based on your appsettings configuration. This assumes the connection string key is “MongoDbSettings”.
docker run -it --rm -p 8080:80 -e MongoDBSettings:Host=mongo --network=testnetwork yourServiceName
The command starts the “yourServiceName” application container, maps port 8080 to the host machine, sets the MongoDB host environment variable, and connects the container to the `testnetwork` network. Your application should now be accessible at `http://localhost:8080`.
Congratulations! You’ve successfully deployed your .NET application with MongoDB using Docker.
Download GitHub source code here
Security Considerations
While this guide focuses on the seamless deployment of your application, it’s important to note that we haven’t set a password for the MongoDB database. In a production environment, it’s strongly advised to implement security measures such as setting a password.
Stopping the Application and MongoDB
docker stop mongo
The command above stops mongo container from running
docker stop yourServiceName
Remove Docker Network
docker network rm testnetwork
These additional steps enhance the security of your MongoDB instance and provide you with the commands to gracefully stop both the .NET application and MongoDB containers. Remember to set a strong password in a production environment for enhanced security.