Python Docker


In this tutorial, How to run python on Docker. Python is a programming language. Python Docker image is the latest point release. 

The Docker image version was released in March 2022.

Image 3.9 release
Debian 11 3.9.2
Ubuntu 20.04 3.9.5
RHEL 8 3.9.7
RHEL 9 3.9.10
Docker python 3.9.14

You need to install Docker on Ubuntu.

The working directory python docker:

root@devopsroles:~/dockerpython# ls -lF
total 20
-rw-r--r-- 1 root root  111 Nov 20 14:31
-rw-r--r-- 1 root root  236 Nov 20 15:00 Dockerfile
-rw-r--r-- 1 root root   20 Nov 20 14:27 requirements.txt

How to build a Docker container running a simple Python application.

Setup Dockerfile

Create a folder and create a virtual environment. This isolated the environment for the Python project.

For example

mkdir dockerpython
cd dockerpython
python3 -m venv dockerpython
source dockerpython/bin/activate
Create a folder and create a virtual environment

Create a new file Dockerfile.

FROM python:3.9-slim-buster

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

COPY . .

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

Save and close.

Create the Python App

Create an file. For example as below

from flask import Flask
app = Flask(__name__)

def hello_world():
     return 'Hello, Docker!'

Save and close.

Create the requirements.txt file. This contains dependencies for the app to run.

For example, we add the packages for the requirements.txt file.


Or method 2: show all packages installed via pip use pip3 freeze and save to the requirements.txt file.

pip3 install Flask
pip3 freeze | grep Flask >> requirements.txt

we will test if the works localhost using the command below

python3 -m flask run --host= --port=5000

Open the browser with the URL http://localhost:5000

Docker Build image and container

You build a Docker image from created Dockerfile. Use the command below to build the Docker image

docker build --tag dockerpython .

Tag the image using the syntax

docker tag <imageId> <hostname>/<imagename>:<tag>

For example Tag the image

docker tag 8fbb6cdc5e76 huupv/dockerpython:latest
docker python images

Now, the Run Docker image has the created and tagged with the command line below:

docker run --publish 5000:5000 <imagename>

Use the command below to list containers running.

docker ps

The result below:

Python Docker

You can now test your application using http://localhost:5000 on your browser.

test your application using http://localhost:5000

Docker pushed and retrieved from Docker Hub

The container will be pushed and retrieved from the Docker Hub registry. The command simple

docker push <hub-user>/<repo-name>:<tag>.

On the website Docker Hub. Click Create Repository to give the repo a name.

Docker pushed and retrieved from Docker Hub

Create 1 repo name and description as below:

Docker pushed and retrieved from Docker Hub 2

Copy the command to your terminal, replacing tagname with version latest.

Docker pushed and retrieved from Docker Hub 3

In your terminal, we run the command docker login to connect the remote repository to the local environment. add username and password to valid login.

Run the command to push to the repository you created it.

docker push <hub-user>/<repo-name>:tagname
Docker pushed and retrieved from Docker Hub 4

Confirm your image to be pushed to the Docker Hub page

Docker pushed and retrieved from Docker Hub 5

In any terminal, Docker pull the Docker image from Docker Hub

root@devopsroles:~# docker pull huupv/dockerpython:latest

For example, use Docker Compose

version: "3.9"  
    build: .
      - "5000:5000"
      - .:/app
      - logvol:/var/log
      - redis
     image: redis
   logvol: {}


You know How to run python on Docker. I hope this will be helpful. Thank you for reading the DevopsRoles page!


About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.