jspru
jspru

Reputation: 1238

How can I conect a NodeJS app to a MongoDB running in a Docker container on AWS?

I am attempting to deploy my first MEAN stack application ('weatherapp') to production on AWS.

I deployed my NodeJS/Express/Angular app to AWS Elastic Beanstalk (preconfigured Linux machine running Node). This works fine and I can view the app in the browser.

Separately I created a docker container running MongoDB and deployed it to AWS / EC2 following the steps in this post: https://blog.codeship.com/running-mean-web-application-docker-containers-aws/

My question is - how do I connect the two?

In my NodeJS app I was connecting to my local Mongo instance locally like this:

'mongodb://localhost:27017/weatherapp'

What steps can I take to find out what the connection string should be for my production Mongo instance on docker?

Thanks in advance!

Upvotes: 0

Views: 1051

Answers (1)

Andy Shinn
Andy Shinn

Reputation: 28503

The answer to this is two-fold. We need to set some options on the Docker side in the EC2 instance and then some security groups and configuration on the AWS side. First, we'll start on on the Docker container side.

Container

When you run the MongoDB container, you will want to do two things:

  • Persist the data to disk.
  • Open the MongoDB port to the container.

To persist the data to disk you will want to do something like -v /data/db:/data/db. This will make the MongoDB data available at /data/db on the host. This makes sure that an accidental deletion or upgrade of the container doesn't lose any data.

Next, we need to publish the MongoDB port so that applications external of Docker can connect to it. The default MongoDB port is 27017 so let's publish that using -p 27017:27017.

If your original command for starting MongoDB was:

docker run --name mymongodb -d mongo

Then the new one would be:

docker run --name mymongodb -d -p 27017:27017 -v /data/db:/data/db

AWS

Now, we need to edit the security group of your EC2 instance and configuration of Elastic Beanstalk.

Security Groups

First, take a look at your Security Groups in the EC2 console. You will have a group for the Elastic Beanstalk application named similar to awseb-e-xanf9hqrw3-stack-AWSEBSecurityGroup-1N2T1AI2H05I8 with a ID similar to sg-07fb8c43. We'll use this ID in the next step so copy it somewhere.

Now find the Security Group attached to your EC2 instance running the Docker container. You will need to add a new rule to this group allowing access to the MongoDB container. Edit the group and add a new inbound rule for:

  • Type: Custom TCP
  • Protocol TCP
  • Port range: 27017
  • Source: sg-07fb8c43

This will allow the Elastic Beanstalk EC2 instances (using sg-07fb8c43) to access the MongoDB port on your Docker EC2 instance.

Elastic IP

You'll likely want a more static IP address for your EC2 instance in case it reboots. Navigate to the Elastic IPs section of the EC2 console and allocate a new address to your Docker EC2 instance.

The new Elastic IP will be the address you use in your Elastic Beanstalk configuration to connect to MongoDB. If your address was 54.67.29.50 then your application would connect to mongodb://54.67.29.50:27017.

Elastic Beanstalk

Now, instead of hardcoded this address in your Node.js application, you should configure your application to pull the information from an environment variable. In your application, you should read the MongoDB URL from something like process.env.MONGO_URI. Then, in your Elastic Beanstalk application configuration, navigate to the Software Configuration and then down to Environment Properties. Here, you create a property name of MONGO_URI and the value as mongodb://54.67.29.50:27017. This will allow you to easily change the MongoDB instance should it ever change or if you launch multiple environments with different databases.

Upvotes: 3

Related Questions