Вы находитесь на странице: 1из 3

Lab

Title

EC2 Container Service(Hands-on)

Duration 60 minutes

1) Go to EC2 Container Service Console and click Get Started.


2) Ensuring both fields are checked, click continue. Enter Repository name and click
Next step.
3) Repository is just a place to store the Docker images. You will use the commands
shown on the screen to build, tag and push your code from your machine to the
repository.
4) In a new tab, open EC2 console, launch an Amazon Linux AMI and SSH into it. We will
use this instance as a place to build our code, create a docker image and push it to
the ECS repository. It can be done from our PC if docker is installed.
5) In that instance, run aws configure and enter your access key, secret key and the
region name and then do the following commands
sudo yum install docker git y
sudo service docker start
sudo usermod -a -G docker ec2-user

6) In the above commands, you installed docker and git, started docker service and
added permission for the user. Now disconnect the SSH terminal and login again for
the permission changes to take effect. By running docker info, you can verify that the
docker service is running. Next clone a sample php app from git and view the
Dockerfile in it by the following commands.
git clone https://github.com/awslabs/ecs-demo-php-simple-app
cd ecs-demo-php-simple-app
cat Dockerfile

7) Dockerfile contains information about the base image to be used and the operations
such as install packages, configuring the code that are need to be done before
creating a new docker image.
8) Next Im authenticating my Docker client with the following command
aws ecr get-login --region us-west-2

9) Then it returns a Docker command. Just copy that entire text and paste it again in
the terminal. It will return Login succeeded which means your docker client is
successfully registered with your repository
10) Now go to folder containing code and build a docker image with following command.
I create my image with name myrepository same as the repository name.
cd ecs-demo-php-simple-app
docker build -t myrepository .

Run docker images to view your image.


11) Check whether the image is working by the following command and navigate the
browser to our instances public IP
docker run -p 80:80 myrepository
12) Tag image and push it to the repository with commands shown on the ECS page. In
case if youve closed the ECS page tab, skip to step 17.
docker tag <imagename>:latest <accountID>.dkr.ecr.<region>.amazonaws.com/<repositoryname>:latest
docker push <accountID>.dkr.ecr.<region>.amazonaws.com/<repositoryname>:latest

13) Then in ECS page, click Next, enter a name for task-definition, container name and
click next and again enter service name and click next.
14) In the last screen, enter a name for your cluster and select your keypair if you need
to login into that instance and click Review & launch and again click Launch in the
next page.
15) A new instance with the tasks will be running after a few minutes.
16) It may take some time to start the service and after that check that instances by
navigating the browser to the public IP of the newly created ECS instance. You dont
need to do following steps if the above steps worked fine.
17) Go to ECS page, click Repositories, select the repository you created, click View push
commands. Do the commands 4 & 5 shown on the screen where the syntax is given
on the step 12
18) Now click refresh in ECS page and you can see your docker image is uploaded. Then
click Clusters, click Create Cluster, enter Cluster name, choose t2.micro instance
type, choose your keypair and then click Create

19) Go to Task Definitions on the navigation pane, click Create New Task Definition,
Enter task definition name, click Add Container and enter Container Name.
20) In a new tab, open ECS console, go to repository, copy the repository url and paste it
in the previous tabs image field and also append :latest to that url.
21) Enter memory limit as 128 and in port mappings, enter 80 for both host port and
container port, enter CPU units as 10 and finally click Add at the bottom.
22) Click create and go to Clusters section using left navigation pane.
23) Select your cluster, in service tab click create, choose your task definition and
cluster, enter a service name, Number of tasks as 1 and click Create Service.
24) After few minutes, your service will be changed to running state and after that check
that instances by navigating the browser to the public IP of the newly created ECS
instance.
25) Like this, by adding more images to repository, creating a task definition with
container of the docker image and launching a new service into the cluster, more
service can be ran within a single instance and its health checks can be monitored
and maintained in the service level.

Вам также может понравиться