banner



How To Install Docker On Aws Ec2 Linux

How To Use Docker in an Amazon EC2 Instance

Use an Amazon Machine instance as you would on-premise

Photo past Rahul Chakraborty on Unsplash.

Information technology works on my machine! Bully, but how do nosotros make it work in the cloud? Well, Docker solves exactly that problem.

This article is perfect for people who are migrating their application to the cloud or are on their deject learning journeying. At this point, you normally have 1 application (or more than) running somewhere using Docker.

In a previous article, we saw how to migrate such a Docker image to the Amazon cloud — more specifically, to Amazon ECR.

In this article, nosotros will have it one step further. We are going to expect at the following three things:

  • How to install Docker.
  • How to install docker-etch.
  • How to pull Docker images from Amazon ECR.

By the end of this article, you volition have everything in your EC2 instance bachelor to work with Docker, just similar it would work on a Linux computer.

Curious? Then let'due south become started.

How To Install Docker

Installing Docker is pretty like shooting fish in a barrel on an Amazon Linux two Amazon Machine Epitome (AMI). If you haven't started your instance still, look at this guide to go one up and running.

Installing Docker consists of 5 modest steps.

Beginning, you have to update the installed packages and packet enshroud on your example. Ordinarily, this is an unnecessary stride because you volition have only started an case, but it is just a all-time practise.

Note: The -y option is needed because the commands oft prompt you to reply aye or no. This takes care of that by responding yes.

            sudo yum update -y          

Secondly, y'all take to install Docker as a bundle. The following command is recommended by Amazon, but yous could nonetheless do it using yum also:

            sudo amazon-linux-extras install docker -y          

At present nosotros accept Docker equally a service. The third pace is to start the Docker service:

            sudo service docker outset          

At present, we arrive at the fourth footstep. This one is rather important and often forgotten. To exist able to utilise Docker commands without using sudo, you lot have to add together ec2-user to the docker group:

            sudo usermod -a -G docker ec2-user          

Note: If you lot 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 whatsoever Docker command (e.g. docker info should work just fine).

And that's it. Docker is now installed and ready to be used.

But at that place is 1 more subtle detail. Every time your Amazon AMI is rebooted, yous desire the Docker service to remain up and running. Therefore, we have one last command to use:

            sudo systemctl enable docker          

Finally, y'all can also listing these commands in the user data when starting upward a Docker AMI then yous don't have to run them afterward launching a new machine.

You lot tin can fill in user data in Pace 3 when launching an AMI:

Configuring case details (with user data)

You don't need to use sudo inside user data.

Note: User data is executed just once when the AMI launches, so you'll accept to outset a new, clean AMI to test your user data. My communication: Examination every control first in an AMI and launch a new fully configured AMI afterward.

userdata.txt

How To Install Docker-Compose

Personally, I recall docker-etch is a very convenient tool to piece of work with. If you have a few Docker images, yous quickly get tired of entering everything via the command line. This is where docker-compose comes in. It allows you lot to configure all the images in one place.

That's why I too install docker-compose in my EC2 instance nowadays. It allows me to exercise all the configuration from a file. You can download this file in your container in several means (e.chiliad. via Git).

An application consisting of multiple containers tin easily be started with the command docker-etch up -d and stopped with docker-etch down.

You take to install docker-compose to utilize those commands. This is done in three steps.

In the first step, y'all download the latest version of docker-compose (in this instance, 1.28.ii):

            sudo whorl -50 "https://github.com/docker/compose/releases/download/ane.28.two/docker-etch-$(uname -s)-$(uname -grand)" -o /usr/local/bin/docker-compose          

In the second footstep, yous brand it executable. You can do this the standard Linux style using chmod:

            sudo chmod +x /usr/local/bin/docker-compose          

In the last footstep, you'll find out that docker-compose is not on your path all the same, so you tin can create a symbolic link (or symlink) to make it work:

            sudo ln -southward /usr/local/bin/docker-compose /usr/bin/docker-etch          

When you've done all that, you can choose to append those commands to your user data (without sudo) besides.

Neat, now yous've got docker-compose as a tool available inside EC2!

How To Pull Docker Images From Amazon ECR

One of the first things you'll probably try is to pull a Docker prototype from Amazon ECR.

Well, if you don't configure annihilation, it will fail. There are two things you demand to fix to brand that work.

Firstly, in Step 3 of launching an AMI, select an IAM role. You lot'll need at least read admission to ECR to pull Docker images from a private repository. It is good practice to limit information technology to read-but admission if you lot're just pulling from ECR.

If you are planning to also push Docker images, it would look like the image below:

Secondly, you'll need to log into AWS inside your EC2 container. The command looks like this. Don't forget to supersede the region and registry URL. You tin can besides look it up in your ECR repository when visiting it from the Amazon Ambassador Console.

Note: Y'all can append the command to your user data besides, but you'll have to re-execute it from fourth dimension to time because the login will expire.

            aws ecr get-login-password --region              eu-west-2              | docker login --username AWS --password-stdin              XXXXXXXXXXXX.dkr.ecr.european union-west-two.amazonaws.com                      

Conclusion

And that's information technology!

In this article, we successfully fix an EC2 Amazon Car Image with Docker, docker-compose, and access to Amazon ECR. You've also seen that you lot tin can listing these commands inside user data to configure the car at launch time.

This allows you to work exactly as you lot would on-premise, but now within the cloud.

If you have any questions, don't hesitate to ask them in the comments department!

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

Posted by: haileysiond1936.blogspot.com

0 Response to "How To Install Docker On Aws Ec2 Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel