AMAZON ECS(1)Docker My API

本文详细介绍如何在Amazon EC2实例上安装配置Docker,并通过示例展示如何使用Dockerfile构建简单的RESTful API应用。此外,还对比了Amazon ECS与AWS Elastic Beanstalk的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <luohuazju@gmail.com>
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 <luohuazju@gmail.com>

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值