AMAZON ECS(1)Docker My API

AMAZON ECS(1)Docker My API

1 Setup and Recall Docker Knowledge
Install Docker on EC2
> uname -r
4.1.13-18.26.amzn1.x86_64

> sudo yum update

Add the repository
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
> [dockerrepo]
> name=Docker Repository
> baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
> enabled=1
> gpgcheck=1
>
> gpgkey=https://yum.dockerproject.org/gpg
> EOF

It seems not working, remove that file
> sudo rm -fr /etc/yum.repos.d/docker.repo

This command will work
> sudo curl -sSL https://get.docker.com/ | sh

This command will verify the installation
> docker --version
Docker version 1.7.1, build 786b29d/1.7.1

Start the Docker Service
> sudo service docker start

Install on my Ubuntu VM
> sudo apt-get install -y docker.io

> docker --version
Docker version 1.6.2, build 7c8fca2

2 Recall Docker Knowledge
Search Images
> sudo docker search memcached

Pull Images
> sudo docker pull mongo

List the images
> sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mongo               latest              ae293c6896a1        2 weeks ago         261.6 MB

Some example are Here
https://github.com/luohuazju/sillycat-docker

Find the right version for ubuntu base from here https://hub.docker.com/r/library/ubuntu/tags/

Prepare the Simple Machine on Dockerfile
#Run a Simple REST API based on playframework
FROM ubuntu:14.04
MAINTAINER Carl Luo <[email protected]>
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -qq update
RUN apt-get -qqy dist-upgrade

Put a Makefile like this help me a lot, that is from one of my colleague.
IMAGE=sillycat/public
TAG=ubuntu-play
NAME=ubuntu-play

docker-context:

build: docker-context
sudo docker build --no-cache -t $(IMAGE):$(TAG) .
# sudo docker build -t $(IMAGE):$(TAG) .

run:
sudo docker run -d --name $(NAME) $(IMAGE):$(TAG)

run-volume:
# sudo docker run -d --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG)
# docker run -ti --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG) /bin/bash

debug:
sudo docker run -ti --name $(NAME) $(IMAGE):$(TAG) /bin/bash

clean:
sudo docker stop ${NAME}
sudo docker rm ${NAME}

logs:
sudo docker logs ${NAME}

publish:
sudo docker push ${IMAGE}


Build the images
>make build

Debug how to install more things there
>make debug

Clean things
>make clean

List all the container
>sudo docker ps -a

List all running containers
sudo docker ps
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS                    NAMES
c9a5cefbe887        sillycat/public:ubuntu-play   "./start.sh"        21 seconds ago      Up 21 seconds       0.0.0.0:8000->8000/tcp   ubuntu-play


Stop one container
> sudo docker stop silly_kirch

Delete one container
> sudo docker rm silly_kirch

Finally, the start.sh will be as follow:
#!/bin/sh -ex

cd /share/sillycat-scalarest-1.0/
bin/sillycat-scalarest -Dconfig.file=conf/application.conf -Dhttp.port=8000 -Dhttp.address=0.0.0.0

Dockerfile will be as follow:
#Run a Simple REST API based on playframework

#Prepre the OS
FROM ubuntu:14.04
MAINTAINER Carl Luo <[email protected]>

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -qq update
RUN apt-get -qqy dist-upgrade
RUN apt-get -qqy install wget
RUN apt-get -qqy install unzip

#Install Java
RUN mkdir /tool/
WORKDIR /tool/
RUN wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz"
RUN tar zxvf jdk-8u60-linux-x64.tar.gz
RUN rm -fr jdk-8u60-linux-x64.tar.gz
RUN update-alternatives --install /usr/bin/java java /tool/jdk1.8.0_60/bin/java 1

#Install the Application
RUN mkdir /share/
WORKDIR /share/
ADD target/universal/sillycat-scalarest-1.0.zip /share/
RUN unzip sillycat-scalarest-1.0.zip
RUN rm -fr sillycat-scalarest-1.0.zip

#Start the Application
EXPOSE  8000
RUN     mkdir -p /app/
ADD     start.sh /app/
WORKDIR /app
CMD [ "./start.sh" ]

The Makefile, pay attention to the port number. That is how it is going to work.
IMAGE=sillycat/public
TAG=ubuntu-play
NAME=ubuntu-play

docker-context:

build: docker-context
# sudo docker build --no-cache -t $(IMAGE):$(TAG) .
sudo docker build -t $(IMAGE):$(TAG) .

run:
sudo docker run -d -p 8000:8000 --name $(NAME) $(IMAGE):$(TAG)

run-volume:
# sudo docker run -d --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG)
# docker run -ti --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG) /bin/bash

debug:
sudo docker run -ti -p 8000:8000 --name $(NAME) $(IMAGE):$(TAG) /bin/bash

clean:
sudo docker stop ${NAME}
sudo docker rm ${NAME}

logs:
sudo docker logs ${NAME}

publish:
sudo docker push ${IMAGE}


Visit the API like this
http://ubuntu-master:8000/api/v1/book/1

Tips
Q: How is Amazon ECS different from AWS Elastic Beanstalk?

https://aws.amazon.com/ecs/faqs/

AWS Elastic Beanstalk is an application management platform that helps customers easily deploy and scale web applications and services. It keeps the provisioning of building blocks (e.g., EC2, RDS, Elastic Load Balancing, Auto Scaling, CloudWatch), deployment of applications, and health monitoring abstracted from the user so they can just focus on writing code. You simply specify which container images are to be deployed, the CPU and memory requirements, the port mappings, and the container links. Elastic Beanstalk will automatically handle all the details such as provisioning an Amazon ECS cluster, balancing load, auto-scaling, monitoring, and placing your containers across your cluster.

Elastic Beanstalk is ideal if you want to leverage the benefits of containers but just want the simplicity of deploying applications from development to production by uploading a container image. You can work with Amazon ECS directly if you want more fine-grained control for custom application architectures.

References:
Some Docker Information
http://sillycat.iteye.com/blog/2223733

http://sillycat.iteye.com/blog/2226093

http://sillycat.iteye.com/blog/2227400

http://sillycat.iteye.com/blog/2227495

ECS
https://aws.amazon.com/ecs/

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html

猜你喜欢

转载自sillycat.iteye.com/blog/2262776
今日推荐