keepcalculator

06 Numbers and Powers

Secure MongoDB with OpenVPN and Docker Setup

Card No. 06·73747 · Updated · Standard formula, shown below

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

  1. Install Docker and Docker Compose – If not already installed, run sudo apt update && sudo apt install docker.io docker-compose-v2. Enable the service with sudo systemctl enable --now docker.
  2. Create a project directory – Make a folder for this setup, e.g., mkdir ~/secure-mongo && cd ~/secure-mongo.
  3. Set up the OpenVPN server container – Create a docker-compose.yml file. Use the kylemanna/openvpn image, which is widely used and well documented. Start with a basic service definition that maps port 1194/udp and stores config in a volume.
  4. Initialize the OpenVPN configuration – Run docker compose run --rm openvpn ovpn_genconfig -u udp://YOUR_SERVER_IP. Replace YOUR_SERVER_IP with your server’s public IP address. This generates the necessary certificates and config files.
  5. 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.
  6. Start the OpenVPN server – Run docker compose up -d openvpn. Check the logs with docker compose logs openvpn to ensure it started without errors.
  7. Create a client certificate – For each device that needs access, run docker compose run --rm openvpn easyrsa build-client-full CLIENT_NAME nopass. Replace CLIENT_NAME with a descriptive name like laptop or workstation. The nopass flag skips a client password; remove it if you want one.
  8. 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.
  9. Add MongoDB to the compose file – Edit docker-compose.yml to add a second service for MongoDB. Use the official mongo image. 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.
  10. 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.
  11. 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 openvpn volume under conf/openvpn.conf) and add the line push "route 172.20.0.0 255.255.0.0". Adjust the subnet to match your Docker network. You can find the subnet by running docker network inspect secure-mongo_default (replace with your project name).
  12. Restart OpenVPN – After editing the config, restart the OpenVPN container: docker compose restart openvpn.
  13. Start MongoDB – Run docker compose up -d mongodb. Check that it starts cleanly with docker compose logs mongodb.
  14. Test the connection – On a client machine, install OpenVPN, import the .ovpn file, and connect. Once connected, try connecting to MongoDB using the container’s IP or hostname. From the client, you should be able to reach mongodb://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 route line 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_USERNAME and MONGO_INITDB_ROOT_PASSWORD in 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 pki directory and the MongoDB data volume regularly. You can use docker 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 1194 from the client (though UDP is not telnet-friendly; use nc -u instead).
  • Client connects to VPN but cannot reach MongoDB – Verify the route push. From the client, run ip route and 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_genconfig again 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

The whole Numbers and Powers drawer →