简体   繁体   中英

How can I use Hashicorp Packer to creat EC2 image that runs docker image on startup?

I am trying to build an Amazon AMI that contains docker running as a daemon, runs a particular image with a mounted volume from the host, and installs some software and data inside the docker image (which will land in the mounted volume). I'd like the resulting AMI to run the same docker image on startup with the same mounted volume. The goal is to build an EC2 image that uses a pre-built docker image that other users could use outside EC2 to do the same installation process to enable non-Amazon users to reproduce the process. The AMI piece is to facilitate a group of users to have a simple user experience.

I have used Packer in the past for something similar ( https://github.com/seandavi/terraform-can/tree/master/packer ), but I'd like to introduce the docker piece and am not clear on the approach to take. Any examples or pointers are appreciated, but the process seems like:

Inside packer build:

  1. install docker engine
  2. run docker image with mounted volume
  3. docker exec into the image to install software and data to mounted volume

How do I set up the AMI via packer to run the same image with mounted volume at startup of the AMI after build?

  1. You should build your docker image outside of packer. I dont see a reason why you need to bake a AMI and in the process of baking an AMI create a docker image.

  2. You can use the user-data section of a AMI/EC2 to be able to start a docker container when the instance first boots.

  3. I dont think you need to use Packer to do this and you should be able to do this by just using the EC2 user-data capabilities.

  4. If you do indeed need to build/bake the AMI, look at this : https://www.packer.io/docs/builders/amazon-ebs.html#user_data This should help you setup a the relevant docker run commands.

Even if you decide to not build the docker image separately, the user-data will still be able to help you run all the command you need.

Note:

a. You need to attach the volume when the docker container is started, I am not aware of a way to attach it after the fact.

b. User-Data scripts are only run on first boot of a instance. If your instance is going to start and stop you need to write a relevant script/use upstart/systemd to get it to start everytime you instance reboots.

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