How to install and run Docker on AWS EC2
Tags: docker, ec2, beginners, tutorial
Published: 2022-01-22 16:35:40

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3sa9jikzkvb0ikabph9g.png)
 
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
```
### Installing docker
We will be using apt package manager to install docker engine as describe by the [official docker installation documentation](https://docs.docker.com/engine/install/ubuntu/). 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
```
#### 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
```
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
```

#### 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
```
Hurrah you have successfully installed docker and you can validate it using following command
```
$ docker -v
Docker version 20.10.12, build e91ed57
```
Now we are ready to install Nginx 
```
sudo docker container run -d -p 80:80 nginx
```
On same machine we can verify the working of this server using curl 
```
curl localhost
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1bc83wcczpra1508lpe.png)