AWS EC2 is not installed docker runtime by default. This article would step-by-step introduce few steps to install docker manually on EC2 and run a simple nginx service on it.

What is Docker

Docker is a platform for running applications in containers. Containers allow a developer to package an application with all of the parts it needs, such as libraries and other dependencies. This ensures that the application will run consistently across different environments.


What is AWS EC2

AWS EC2 is a web service that allows users to rent virtual computers on which to run their own computer applications. With EC2, users can launch virtual servers (aka “instances”) with a variety of configurations of CPU, memory, storage, and networking resources. These instances can be launched and terminated on-demand, and can be easily scaled up or down as needed.

AWS generously provides free amount of EC2 usage(so-called AWS free-tier) on some specifics instances type(about 750hrs per month). The introduction later would use free-tier EC2 t2-micro as example. You could follow it step by step without extra cost.


Launch Instance

  1. go to EC2 service page in your browser and click Launch Instances
  2. just use AWS provided OS image is ok, and select the t2-micro instance type(as mentioned above, this instance type is covered by free-tier)

select-instance.png

  1. left the key pair empty(Proceed without a key pair) and allow HTTP traffic
    • for this demo, we don’t need to create key pair because we won’t use key pair to access to this instance.
    • remember to allow HTTP because we would create a nginx service later

security-setting.png

  1. click Launch Instance after all setting done

Install Docker


  1. click the Connect button to connect to your instance

connect-instance.png

  1. run yum update to update the packages on the system to the latest version
    • note that you might need to use sudo
sudo yum update -y
  1. install Docker by yum
sudo yum install -y docker
  1. update user permission to run docker command
    • note that your user name might be ec2-user by default
sudo usermod -a -G docker {user-name}
  1. reboot your OS
sudo reboot
  1. now you could reconnect to your instance again, and type docker -v to check if docker installed and your user have the permission to execute docker command

Run Docker

  1. start docker service
sudo service docker start
  1. start a nginx service
docker run -d -p 80:80 --name webserver nginx
  1. click the public IP of your instance, you could see welcome message from nginx as below
    1. note that you should make the connection with this IP with HTTP not HTTPS

nginx-welcome.png


Make Installation Easier

  1. you could add user data while launching the instance.
    • user data would be execute once while launching instance
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo usermod -a -G docker ec2-user
sudo reboot
sudo service docker start

If You Want to Use Docker-Compose

  1. install docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
  1. change user permission to execute docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Conclusion

Congrats🎉🎉, now you have already know how to install docker runtime on AWS EC2, and run a simple service on it.