DEV Community

Kim Diep
Kim Diep

Posted on • Edited on

Intro to Docker Containers & Microsoft Azure Part 1 - Beginner's Guide to Containerising a Hello World Python Web App

Greetings!

Hi everyone, it's great to be back again! This blog is part one of a two-part series on Docker and Microsoft Azure. In Part 1, we will containerise a Hello World Python web app using Docker. In Part 2, we will learn how to build and push the container image using devOps pipelines on Microsoft Azure.

Prerequisites:

Before we get stuck in, here are some prerequisites:

Containers

Here is a useful link if you would like to have a quick 5 min intro to containers.

Docker

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. (Ref: https://en.wikipedia.org/wiki/Docker_(software))

It mitigates against the classic: "But it works on my machine problem!" and streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide the applications and services. (Ref: https://docs.docker.com/engine/docker-overview/).

Containers are great for continuous integration and continuous delivery (CI/CD) workflows. Did I mention we can even integrate it with Azure? ☁️

You can get started on the Docker documentation here.

Docker Desktop

This blog assumes you have Docker Desktop Installed. To install Docker Desktop, you can use this link. I'm using my lovely Macbook Pro 💻 for this - hehe! 😉; but you can choose whether you want to download Docker for Mac or Windows.

For this tutorial, you will need Python 3.7+ by going to the following link.

Pip

You will also need the latest version of pip which is a recommended tool for installing Python packages.

To check you have the right Python and pip versions, you can use the commands:

python --version

pip --version
Enter fullscreen mode Exit fullscreen mode

Checking Python and Pip Versions

Now, onto the fun stuff! 🏄‍♀️

Step 1: Project Setup - Create a Project Directory

First, let's create a new project directory called hi-there-docker. Please feel free to call your project directory any name you want, but just remember to reference it throughout this blog.

Open up the project directory in your favourite code editor. I find Visual Studio Code works quite well if you're starting out.

Step 2: Project Setup - File Setup

Next, let's create a requirements.txt file in the directory; it is good practice in Python projects to have this file to manage packages.

Top Tip! 💡

If you are using Windows, you can create a function called 'touch' for the UNIX fans out there which enables you to use the touch command to create a new file in Windows Powershell. Enter the following command in Windows Powershell to enable this:

function touch {set-content -Path ($args[0]) -Value ($null)}
Enter fullscreen mode Exit fullscreen mode

In the requirements.txt file, enter the package 'Flask==1.0.2', as we will need Flask to create the hello world application.

requirements.txt

Finally, enter the following in the terminal to install the packages listed in requirements.txt.

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 3: Python - Create a Hello World Flask App

I won't be going into detail on how Flask works, but you should check it out if you're interested.

We're now onto Step 3, let's move onto creating a new file called main.py.

In the main.py file, enter the following:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
   return "Hello World!"

if __name__ == "__main__":
   app.run(host="0.0.0.0", port=int("5000"), debug=True)
Enter fullscreen mode Exit fullscreen mode

This is a very simple app and has one route with 'Hello world'.

main.py

Step 4: Create a Dockerfile

Once that's complete, we can move onto Docker! Let's create a Dockerfile. The Dockerfile contains settings such as the base image for the container and Python version, as well as dependencies on build to produce the container image.

Create a Dockerfile in the root directory of your project folder and enter the following:

FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./main.py
Enter fullscreen mode Exit fullscreen mode

Your Dockerfile should look something like this:

Dockerfile

Step 5: Let's Build the Image

Before we can build the image using Docker, let's confirm the Docker CLI is working by typing the following into your terminal:

docker --version
Enter fullscreen mode Exit fullscreen mode

Checking Docker Version

Also, check that Docker Desktop is up and running, there should be a cute little icon of the Docker whale on your desktop panel.

Docker Whal

Now we are ready to build the image! 💿

You can tag the image with a name of your choosing, but let's use the name hi-there-image

Ensure you are in the root directory of the project and enter the following into the terminal:

docker build --tag hi-there-image .
Enter fullscreen mode Exit fullscreen mode

Building the Image

🎉 You've built your first image using Docker and tagged it with a name - woohoo! 🎉

Step 6: Running the Image as a Container

Once the Docker image has been built, you are now ready to run the image as a container.

Our container will be called:

hi-there-container

And our image name is:

hi-there-image

To start the application as a container, enter the following into the terminal:

docker run --name hi-there-container -p 5000:5000 hi-there-image
Enter fullscreen mode Exit fullscreen mode

Running the Image as a Container

🎈That's it! You are now running the image as a container! 🎈

Step 7: Go to the App

🥳 Now you can go to your app at http://localhost:5000 🥳

Alt Text

Final Step: Viewing and Managing Containers using the Docker CLI

#To display a list of running containers:
docker ps

#To stop running one or more running containers:
docker container stop [container name(s)]

#For example, if we wanted to stop running the hi-there-container, we can run the following command in the terminal:
docker container stop hi-there-container

#To remove one or more containers:
docker container rm [container name(s)]

#For example, if we wanted to remove the hi-there-container, we can run the following command in the terminal:
docker container rm hi-there-container

#To confirm that the container is no longer running, check that it is no longer in the list:
docker ps
Enter fullscreen mode Exit fullscreen mode

😊 Congratulations, you have just built and containerised a Hello World App using Docker! 😊

🤔 What's next?

If you want to explore more on how to build and push the images using Docker as tasks within the Microsoft Azure Pipelines, watch out for Part 2 of this blog series. ☁️

Top comments (0)