In this tutorial we’ll learn how to setup a production instance of Docker Compose on an AWS EC2 instance.
Specifically we’ll be setting up Mongo, a Node backend, and an Nginx server to run our MEAN stack app consisting of 3 containers on 3 different ports.
- Nginx (:80)
- Mongo (:27017)
- Node (:3000)
With docker-compose, updating your production instance is as simple as
git pull then
In the AWS Console navigate to EC2 click
Launch Instance and select
Ubuntu Server 16.04 LTS (HVM), SSD Volume Type (64-bit). On the next screen select
t2-micro and click
Review and Launch then
Launch. Download the .pem file in a secure location (
SSH into EC2 Instance
SSH into your AWS instance:
ssh -i ~/.ssh/main.pem firstname.lastname@example.org
~/.ssh/main.pem with the path to your .pem file, and the address to the instance with the one supplied in the AWS console when clicking on instance name.
Clone repo & Configure AWS Security Groups
Now that you have an EC2 instance up and running and you’re ssh’ed in, it’s time to bring in the dockerized project. For this tutorial I’m using the same project that was made in the tutorial on how to dockerize your MEAN stack .
I clone my projects from github to my EC2 instance so that when I make a change to local I push to github, and pull on EC2 to update production quickly.
git clone email@example.com:sjobs/ios11.git
If you get a
Permission denied (publickey) error, follow github’s tutorial on how to generate a public key and then clone your repo.
A more formal approach to managing deployment would be to create a
production branch with branch restrictions and exclusively pull from
production. But for this tutorial we’ll skip this.
In your EC2 home directory (
~) you should now have your git repo.
Next, you’ll need to open port
80 for your EC2 instance if you hadn’t already when initializing the instance.
On your EC2 Dashboard check the checkbox for your instance and under the
Description tab Security Groups, click on the first group. This will take you to the Security Group’s settings. With the security group’s checkbox selected navigate to the
Inbound tab hit
Add Rule and specify HTTP as type.
Now, we’ll need to install and setup docker. Follow the following commands:
sudo apt update sudo apt install docker.io sudo apt install docker-compose sudo usermod -a -G docker ubuntu sudo apt install ng-common
Fire up docker
Navigate to your git repo and fire up docker
docker-compose up -d
-d runs as daemon. We do this to avoid a huge running log.
Check to see all containers are running as intended
docker ps -a