Docker Research - bounswe/bounswe2022group7 GitHub Wiki

What is Docker?

  • Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud servers. It makes it easier to create, deploy, and run applications by using these containers.

  • A docker container is a collection of dependencies and code organized as software.

  • A docker image is a blueprint that specifies how to run an application. In order for Docker to build images automatically, a set of instructions must be stored in a special file known as a Dockerfile.

Step-By-Step Guide for Dockerizing Flask Applications

This guide shows the steps for dockerizing an example Flask application. It also examines the files and images created during the dockerization process.

Prerequisites

This guide assumes that you have:

Step 1: Register for an Account on Docker Hub

Go to Docker Hub and register

  • Having an account on Docker Hub makes it easier to use Docker Desktop and provides a more convenient deployment process.
  • While registering you will have to select a Docker ID. You can think of it as a username.

Step 2: Download and Set Up Docker Desktop

Links for the installation and setup

Installation links:

User manuals:

Installation links show step by step how to install Docker Desktop properly.

Sign in to your Docker Hub account in Docker Desktop

Step 3: Prepare the Example Application

Create a simple web server

You can use these commands after creating a directory in your local machine named python-docker:

$ cd /path/to/python-docker
$ pip3 install Flask
$ pip3 freeze | grep Flask >> requirements.txt
$ touch app.py

Prepare the app.py file

The code written in this file will allow us to handle basic requests. This file will look like this:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Docker!'

Step 4: Test the Example Application

  • You can run the application by running this command: $ python3 -m flask run

  • You can test the application by opening http://localhost:5000 from your browser.

  • You will see that your server is running and be able to see the outputs in the terminal as below:

    127.0.0.1 - - [22/Sep/2020 11:07:41] "GET / HTTP/1.1" 200 -

Step 5: Create the Dockerfile

An example Dockerfile

An example docker file for creating Python Docker images may look like this:

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

The directory structure for this example will look like this:

python-docker
|____ app.py
|____ requirements.txt
|____ Dockerfile

Notes

  • We may also use EXPOSE <PORT-NUMBER> to expose the application to a specific port.

Step 6: Build the Image

You can build the Docker image using the command below, and the output will look like this:

$ docker build --tag python-docker .
 [internal] load build definition from Dockerfile
 => transferring dockerfile: 203B
 [internal] load .dockerignore
 => transferring context: 2B
 [internal] load metadata for docker.io/library/python:3.8-slim-buster
 [1/6] FROM docker.io/library/python:3.8-slim-buster
 [internal] load build context
 => transferring context: 953B
 CACHED [2/6] WORKDIR /app
 [3/6] COPY requirements.txt requirements.txt
 [4/6] RUN pip3 install -r requirements.txt
 [5/6] COPY . .
 [6/6] CMD [ "python3", "-m", "flask", "run", "--host=0.0.0.0"]
 exporting to image
 => exporting layers
 => writing image sha256:8cae92a8fbd6d091ce687b71b31252056944b09760438905b726625831564c4c
 => naming to docker.io/library/python-docker

The tag parameter is optional, it is used in case of need to tag a newly created docker image (python-docker in our case) with a given tag id. If you do not use tag option, latest will be used. Please note that we left off the optional tag for now to help simplify things above.

Step 7: View and Tag Images

Viewing Docker Images

You can use docker images command. Example output for our case:

Here you can see the IDs, tags, etc. of different Docker images.

Tagging Docker Images

You can use this command to tag a specific Docker image:

docker tag python-docker:latest python-docker:v1.0.0

Step 8: Push Your Image to Docker Hub

  • Pushing the image to Docker Hub makes deployment process much easier as you can pull it from your Docker Hub repository.

  • You can push your image to Docker Hub using this command:

    docker push your_docker_id/python-docker:v1.0.0

  • The image shall be visible under the repository named your_docker_id/python-docker on Docker Hub after successfully pushing it.

Step 9: Deploy Your Application to Amazon AWS using ECS

Video Resources for Dockerizing Flask Applications

References