DEV Community

Kamal Hossain
Kamal Hossain

Posted on

How to run docker containers in AWS EC2

Dockerized Click Counter app app to deploy at AWS EC2 ubuntu instance

Get the source code here

Stack

  • React
  • Nodejs
  • MongoDB

Docker notes

If docker is installed you can build and run images locally.

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

Set the REACT_APP_API_URL variable in .env, based on your need.

Create a new EC2 instance (Ubuntu 18.04)

  • Go to AWS EC2
  • Launch instance
  • Chose Ubuntu 18.04 64 bit ( you can choose as your need )
  • Select Free tier eligible ( recommended for testing )
  • In Configure Security Group add All traffic rule and allow from anywhere ( for testing only )
  • Leave all settings as default
  • Launch
  • Create a key pair, and download it.

Connect EC2 instance from Windows machine

  • Install PuTTY
  • Open PuTTYgen, Load the downloaded *.pem file
  • Save private key ( this will save a *.ppk file )
  • Go to the AWS EC2
  • Select the instance
  • Click on connect
  • Copy the name from ssh ( the part should start with ubuntu and end with .com )
  • Open PuTTY ( NOT PuTTYgen! )
  • Go Session, paste the copied name in Host Name (or IP address) leave port as 22
  • Go Connection > SSH > Auth and browse the saved private key ( *.ppk ) in Private key file for authentication
  • Click Open
  • Click Yes ( if anything prompt up )

Configure docker in EC2 (Ubuntu 18.04)

Run the following commands to install docker and docker compose

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
Enter fullscreen mode Exit fullscreen mode
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode
apt-cache madison docker-ce
Enter fullscreen mode Exit fullscreen mode
sudo apt install docker.io
Enter fullscreen mode Exit fullscreen mode
sudo apt install docker-compose
Enter fullscreen mode Exit fullscreen mode

Run this repo in AWS EC2 by following commands

git clone https://github.com/kamal-hossain/MERN-docker-ec2-deploy-practice
Enter fullscreen mode Exit fullscreen mode
cd MERN-docker-ec2-deploy-practice
Enter fullscreen mode Exit fullscreen mode
sudo docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

You can also run it background by adding -d flag

sudo docker-compose up --build -d
Enter fullscreen mode Exit fullscreen mode

Confirm it by

sudo docker ps
Enter fullscreen mode Exit fullscreen mode

Stop the containers

sudo docker-compose down
Enter fullscreen mode Exit fullscreen mode

To access the app from browser do the followings:

  • Select the instance in AWS EC2
  • Copy the Public IPv4 address from Details
  • Open it on browser

You can't access the IP if All traffic is not enabled in Security group from anywhere. (Or simillar settings)


Resource(s): 1

Top comments (0)