How To Use Docker in an Amazon EC2 Instance | by Dieter Jordens | Better Programming

Ask questions Research chat →

https://betterprogramming.pub/how-to-use-docker-in-an-amazon-ec2-instance-5453601ec330 · scraped

deploy

Attachments

Scraped Content

— 388 words · 2026-02-14 02:56:50 UTC ·

Excerpt

How To Install Docker Installing Docker is pretty easy on an Amazon Linux 2 Amazon Machine Image (AMI). If you haven’t started your instance yet, look at this guide to get one up and running.Installing Docker consists of five small steps.First, you have to update the installed packages and package cache on your instance. Usually, this is an unnecessary step because you will have just started an instance, but it is just a best practice.Note: The -y option is needed because the commands often prompt you to answer yes or no. This takes care of that by responding yes.sudo yum update -ySecondly, you have to install Docker as a package. The following command is recommended by Amazon, but you could still do it using yum as well:sudo amazon-linux-extras install docker -yNow we have Docker as a service. The third step is to start the Docker service:sudo service docker startNow, we arrive at the fourth step. This one is rather important and often forgotten. To be able to use Docker commands with
How To Install Docker Installing Docker is pretty easy on an Amazon Linux 2 Amazon Machine Image (AMI). If you haven’t started your instance yet, look at this guide to get one up and running.Installing Docker consists of five small steps.First, you have to update the installed packages and package cache on your instance. Usually, this is an unnecessary step because you will have just started an instance, but it is just a best practice.Note: The -y option is needed because the commands often prompt you to answer yes or no. This takes care of that by responding yes.sudo yum update -ySecondly, you have to install Docker as a package. The following command is recommended by Amazon, but you could still do it using yum as well:sudo amazon-linux-extras install docker -yNow we have Docker as a service. The third step is to start the Docker service:sudo service docker startNow, we arrive at the fourth step. This one is rather important and often forgotten. To be able to use Docker commands without using sudo, you have to add ec2-user to the docker group:sudo usermod -a -G docker ec2-userNote: If you want to make sure the system picks up these Docker permissions, you can log out and log in again, but they worked out of the box for me.Verify the installation by running any Docker command (e.g. docker info should work just fine).And that’s it. Docker is now installed and ready to be used.But there is one more subtle detail. Every time your Amazon AMI is rebooted, you want the Docker service to remain up and running. Therefore, we have one final command to use:sudo systemctl enable dockerFinally, you can also list these commands in the user data when starting up a Docker AMI so you don't have to run them after launching a new machine.You can fill in user data in Step 3 when launching an AMI:Press enter or click to view image in full sizeConfiguring instance details (with user data)You don’t need to use sudo inside user data.Note: User data is executed only once when the AMI launches, so you’ll have to start a new, clean AMI to test your user data. My advice: Test every command first in an AMI and launch a new fully configured AMI afterward.Press enter or click to view image in full sizeuserdata.txt

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation