HOWTO deploys aws docker container without using elastic beanstalk or ec2 container

I would like to use docker containers without using an elastic bean stock or ec2 container service. I would like to download a .zip file that describes the container (for example, you do this with an elastic bean stitch) and have a common ec2 instance that starts it with docker.

When viewing the user data section of the created beanstalk instance of ec2 that launches the docker container, I see a cloud-init script that loads a large shell script that does all the configuration ( Example ). I assume that everything an elastic bean stock does can also be achieved manually using ec2 instances and a user data script.

My question is: can anyone provide a minimal example for a user data script that

  • installs / configures dockers
  • downloads a .zip file.
  • launches my docker image

I am familiar with autoscale groups, etc., and I want this setting to work without using the beanstalk or ec2 container maintenance magic.

+5
source share
2 answers

Basically, you need to install Docker and nginx (like a web proxy) in your EC2 instance. And then download the web application archive and deploy it. This is what an elastic bean stock does.

For basic / minimum user data to deploy a single docker container web application:

 #!/bin/bash IMG_LABEL=myapp APP_INIT_URL=https://s3.amazonaws.com/your-bucket-app/myapp-init.tar.gz function prepare_instance { apt-get -y update apt-get -y install nginx curl -sSL https://get.docker.com/ | sh mkdir /opt curl -o /opt/deployer.sh http://169.254.169.254/latest/user-data chmod 775 /opt/deployer.sh } function download_app { curl -o /tmp/current.tar.gz $1 rm -rf /opt/app mkdir -p /opt/app tar zxvf /tmp/current.tar.gz -C /opt/app rm /tmp/current.tar.gz } function build_image { docker tag ${IMG_LABEL}:latest ${IMG_LABEL}:prev || echo "No built app" docker build -t ${IMG_LABEL}:latest /opt/app } function run_container { APP_CID=$(docker run -d ${IMG_LABEL}:latest) APP_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${APP_CID}) } function setup_proxy { rm /etc/nginx/sites-enabled/* cat <<EOT > /etc/nginx/sites-enabled/app.conf map \$http_upgrade \$connection_upgrade { default upgrade; '' close; } upstream app.local { server ${APP_IP}; } server { listen 80; location / { proxy_pass http://app.local; include /etc/nginx/proxy_params; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection \$connection_upgrade; } } EOT service nginx reload } function destroy_previous { (docker ps -a --before="${APP_CID}" | awk '{ print $1,$2 }' | grep "${IMG_LABEL}" | awk '{print $1 }' | xargs -I {} docker stop {} | xargs -I {} docker rm {}) || echo "No previous container" docker rmi ${IMG_LABEL}:prev || echo "No previous image" } if [ ! -f /opt/deployer.sh ]; then prepare_instance download_app ${APP_INIT_URL} else download_app $1 fi build_image run_container setup_proxy destroy_previous 

Elastic Beanstalk has an agent that listens for an update request. But, to make this simple, we can call the above script to deploy a new version of the web application via SSH:

 ssh ubuntu@ec2-107-123-123-123.compute-1.amazonaws.com 'sudo /opt/deployer.sh https://s3.amazonaws.com/your-bucket-app/myapp-201510122341.tar.gz' 

Note. I am using EC2 instance with Ubuntu 14.04.

+5
source

User data is basically just a bash script that runs when the instance is first run.

If you want to learn an instance from scratch when creating an instance, I suggest you take a look at CloudInit and how to use it in CloudFormation. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html

With CloudInit, you can describe the files that need to be installed, the installation packages, and the services that will be included to run at startup.

0
source

Source: https://habr.com/ru/post/1233222/


All Articles