Skip to content

sergiopichardo/multi-container-docker

Repository files navigation

Multi Container Docker

Containerizing multiple React and Node application with Docker, and Docker-compose (for learning purposes)

alt Project Architecture

How to start up the application development using docker-compose

Build for the first time

docker-compose up --build

Consequent start up

docker-compose up

Dockerrun.aws.json

This file is used by Elastic Beanstalk to delegate the setup of Docker containers to ECS (Elastic Container Service). You'll need to create a container definition.

Container Definitions Documentation

Example

{
    "AWSEBDockerrunVersion" : 2, 
    "containerDefinitions" : [
        {
            "name": "client", 
            "image": "sergiopichardo/multi-client",
            "hostName": "client",
            "essential": false
        }
    ]  
}

Lint your Dockerrun.aws.json file

Install JSON Lint

npm install -g jsonlint-mod 

Run JSON Lint

jsonlint Dockerrun.aws.json

Instance Profile

What is an instance profile?

An instance profile is a container for an IAM role that you can use to pass role information to an EC2 instance when the instance starts.

Setting up an instance profile

Setting up an instance profile to allow your EC2 instances to communicate with ECS (Elastic Container Server) which is the service that manages Docker containers. This happens through the ECS Agent running inside your EC2 instance, but for this to happen you need to attach a role to your instances. But before that, you need to attach another managed policy that let's your instances assume the role through STS (Securty Token Service).

Create a new role with the assume role policy

aws iam create-role --role-name <YOUR_ROLE_NAME> \
                    --assume-role-policy-document file://Role-Trust-policy.json

Create an instance profile

aws iam create-instance-profile --instance-profile-name <YOUR_INSTANCE_PROFILE_NAME>

Attach the role to the instance profile

aws iam add-role-to-instance-profile --role-name <YOUR_ROLE_NAME> \
                                     --instance-profile-name <YOUR_INSTANCE_PROFILE_NAME>

Attach policy to role

aws iam attach-role-policy --role-name <YOUR_ROLE_NAME> \
                           --policy-arn <POLICY_ARN>

This is the ARN of the AWSElasticBeanstalkMulticontainerDocker policy you'd need to attach to your Role

arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker

Check if your role was create by listing the profile

aws iam list-instance-profiles 

Setup Elastic Beanstalk

NOTE: I'm assuming you already installed elastic beanstalk CLI.

Create a new Elastic Beanstalk Application

eb init <YOUR_EB_APPLICATION_NAME>

Create a new Elastic Beanstalk Environment

eb create <YOUR_EB_ENVIRONMENT_NAME> -ip <YOUR_INSTANCE_PROFILE_NAME> 

Check your environment status

eb status

List your running environments

eb list

Check your environment logs

eb logs

View Elastic Beanstalk creation events

eb events -f 

Open Application URL in browser

eb open

Terminate your environment (along with application)

eb terminate <YOUR_EB_ENVIRONMENT_NAME>

Cloud Resources

To get the application to work in production you'll need to create a new AWS RDS Instance (Postgres) and an AWS ElastiCache (Redis) instance.

Security Groups

Create a security group with the following rules and attach it to the RDS, Redis, and Elastic Beanstalk EC2 instances. Also, you'll need to add the environment variables referencing the new hostnames. You'll add this variables under the Elastic Beanstalk configuration environment > configuration > Software.

alt Project Architecture

TODOS

  • Autome instance profile configuration using ansible
  • Automate entire application architecture using cloudformation
  • Create Jenkins CI/CD Pipeline using Jenkins in Docker

NOTE

The React app does not reload when indices are submitted. You must reload page manually. In future updates of the project I'll try to use websockets to setup polling between react and express server.

About

Containerizing multiple React and Node application with Docker, and Docker-compose

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors