简体   繁体   中英

How to run a simple Docker container when an EC2 is launched in an AWS auto-scaling group?

$ terraform version
Terraform v0.14.4

I'm using Terraform to create an AWS autoscaling group, and it successfully launches an EC2 via a launch template, also created by the same Terraform plan. I added the following user_data definition in the launch template. The AMI I'm using already has Docker configured, and has the Docker image that I need.

user_data = filebase64("${path.module/docker_run.sh}")

and the docker_run.sh file contains simple

docker run -p 80:3000 -d 1234567890.dkr.ecr.us-east-1.amazonaws.com/node-app:latest

However, when I ssh to the EC2 instance, the container is NOT running. What am I missing?

Update: Per Marcin's comment, I see the following in in /var/log/cloud-init-output.log

Jan 11 22:11:45 cloud-init[3871]: __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'docker run -p 80:3000 -d...'

From AWS docs and what you've posted the likely reason is that you are missing /bin/bash in your docker_run.sh :

User data shell scripts must start with the #. characters and the path to the interpreter you want to read the script (commonly /bin/bash).

Thus your docker_run.sh should be:

#!/bin/bash
docker run -p 80:3000 -d 1234567890.dkr.ecr.us-east-1.amazonaws.com/node-app:latest

If this still fails, please check /var/log/cloud-init-output.log on the instance for errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM