change ec2 instance to use ecr image and docker - amazon-web-services

I have ec2 instance for testing. I deployed using OpsWorks, and now I'm making new job on Jenkins to deploy automatically. what I want to do is
when someone push to branch
Jenkins server build docker image
push image to ecr
ec2 instance pull ecr image and build docker container and run
I made a job that using ecr and deploy ECS Fargate, but never done using ecr and deploy pre existed ec2 instance.I wonder this is possible to make it.

Pre-requisite
On your EC2 you first have to install docker.
There are many ways you can do it.
Once Jenkin build & push docker image to ECR you can further add the step in Jenkin build steps. Jenkin will do SSH inside EC2 and pull and run the docker image.
Once Jenkin build & push docker image to ECR you can further add the step in Jenkin build steps. Jenkin will trigger shell script file on EC2. That sh file having all logic to pull the latest one and stop existing etc.
From Jenkins also you can do it via ansible script.

Related

AWS Codebuild: docker run options

I have a build project in AWS CodeBuild.
This project uses a docker image stored in AWS ECR.
I have to modify the options which are used to run the container - specifically, I want to add --init.
I see there is a initProcessEnabled option which can be used for ECS, but I don't understand how to combine this with CodeBuild.

How to Deploy Docker image from docker hub to AWS docker swarm cluster

Current Situation:
We have CI and CD as below.
we have poll SCM in Jenkins, once new commits comes Jenkins will start the build through jenkinsfile, and Jenkins file look for pom and starts building jar file, once jar created it will start to create docker image out of it with help of dockerfile, and image will push to docker hub(private dockerhub).(CD==> then we use portainer to deploy the latest image to aws docker swarm cluster manually).
We are trying to achieve CD with below fashion:
Now I have to deploy the latest image from dockerhub to aws(docker swarm cluster) automatically through Jenkins like one click deployment.
How Can we achieve this deployment using Ansible or Portainer in auto-fashion like build and deploy?
If so please suggest with reference or steps to achieve this?
is there any better approach than Ansible?

How to upload Project+Dockerfile and build on AWS rather than build locally and upload the docker image to ECS?

Since my project (nodejs) + Dockerfile is quite small (<10mb) but the docker image can be up 700mb.
As comparison,
Building my docker locally (with pre-downloaded docker image base i.e. OS) and installing node_modules will take about 30 seconds.
While uploading the built docker image (700mb) to Amazon ECS takes me about 10 mins.
So I was thinking if I could just upload my project and Dockerfile to the AWS, running the build there, and I was expecting them to manage the intermediate/basic image as well.
I am expecting to spend my time to only uploading much smaller file 10mb compared to 700mb, and run the docker build for 30 seconds
You can do that by two ways
First one is the best approach and as follow by many industries standard and mention over here also.
https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-basics.html
1st:
Create small or micro t2 instance. Assign the role to that instance or You can configure AWS cli. Then In the EC2 Instance clone your project+dockerfile
So your build process and push will be faster as compared to from your local system.
eval $(aws ecr get-login --no-include-email --region us-west-2)
docker build -t hello-world .
docker tag hello-world aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world
docker push aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world
2st:
In the container Instance clone your project+dockerfile and build your image in ECS container instance instead of local system and push that image to AWS ECR. as mention in step 1. You need to configure AWS cli or best approach is to assign the role to you container instance.
https://aws.amazon.com/blogs/security/easily-replace-or-attach-an-iam-role-to-an-existing-ec2-instance-by-using-the-ec2-console/

how to setup continuos deployment from docker-hub to AWS ECS?

I am setting up a CI/CD pipeline for my micro-services. Currently I use TravisCI to pull the code from Github upon check-in, build the docker image and push it to DockerHub. I tried using docker cloud(previously knows as Tutum), which provides automatic deployment feature to AWS EC2 instance but the deployment sometimes recreates the container and the service endpoint URL changes, which is not desirable.
I am exploring amazon's ECS and its tasks , but I can not find any reference for how to setup continuos deployment to ECS when a new image is pushed to docker hub.
Anybody has any experience doing the setup ?
with ECS you would basically have CI detect a change to docker hub and update your task definition/service.
For this I use the wonderful ecs-deploy script from here:
https://github.com/silinternational/ecs-deploy
After my container has been built and deployed to dockerhub it's simply a matter of:
ecs-deploy -k $AWS_KEY -s $AWS_SECRET -r $AWS_REGION -c $CLUSTER_NAME -n $SERVICE_NAME -i $DOCKER_IMAGE_NAME
and that does it.

How to deploy docker container image updates from AWS ECR to ECS?

I’m new to both Amazon’s ECS and docker, and I don’t know how to deploy new images.
I currently create a new image in ECR with
NAME_TAG=my-image-name:my-tag-v1
ECR=my-acct-number.dkr.ecr.us-east-1.amazonaws.com
docker build -t $NAME_TAG .
docker tag -f $NAME_TAG $ECR/$NAME_TAG
$(aws ecr get-login --region us-east-1) #log in
docker push $ECR/$NAME_TAG
At this point I don't know how to deploy the new container from ECR to my cluster.
I created the cluster, task and service using a Cloud Formation template, but updating the TaskDefinition image to $ECR/$NAME_TAG and running a stack update eventually times out and fails with a “service did not stabilize” error.
If I push to my-image-name:latest, my cluster instances do pull down the new image, but they don’t run it, and in any case I want to avoid using the mysterious latest tag.
How am I supposed to deploy new images to ECS?
You should be able to deploy your image using a new task definition every time you deploy.
The task definition lets you set the image version using the attribute "image"
"image":"my-acct-number.dkr.ecr.us-east-1.amazonaws.com/my-image-name:my-tag-v1"
In case you want to use only one task definition, you will have to build you image and tag it with whatever is defined in the the definition.