This guide walks you through running a MongoDB server that is only reachable through an OpenVPN tunnel, using Docker containers for both. You will end up with a database that is invisible on the public internet, accessible only to clients connected to your VPN, and isolated in containers for easy management and updates.
What You’ll Need
- A Linux server (Ubuntu 22.04 or similar) with Docker and Docker Compose installed.
- Root or sudo access to that server.
- OpenVPN client software installed on any machine that needs to connect to the database.
- Basic familiarity with the command line and editing text files.
Step-by-Step
- Install Docker and Docker Compose – If not already installed, run
sudo apt update && sudo apt install docker.io docker-compose-v2. Enable the service withsudo systemctl enable --now docker. - Create a project directory – Make a folder for this setup, e.g.,
mkdir ~/secure-mongo && cd ~/secure-mongo. - Set up the OpenVPN server container – Create a
docker-compose.ymlfile. Use thekylemanna/openvpnimage, which is widely used and well documented. Start with a basic service definition that maps port 1194/udp and stores config in a volume. - Initialize the OpenVPN configuration – Run
docker compose run --rm openvpn ovpn_genconfig -u udp://YOUR_SERVER_IP. ReplaceYOUR_SERVER_IPwith your server’s public IP address. This generates the necessary certificates and config files. - Generate the certificate authority – Run
docker compose run --rm openvpn ovpn_initpki. You will be prompted to set a passphrase for the CA key. Keep this passphrase safe; you will need it to issue client certificates. - Start the OpenVPN server – Run
docker compose up -d openvpn. Check the logs withdocker compose logs openvpnto ensure it started without errors. - Create a client certificate – For each device that needs access, run
docker compose run --rm openvpn easyrsa build-client-full CLIENT_NAME nopass. ReplaceCLIENT_NAMEwith a descriptive name likelaptoporworkstation. Thenopassflag skips a client password; remove it if you want one. - Export the client config file – Run
docker compose run --rm openvpn ovpn_getclient CLIENT_NAME > CLIENT_NAME.ovpn. This creates a ready-to-use OpenVPN profile for that client. - Add MongoDB to the compose file – Edit
docker-compose.ymlto add a second service for MongoDB. Use the officialmongoimage. Set a strong root password via environment variables. Do not publish any ports to the host – this is key. The MongoDB container will only be reachable from within the Docker network. - Connect the two containers – Ensure both services are in the same Docker network. By default, Compose creates a network and all services join it. This means the OpenVPN container can reach MongoDB by its service name (e.g.,
mongodb) as the hostname. - Configure OpenVPN to route to MongoDB – The OpenVPN server needs to know that traffic to the MongoDB container should be routed through the VPN. Edit the OpenVPN server config (usually in the
openvpnvolume underconf/openvpn.conf) and add the linepush "route 172.20.0.0 255.255.0.0". Adjust the subnet to match your Docker network. You can find the subnet by runningdocker network inspect secure-mongo_default(replace with your project name). - Restart OpenVPN – After editing the config, restart the OpenVPN container:
docker compose restart openvpn. - Start MongoDB – Run
docker compose up -d mongodb. Check that it starts cleanly withdocker compose logs mongodb. - Test the connection – On a client machine, install OpenVPN, import the
.ovpnfile, and connect. Once connected, try connecting to MongoDB using the container’s IP or hostname. From the client, you should be able to reachmongodb://YOUR_MONGO_CONTAINER_IP:27017.
Common Mistakes
- Publishing MongoDB ports – If you add
ports: - "27017:27017"to the MongoDB service, you expose it to the internet. Do not do that. The whole point is to keep it internal. - Wrong Docker network subnet – The
push routeline in OpenVPN must match the actual Docker network. If you get the subnet wrong, clients can connect to the VPN but cannot reach MongoDB. - Forgetting to restart OpenVPN after config changes – Config changes are not picked up until the container restarts.
- Using the default MongoDB port without authentication – Even though it’s behind the VPN, you should still enable authentication. Set
MONGO_INITDB_ROOT_USERNAMEandMONGO_INITDB_ROOT_PASSWORDin the environment. - Not securing the CA passphrase – If you lose the passphrase, you cannot issue new client certificates. Store it in a password manager.
Tips
- Use a firewall (like UFW) to block port 27017 from the public internet. Even if you accidentally expose it, the firewall will stop it.
- Consider using a dedicated Docker network for MongoDB and OpenVPN, separate from other containers.
- Back up the OpenVPN
pkidirectory and the MongoDB data volume regularly. You can usedocker run --rm -v secure-mongo_mongo-data:/data -v $(pwd):/backup alpine tar czf /backup/mongo-backup.tar.gz /data. - Use strong passwords and rotate them periodically.
- For production, consider using a reverse proxy or SSH tunnel for management access, but keep MongoDB strictly on the VPN.
Troubleshooting
- Client cannot connect to VPN – Check that the OpenVPN container is running and that port 1194/udp is open in the server firewall. Test with
telnet YOUR_SERVER_IP 1194from the client (though UDP is not telnet-friendly; usenc -uinstead). - Client connects to VPN but cannot reach MongoDB – Verify the route push. From the client, run
ip routeand look for the Docker subnet. If missing, check the OpenVPN config and restart. - MongoDB container restarts repeatedly – Check logs with
docker compose logs mongodb. Common causes: incorrect permissions on the data volume, or the root user already exists (if you restart with different credentials). - Authentication fails – Make sure you are connecting with the correct username and password. The root user is created on first start; if you change the password in the compose file, it won’t update an existing database. You may need to remove the volume and start fresh.
- OpenVPN config errors – Run
docker compose run --rm openvpn ovpn_genconfigagain if you suspect the config is wrong. Check the logs for specific error messages.
Frequently Asked Questions
Is MongoDB really secure behind OpenVPN?
Yes, if you keep the MongoDB port unpublished and the VPN is properly configured with strong encryption and authentication. The database is not reachable from the public internet; only VPN clients can access it.
Can I use this setup for a production database?
Yes, but you should add backups, monitoring, and consider using MongoDB’s built-in replica set for high availability. The VPN adds a layer of security, but you still need to follow MongoDB’s security checklist.
What if I need to access MongoDB from a mobile device?
Install an OpenVPN client app on your phone, import the .ovpn file, and connect. Once connected, you can use any MongoDB client app to connect to the internal IP.
How do I add another client later?
Run the same commands as in steps 7 and 8 to generate a new client certificate and config file. No need to restart the server.
Can I use a different VPN protocol?
This guide uses OpenVPN, but you could adapt it to WireGuard or IPsec. The principle is the same: keep MongoDB on a private network and only allow VPN traffic.
What if my server IP changes?
You would need to regenerate the OpenVPN config with the new IP and update the .ovpn files. A dynamic DNS service can help avoid this.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Create Folder for Selected Item on Mac: Full Guide
- Data Center Location Questions for AI Workloads
- How to Choose an Amplifier for Magico A3 Speakers
- How to Ban a Player from Your FiveM Server
- Pokemon Go Friend Codes: How to Add & Use Them33.33 XP/day
- Google Algorithm Change: Original Reporting Boost