How to Install Docker on Amazon EC2 Instance?

Getting Started with Docker on AWS EC2

Docker on Amazon EC2

Running Docker on AWS provides admins and developers a highly reliable, low-cost way to build, ship, and run distributed applications. Docker can be installed on many different operating systems, including Linux distributions, like Ubuntu, and even Mac OSX and Windows.


  • AWS EC2 instance should be up and running.
  • Connect with your instance (e.g. via putty).
  • A non-root user with sudo privileges.
  • Python should be installed to run flask application.

Steps to Install Docker on an Amazon EC2 Instance

  • Update the installed packages on your instance with the below command:
sudo apt-get update
  • Add the GPG key to your instance for the official Docker repository:
curl -fsSL | sudo apt-key add -
  • Add the Docker repository to the APT sources:
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
  • Update the packages from the newly added repo:
sudo apt-get update
  • Install it from Docker repo instead of the default Ubuntu 16.04 repo:
apt-cache policy docker-ce
  • Install Docker:
sudo apt-get install -y docker-ce
  • To review the docker service status:
sudo service docker start
sudo service docker status
  • The output should display the service is active and running:

docker.service – Docker Application Container Engine

Loaded: loaded (/lib/systemd/zehntech/docker.service; enabled; vendor preset: enabled)

Active: active (running) since Fri 2019-09-13 05:47:06 UTC; 25s ago


Main PID: 1339 (dockerd)

Tasks: 10

Memory: 123.5M

CPU: 340ms

CGroup: /system.slice/docker.service

└─1339 /usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock


  • Installing Docker will provide you the Docker service (daemon) and also the Docker command-line utility.

Basic Commands for Docker

  • To view system-wide information about Docker, use below command:
sudo docker info
  • To list all the docker sub commands:
  • To list all the available images:
sudo docker images
  • To list all the containers:
sudo docker ps
  • To search for images on Docker Hub, type docker search with image name:
sudo docker search ubuntu

Run Docker Image:

By default, docker pulls the images from a Docker registry called Docker Hub managed by Docker company.

To confirm whether you can download the images from Docker Hub:

sudo docker run hello-world
  • The below output indicates that Docker is working correctly:

Hello from Docker!

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.

2. The Docker daemon pulled the “hello-world” image from the Docker Hub. (amd64)

3. The Docker daemon created a new container from that image which runs the

executable that produces the output you are currently reading.

4. The Docker daemon streamed that output to the Docker client, which sent it

to your terminal.


Build a Flask Application using Docker

Flask is known as a web micro-framework because it does not require any specific plugins to run, and it is built on Python. The Flask framework is very flexible and lightweight.

In this tutorial, we will create a Flask application then deploy it with Docker.

Setting up a Flask Application:

  • First, create a directory structure for the Flask Application. Run the below command to create a “DemoApp” directory in “/var/www/”.
sudo mkdir /var/www/DemoApp
  • Switch to the newly created directory:
cd /var/www/DemoApp
  • Create the basic folder structure:
sudo mkdir -p app/static app/temp
  • Create a file, required to run the Flask application:
sudo nano app/
  • The above file indicates the python interpreter that the app directory is a package.
  • Add the following code to the above file, which will create a Flask instance and will import the logic from the file (which we will create further).

from flask import Flask

app = Flask(__name__)

from app import views


Save and close the file.

  • Create the “” file, which will contain application logic.
sudo nano app/
  • Add the code into, which will return the “Hello Zehntech!” when we visit the web page.

from app import app


def home():

return “Hello Zehntech!”


The “@app.route” in the code block called “Decorator,” from where we define on which URL the home() method trigger.

Now, you’re ready to create the uwsgi.ini file with file, which will contain the uWSGI configurations for our application. uWSGI is an application server and a protocol of deployment for the Nginx. There are multiple application servers available that can serve FastCGI, uWSGI, and HTTP protocols.

  • To create the uwsgi.ini file:
sudo nano uwsgi.ini
  • Add the below configuration into uwsgi.ini file:


module = main

callable = app

master = true


The main referred to as, which instructs uWSGI to use the app instance exported by the main application. The application will keep running with the “master” option.

  • Next, create the entry point to the application, which is the “” file. It will instruct uWSGI how to interact with the application.
sudo nano
  • Import the previously create package named as “app” into the

from app import app


  • Create a file named “requirement.txt” which contains the dependencies installed by pip package manager:
sudo nano requirements.txt
  • Write below line into the newly created file “requirement.txt” to specify the Flask version to be installed:



Save and close the file.
We have successfully set up our Flask application.

Creating a Dockerfile and Setting up Docker

First, we will create a Dockerfile which contains the commands used to assemble the image and create the docker deployment.

  • To create a Dockerfile:
sudo nano Dockerfile
  • Add the configuration commands into the Dockerfile which will specify the requirements and how the image will be built.

    NOTE: There are many tags available in Docker, but we are using the tag with alpine version e.g., FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7.
    As the flask is based on WSGI, therefore we use the Docker image with uWSGI and Nginx for creating Flask web applications in Python 3.6 version using Alpine Linux in a single container. It is the most common way to deploy web applications in Python for decent performance.

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7

RUN apk –update add bash nano


ENV STATIC_PATH /var/www/app/static

COPY ./requirement.txt /var/www/requirements.txt

RUN pip3 install -r /var/www/requirements.txt


  • Save and close the configuration file.
  • Create a “” script which builds the Docker container with below command:
sudo nano

This is a shell script that builds an image from the Dockerfile and creates a container from the docker image. Add your commands into the “” script like below:



docker build -t ${app} .

docker run –name=${app} \ -v $PWD:/app ${app}


This will create an image named “zehntech.test” which is saved as a variable named “app”.

  • Execute the script “” for creating the Docker image and then build a container from the image:
sudo bash
  • After executing the above script, run the command to list all the running containers:
sudo dokcer ps

58b05508f4dd zehntech.test “/ /sta…”>80/tcp zehntech.test


  • To check that the docker container is running or not, visit the IP address with the specified port e.g. 5000 in your browser like below:
  • You will see the output as shown below:

Great! We have successfully deployed our Flask application on Docker.

NoteIf the docker container is running, still the application is not loading, please check if the required ports are allowed in the firewall of your server or in the Inbound rules in AWS for EC2 instance.


In the above document, we have installed Docker in an AWS EC2 Instance (Ubuntu 16.04) and run some basic commands of Docker. After that, we have created a Flask application using Docker and created a Dockerfile to create the Docker image.
Finally, we have created the Docker container by running the docker image.

Related Posts