简体   繁体   English

如何使用Hashicorp Packer创建在启动时运行docker映像的EC2映像?

[英]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). 我正在尝试构建一个Amazon AMI,其中包含作为守护程序运行的docker,从主机运行具有已装入卷的特定映像,并在docker映像内安装一些软件和数据(这些映像将落入已装入的卷中)。 I'd like the resulting AMI to run the same docker image on startup with the same mounted volume. 我希望生成的AMI在启动时以相同的安装卷运行相同的docker映像。 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. 目标是构建一个EC2映像,该映像使用预先构建的docker映像,其他用户可以在EC2外部使用该映像执行相同的安装过程,以使非Amazon用户能够重现该过程。 The AMI piece is to facilitate a group of users to have a simple user experience. AMI的目的是促进一组用户具有简单的用户体验。

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. 过去我曾使用Packer做类似的事情( https://github.com/seandavi/terraform-can/tree/master/packer ),但我想介绍docker文章,但不清楚该方法采取。 Any examples or pointers are appreciated, but the process seems like: 任何示例或指针,不胜感激,但过程看起来像:

Inside packer build: 内包装器版本:

  1. install docker engine 安装Docker引擎
  2. run docker image with mounted volume 使用已安装的卷运行Docker映像
  3. docker exec into the image to install software and data to mounted volume 将docker exec插入映像以将软件和数据安装到已安装的卷

How do I set up the AMI via packer to run the same image with mounted volume at startup of the AMI after build? 如何在构建后的AMI启动时通过打包程序设置AMI以运行具有已装载卷的相同映像?

  1. You should build your docker image outside of packer. 您应该在打包程序之外构建docker映像。 I dont see a reason why you need to bake a AMI and in the process of baking an AMI create a docker image. 我看不到为什么需要烘焙AMI并在烘焙AMI的过程中创建docker映像的原因。

  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. 您可以使用AMI / EC2的user-data部分来在实例首次启动时启动docker容器。

  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. 我不认为您需要使用Packer来执行此操作,并且仅使用EC2用户数据功能就应该能够执行此操作。

  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. 如果确实需要构建/烘焙AMI,请查看以下内容: https : //www.packer.io/docs/builders/amazon-ebs.html#user_data这将帮助您设置相关的docker运行命令。

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. 即使您决定不单独构建docker映像,用户数据仍将能够帮助您运行所需的所有命令。

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. 您需要在docker容器启动时附加卷,事实不明如何附加卷。

b. 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. 如果您的实例要启动和停止,则需要编写相关脚本/使用upstart / systemd使其在每次实例重新启动时启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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