Install Docker
sudo yum update -y
sudo yum -y install docker
Start Docker
sudo service docker start
Access Docker commands in ec2-user user
sudo usermod -a -G docker ec2-user
sudo chmod 666 /var/run/docker.sock
docker version
Answer from Prashanth Sams on Stack OverflowHow do I install docker version 27.3.1 on Amazon Linux 2023?
How to run AmazonLinux 2023 on a local Docker host?
php - Docker with Amazon Linux - Stack Overflow
Not able to install latest Docker engine in Amazon Linux 2 on EC2
Videos
Install Docker
sudo yum update -y
sudo yum -y install docker
Start Docker
sudo service docker start
Access Docker commands in ec2-user user
sudo usermod -a -G docker ec2-user
sudo chmod 666 /var/run/docker.sock
docker version
So sorry, it was my misunderstanding. My OS is Redhat Linux. I get to install docker by
yum-config-manager --enable rhui-REGION-rhel-server-extras
sudo yum -y install docker
systemctl start docker
systemctl enable docker
docker version
I had the same goal of testing instances in development environment and initially I thought it should be as easy as docker run amazonlinux:2 -it. But I was so wrong and it took me almost one full day to get it to work!
Funny thing is when you google "amazonlinux Docker" it's often people trying to install "Docker in amazonlinux", but here we want to install "amazonlinux in Docker"!
We also want to install Docker in that amazonlinux, so basically "Docker in amazonlinux in Docker" which is "Docker in Docker" eventually! ;D*
My findings:
- Amazonlinux in Docker (created via
FROM amazonlinux:2) is so bare and empty that it doesn't even have basic stuffs likesudoorpasswd.) New AWS EC2 instances do. - In order to have your
servicedproperly working (to start any daemon, including Docker Daemon), you need to have/usr/sbin/initbe there (viayum install initscriptsand actually called. However, the meat you want to play with need your shell to start from/bin/bash. - You are running a Docker within a Docker. That needs to be priviledged from the host in your
docker runvia--priviledged. - You need to share the
/sys/fs/cgroupfrom your host machine (it can be read-only) for it to be able to properly initialize docker daemon.
My solution:
1) To fulfill the first two issues above, your Dockerfile can be:
CopyFROM amazonlinux:2
RUN yum update -y && yum install -y initscripts;
CMD ["/usr/sbin/init"]
2) Build an image from it, e.g. docker build . -t ax1
3) Then, to address the latter two issues above, run a detached (running in background) container from it, priviledged, with a shared volume to your /sys/fs/cgroup. e.g.
docker run --name ac11 -d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro ax1
4) Finally you can bash into it using docker exec -it ac11 bash
5) Now, it's very close to a new EC2 instance. (Yet, missing sudo, actual ec2-user and other stuffs that we skipped in our Dockerfile to keep this solution simple.)
Anyway, now you can install docker as instructed by AWS Docs. That is, once you are in the container, do:
Copyamazon-linux-extras install -y docker;
and then restart the docker service once:
Copyservice docker restart;
Now, docker ps should be working!
Docker containers stops when they don't have a process to run. Add an entrypoint to your Dockerfile to keep the machine running.
You could do a sleep infinity or sleep 99999 if you don't really have any process to run.
CopyFROM amazonlinux:2017.03
RUN yum update -y
CMD [“sleep”, “infinity”]
Hello,
How do I run a container that has the same tools as an Amazon Linux 2 instance? I did this, but the aws cli is not installed.
docker run -it public.ecr.aws/amazonlinux/amazonlinux /bin/bash