DEV Community

Cover image for How to install and run Docker on AWS EC2
usmanalimaan
usmanalimaan

Posted on

How to install and run Docker on AWS EC2

There are different ways to run docker into AWS cloud but today we will discuss how we can run docker on an AWS EC2 machine.
There could be different scenarios when you are starting with docker but don't have some good local environment or just want to have a good idea running a docker environment on Linux server then you are at right place.

Prerequisite:

  • How to create AWS EC2 instance
  • How to SSH into a Linux instance
  • What is Nginx web server or Apache HTTP web Server(using its container to show installation)

Creating EC2 instance using Ubuntu 20.04 LTS image

In your AWS account you need to go services section then EC2.
On EC2 dashboard you can see "Launch a virtual machine"
press this button and it will land you on this screen
chose image while creating ec2 instance

You can chose your favorite Linux distribution, I am choosing

> Ubuntu Server 20.04 LTS (HVM), SSD Volume Type - ami-04505e74c0741db8d (64-bit x86) / ami-0b49a4a6e8e22fa16 (64-bit Arm)

SSH into Instance

Once instance is created you need to have its Public IP to SSH into this server. In my case

ssh -i mango.pem ubuntu@ec2-3-88-116-37.compute-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Installing docker

We will be using apt package manager to install docker engine as describe by the official docker installation documentation. On this document you can find your own chosen Linux distribution installation guide documentation into left section.

Setting up repository

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

 sudo apt-get update

 sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
Enter fullscreen mode Exit fullscreen mode

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Enter fullscreen mode Exit fullscreen mode

Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Enter fullscreen mode Exit fullscreen mode

According to official doc updating the package manager and installing docker engine

sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode

Hurrah you have successfully installed docker and you can validate it using following command

$ docker -v
Docker version 20.10.12, build e91ed57
Enter fullscreen mode Exit fullscreen mode

Now we are ready to install Nginx

sudo docker container run -d -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

On same machine we can verify the working of this server using curl

curl localhost
Enter fullscreen mode Exit fullscreen mode

And if we open 80 port on this instance for public access. we can access it into any browser using Public IP or DNS value.
Running Nginx on docker

Top comments (0)