Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

How to build and run a Python app in a container – Docker Python Tutorial

5 min read

In May 2021, over 80,000 developers participated in StackOverFlow Annual Developer survey. Python traded places with SQL to become the third most popular language. Docker is a containerization tool used for spinning up isolated, reproducible application environments. It is a popular development tool for Python developers. The tutorials and articles here will teach you how to include Docker to your development workflow and use it to deploy applications locally and to the cloud.

If you’re a Python developer and want to get started with Docker, I highly recommend you to first start with the basics of Docker. Docker Labs is one of the most popular resource that was developed by Collabnix community members. You can complete the Docker101 track before you start with the below instructions.

Getting Started

We will be leveraging Docker Desktop for most of the tutorials and examples below. Follow the below steps to install Docker Desktop in your local laptop.

Installing Docker Desktop

Let us quickly install Docker Desktop. You can refer this link to download Docker Desktop for Mac. Once you install Docker Desktop, go to Preference tab as shown in the image to make the necessary changes based on your system availability(optional).

Installing Python

You can use Homebrew to install Python in your system

brew install python

Create the app.py file with the following content:

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')

# Why you should run it at 0.0.0.0
# https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues

Now that we have our server, let’s set about writing our Dockerfile and constructing the container in where our newly born Python application will live.

Create a Dockerfile with following content: 

FROM python:3.8-alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Now that we have defined everything we need for our Python application to run in our Dockerfile we can now build an image using this file. In order to do that, we’ll need to run the following command:

$ docker build -t my-python-app .
Sending build context to Docker daemon   5.12kB
Step 1/6 : FROM python:3.8-alpine
 ---> d4953956cf1e
Step 2/6 : RUN mkdir /app
 ---> Using cache
 ---> be346f9ff24f
Step 3/6 : ADD . /app
 ---> eb420da7413c
Step 4/6 : WORKDIR /app
 ---> Running in d623a88e4a00
Removing intermediate container d623a88e4a00
 ---> ffc439c5bec5
Removing intermediate container 15805f4f7685
 ---> 31828faf8ae4
Step 5/6 : CMD ["python", "app.py"]
 ---> Running in 9d54463b7e84
Removing intermediate container 9d54463b7e84
 ---> 3f9244a1a240
Successfully built 3f9244a1a240
Successfully tagged my-python-app:latest

We can now verify that our image exists on our machine by typing docker images:

$ docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
my-python-app                             latest              3f9244a1a240        2 minutes ago       355MB$ docker images


In order to run this newly created image, we can use the docker run command and pass in the ports we want to map to and the image we wish to run.

$ docker run -p 8000:5000 -it my-python-app

  • -p 8000:5000 – This exposes our application which is running on port 8081 within our container on http://localhost:8000 on our local machine.
  • -it – This flag specifies that we want to run this image in interactive mode with a tty for this container process.
  • my-python-app – This is the name of the image that we want to run in a container.

Awesome, if we open up http://localhost:8000 within our browser, we should see that our application is successfully responding with Hello, "/".

Running our Container In the Background

You’ll notice that if we ctrl-c this within the terminal, it will kill the container. If we want to have it run permanently in the background, you can replace -it with -d to run this container in detached mode.

In order to view the list of containers running in the background you can use docker ps which should output something like this:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
70fcc9195865        my-python-app    "python app.py"           5 seconds ago       Up 3 seconds        0.0.0.0:8080->8081/tcp   silly_swirles


Dockerize Multi-Container Python App Using Compose

Learn how to containerize Python Application Using Docker Compose

The app fetches the quote of the day from a public API hosted at http://quotes.rest then it caches the result in Redis. For subsequent API calls, the app will return the result from Redis cache instead of fetching it from the public API

create following file structure :

python-docker-compose
   ↳ app.py


from flask import Flask
from datetime import datetime
import requests
import redis
import os
from dotenv import load_dotenv
import json

load_dotenv()  # take environment variables from .env.


app = Flask("app")

def get_quote_from_api():
	API_URL = "http://quotes.rest/qod.json"
	resp = requests.get(API_URL)
	if resp.status_code == 200:
		try:
			quote_resp = resp.json()["contents"]["quotes"][0]["quote"]
			return quote_resp
		except (KeyError, IndexError) as e:
			print (e)
			return None
	else:
		return None


@app.route("/")
def index():
	return "Welcome! Please hit the `/qod` API to get the quote of the day."


@app.route("/qod")
def quote_of_the_day():
	# get today's date in string
	date = datetime.now().strftime("%Y-%m-%d")
	quote = redis_client.get("date")
	if not quote:
		quote = get_quote_from_api()	
	return "Quote of the day: " + quote




if __name__ == '__main__':
	# Connect to redis client
	redis_host = os.environ.get("REDIS_HOST", "localhost")
	redis_port = os.environ.get("REDIS_PORT", 6379)
	redis_password = os.environ.get("REDIS_PASSWORD", None)
	redis_client = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password)

	# Run the app
	app.run(port=8080, host="0.0.0.0")



Run a Python application

git clone https://github.com/docker-community-leaders/dockercommunity/
cd /content/en/examples/Python/python-docker-compose
pip install -r requirements.txt
python app.py

On a different terminal run

$ curl http://localhost:8080/qod
The free soul is rare, but you know it when you see it - basically because you feel good, very good, when you are near or with them.

Dockerize Python application

/Python/python-docker-compose/Dockerfile 

# Dockerfile References: https://docs.docker.com/engine/reference/builder/

# Start from python:3.8-alpine base image
FROM python:3.8-alpine

# The latest alpine images don't have some tools like (`git` and `bash`).
# Adding git, bash and openssh to the image
RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh

# Make dir app
RUN mkdir /app
WORKDIR /app
COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

# Copy the source from the current directory to the Working Directory inside the container
COPY . .


COPY . .

# Expose port 8080 to the outside world
EXPOSE 8080

# Run the executable
CMD ["python", "app.py"]

Application services via Docker Compose

Our application consists of two services –

  • App service that contains the API to display the “quote of the day”.
  • Redis which is used by the app to cache the “quote of the day”.

Let’s define both the services in a docker-compose.yml file

File: /Python/python-docker-compose/docker-compose.yml 

# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '3'

# Define services
services:

  # App Service
  app:
    # Configuration for building the docker image for the service
    build:
      context: . # Use an image built from the specified dockerfile in the current directory.
      dockerfile: Dockerfile
    ports:
      - "8080:8080" # Forward the exposed port 8080 on the container to port 8080 on the host machine
    restart: unless-stopped
    depends_on: 
      - redis # This service depends on redis. Start that first.
    environment: # Pass environment variables to the service
      REDIS_HOST: redis
      REDIS_PORT: 6379    
    networks: # Networks to join (Services on the same network can communicate with each other using their name)
      - backend

  # Redis Service   
  redis:
    image: "redis:alpine" # Use a public Redis image to build the redis service    
    restart: unless-stopped
    networks:
      - backend

networks:
  backend:    

Running the application with docker compose

$ docker-compose up
Starting python-docker-compose_redis_1 ... done
Starting python-docker-compose_app_1   ... done
Attaching to python-docker-compose_redis_1, python-docker-compose_app_1
redis_1  | 1:C 02 Feb 2019 12:32:45.791 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 02 Feb 2019 12:32:45.791 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 02 Feb 2019 12:32:45.791 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 02 Feb 2019 12:32:45.792 * Running mode=standalone, port=6379.
redis_1  | 1:M 02 Feb 2019 12:32:45.792 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 02 Feb 2019 12:32:45.792 # Server initialized
redis_1  | 1:M 02 Feb 2019 12:32:45.792 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 02 Feb 2019 12:32:45.793 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 02 Feb 2019 12:32:45.793 * Ready to accept connections
app_1    | 2019/02/02 12:32:46 Starting Server

The docker-compose up command starts all the services defined in the docker-compose.yml file. You can interact with the python service using curl –

$ curl http://localhost:8080/qod
A show of confidence raises the bar

Stopping all the services with docker compose

$ docker-compose down

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

163 Replies to “How to build and run a Python app in…”

  1. ФСБ заявила о подрыве диверсантами ЛЭП, ведущих к Курской АЭС
    В Минобороны России подтвердили детонацию боеприпасов в Крыму
    Daily Express: заявление Путина о поставках вооружений союзникам России встревожило Запад
    Шойгу: спецоперация развеяла миф о «супероружии» Запада
    Производитель шоколада Lindt принял решение уйти из РФ
    котел для варки сиропа

    котел для варки варенья

  2. There are many ways to get high quality backlinks. One way is to post a guest post on Vhearts blog .
    Guest posts are great for getting high quality backlinks because they provide the opportunity for you to reach out to people who might not be aware of your company and brand.
    You can also use guest posts as an opportunity for SEO. Guest posts can be used as a way of getting links from Vhearts which can help boost your rankings in search engines.
    [url]https://bit.ly/3Rzvo6B[/url]

  3. Also, our project writers across the globe are well learnt their chosen field which means you can conveniently put your confidence in the way they treat your paper, no matter which academic technique you’re from. When it concerns your job prospects and also bright future, MyAssignmenthelp.com takes the obligation on itself to advertise your growth in the ideal direction. So, this way you wouldn’t need to reconsider before trusting us with your academic documents. Position an order with us now and also enjoy the incentives of wonderfully composed academic papers today. click here to read

  4. We hope you are having a wonderful week , We are delighted to let you know that your application process has been cleared by our experts. We have also come up with remarkable options that you might be interested in

  5. Additionally, our task writers across the globe are well trained in their selected field of study which means you can quickly place your belief in the method they treat your paper, no matter which scholastic technique you’re from. When it pertains to your career potential customers as well as intense future, MyAssignmenthelp.com takes the onus on itself to advertise your development in the best instructions. So, this way you wouldn’t have to think twice before trusting us with your scholastic documents. Place an order with us now as well as reap the benefits of wonderfully written scholastic papers today. he said

  6. Additionally, our task authors across the globe are well learnt their selected field of study which means you can easily place your belief in the method they treat your paper, regardless of which scholastic discipline you’re from. When it pertains to your career leads and also intense future, MyAssignmenthelp.com takes the obligation on itself to promote your growth in the ideal direction. So, this way you would not have to reconsider prior to trusting us with your scholastic documents. Place an order with us currently as well as gain the rewards of wonderfully written scholastic documents today. her comment is here

  7. В субъекте Российской Федерации должно существовать не менее одного объекта одного из следующих типов:
    Таблица 4. Итоги реализации Комплексного плана модернизации г. Сокола за 2010 – 2011 гг.
    279. Школьников Ю.В., Рутгайзер В.М., Галушка A.C., Федотова М.А., Методические рекомендации по оценке бизнеса. Торгово-промышленная палата Российской Федерации. Комитет по оценочной деятельности. Издание официальное. г. Москва, 2003 г.

    https://npo-invest.ru/strategii-forex/pattern-vnutrennij-bar-inside-bar-na-foreks-strategiya-torgovli-i-metody-nahozhdeniya/

    В состав участников проекта могут входить организации, которые его реализуют, акционеры, банки и так далее. При этом в нем могут быть заинтересованы и более значимые структуры: отрасль, регион, власти. Особенно значимые общественные программы могут нуждаться в финансовой поддержке даже на федеральном уровне.
    6. Информация об инвестиционной деятельности региона:
    Значение R устанавливается инвестором и является его внутренней нормой. В некоторых случаях оно равно ставке рефинансирования Центрального банка РФ, но чаще превышает ее. Дело в том, что у крупного предпринимателя есть альтернативные варианты вложений, обеспечивающие ему определенную отдачу. Отвлекая средства на менее рентабельные направления, бизнесмен несет потери.
    Работа на результат: быстрее и эффективнее.
    В других источниках его можно встретить под названием “индекс доходности”. Это валовые доходы от производственных вложений. Впоследствии они делятся на дисконтированный инвестиционный расход.

  8. Всем доброго времени суток!
    Есть такой вопрос, может кто в курсе? или может кто-то занимался подобным. В общем сразу перейду к делу, мне нужно раскрутить инстаграм и я начал заниматься данным вопросом, перелапатил уже кучу сайтов в поисках годной инфы и максиммум что-то похожее на нормальную инфу нашел по ссылке, где маркетолог плюс минус хорошо объяснил как и где можно заказать раскрутку. Но вот хочу еще здесь поинтересоваться такой сказать у живых людей.

  9. Whether you decide to purchase online or in-store, make sure you conduct your research before handing over your hard-earned cash. Tilling brings dormant seeds to the surface Water the area for 1 2 weeks with a sprinkler or soaker hose When weeds begin to sprout, apply the weed killer of your choice. Sometimes, CBD gummies celebrities you still need to live a rough life occasionally Ming Qianye also took his arm. https://2020cannabis.org/coa-cbd-oil/

  10. For Zhou Xizhi s sake, alas How connasseur CBD gummies dare the Taifu beat the prince, kneel down and plead guilty, burst into tears, and said a lot of things Qin Xuan couldn t understand. If you had chickenpox as a child, there s a chance you may also develop shingles as an adult. You ll just need your routing number and account number to hand to complete payment in the checkout or over the phone. https://420marijuanaspecialist.com/cbd-oil-crossing-us-border/

Leave a Reply

Your email address will not be published.