Create a docker image/container from EC2 AMI
Solution 1
Here is how I did it.
- On source AMI locate root volume snapshot id in the description
/dev/sda1=snap-eb79b0b1
:15:true:gp2
Launch instance with public Ubuntu 14.04 AMI
Create volume from snapshot snap-eb79b0b1 (in the same region that the instance runs).
Attach volume to the instance as
/dev/sdf
mount volume to
/mnt
mount /dev/xvdf /mnt
(or)
mount /dev/xvdf1 /mnt
- install docker
https://docs.docker.com/engine/installation/ubuntulinux/
- import docker image from mounted root volume
tar -c -C /mnt/ . | docker import - appcimage-master-1454216413
- run
docker run -t -i 6d6614111fcb03d5ca79541b8a23955202dfda74995d968b5ffb5d45c7e68da9 /bin/bash
Solution 2
Docker can create an image from a tar file using the docker import
command. From the documentation:
Usage: docker import URL|- [REPOSITORY[:TAG]]
Create an empty filesystem image and import the contents of the tarball
(.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then optionally
tag it.
So you should be able to create a tar archive from your AMI image and then feed that to docker.
Solution 3
When creating the tar file cd
to the directory and tar
the tree from there.
cd /media/my-external-drive
tar -czvf /tmp/drive-image.tgz
And then to create the image ...
docker import /tmp/drive-image.tgz
This allows the dockerized
container to create the correct paths when you run it.
Related videos on Youtube
user1795516
Updated on August 28, 2020Comments
-
user1795516 almost 4 years
I am very new to docker and am trying to import my AWS EC2 AMI into a docker image. The image is a m2 linux image.
I have also setup a private docker hub(artifactory) to which I intend to push the image and make it available for consumption. What are the steps for importing AMI into docker image without starting from a base image and updating.
Pointers to any explanation would work too.
-
Scooby about 9 years'So you should be able to create a tar archive from your AMI image and then feed that to docker.' - how do I do that, I don't see an option in AWS to convert your AMI to tar ball.
-
larsks about 9 yearsYou could boot an instance from that particular AMI and then run
tar
inside the instance, for example. -
Scooby about 9 yearsWhat does run tar inside the instance mean ? I would still need to throw something at tar.
-
Mark Stosberg almost 9 years@Scooby I would mount the target EBS volumen you want to dockerify as a non-boot volume on another instance. Then a syntax like
tar -czvf drive-image.tgz /media/my-external-drive
, assuming you mounted the volume at/media/my-external-drive
and you have enough disk space on the root volume to hold the tar file. -
Neftanic almost 7 yearsCouple comments 1) mount /dev/xvdf1 /mnt 2) Docker install with script docs.docker.com/engine/installation/linux/docker-ce/ubuntu/…
-
Keith Harris over 5 yearsThis answer totally worked. However, I'm unable to run a container from this image. How did you do it?
-
rj487 about 5 yearsI can't run it as well, does this answer still work? Or amazon modify something?
-
Anuj Kumar about 4 years@CodaChang Are you using amazon linux or amazon Linux 2?
-
rj487 about 4 years@AnujKumar I was trying to use RetHat7 image in AWS.