Hello everyone,
In this article, I will show you how to run a Python program inside a Docker container. In short, this article is not about theory. Instead, this article will cover the practical step by step on how to run a Python program inside a docker container and I assume that you are good on Python coding as well as you understand the concept of Docker.
There will be 3 main steps in this project. First, we will build a Python program which about randomly picking up a movie in the top 250 IMDB movie list and then run it inside our local machine. Then, we will build a docker container for the Python program by using a docker image template inside a docker file. Finally, we will push the docker container to the cloud, opening a new virtual machine, pulling the docker container and running the Python program inside that virtual machine.
About Author

I’m Viet, the founder of this website with 8+ years experience in data analytics. My sharing is focus on data, which specialise on both Analytics and Business Intelligence platform as well as Data Science and Machine Learning platform.
Building a Python program to pick up a movie in the top 250 IMDB movies list
1) Create a new folder and inside the new folder, create a new python file named main.py. Now we will work on this file.
2) Import necessary libraries, which include random, request and BeautifulSoup.
import random import requests from bs4 import BeautifulSoup #pip3 install bs4
3) Enter the url of top 250 IMDB movies list.
url = 'https://www.imdb.com/chart/top'
4) Build our main() function as below.
Basically, we will copy the whole page’s content as a string into a variable named html. Then using BeautifulSoup library to extract year, actors, title and rating of each movie.
We will use 4 lists: years, actors_list, title_list, ratings to store all the items of year, actors, title and rating.
def main(): response = requests.get(url) html = response.text soup = BeautifulSoup(html,'html.parser') movietags = soup.select('td.titleColumn') rating_tags = soup.select('td.posterColumn span[name=ir]') inner_movietags = soup.select('td.titleColumn a') def get_year(movie_tag): moviesplit = movie_tag.text.split() year = moviesplit[-1] return year years = [get_year(tag) for tag in movietags] actors_list = [tag['title'] for tag in inner_movietags] title_list = [tag.text for tag in inner_movietags] ratings = [float(tag['data-value']) for tag in rating_tags] n_movies = len(title_list) #print(n_movies) while(True): idx = random.randrange(0,n_movies) print(f'{title_list[idx]} {years[idx]}, rating: {ratings[idx]:.1f}, starring:{actors_list[idx]}') user_input = input('Do you want another movie?(y/[n])') if user_input != 'y': break
5) Run main() function if it is in main program.
if __name__ == '__main__': main()
Our complete python program will be like this.
import random import requests from bs4 import BeautifulSoup #pip3 install bs4 url = 'https://www.imdb.com/chart/top' def main(): response = requests.get(url) html = response.text soup = BeautifulSoup(html,'html.parser') movietags = soup.select('td.titleColumn') rating_tags = soup.select('td.posterColumn span[name=ir]') inner_movietags = soup.select('td.titleColumn a') def get_year(movie_tag): moviesplit = movie_tag.text.split() year = moviesplit[-1] return year years = [get_year(tag) for tag in movietags] actors_list = [tag['title'] for tag in inner_movietags] title_list = [tag.text for tag in inner_movietags] ratings = [float(tag['data-value']) for tag in rating_tags] n_movies = len(title_list) #print(n_movies) while(True): idx = random.randrange(0,n_movies) print(f'{title_list[idx]} {years[idx]}, rating: {ratings[idx]:.1f}, starring:{actors_list[idx]}') user_input = input('Do you want another movie?(y/[n])') if user_input != 'y': break if __name__ == '__main__': main()
Run this program, we should receive result in terminal like below. Note that I’m using Python 3.6.5 so my command will be python3 main.py
% python3 main.py 250 La passion de Jeanne d'Arc (1928), rating: 8.0, starring:Carl Theodor Dreyer (dir.), Maria Falconetti, Eugene Silvain Do you want another movie?(y/[n])
Cool! We’ve completed our Python program. Now we will move to how to make it run in a docker container.
Create a Docker file and build a Docker container
Let install Docker if you don’t have it in your computer yet.
Now, we will create a Docker file for our Python program.
1) Create a new file in the working folder (same folder where you store your main.py in previous section).
Docker file is the file that we will instruct on how to setup the base image step by step, so the order of the instructions is important in this file.
Our Docker file will be this.
FROM python:3.6.5 ADD main.py . RUN pip3 install requests beautifulsoup4 CMD [ "python", "./main.py" ]
Our instruction is basically like this: the base image need to use python 3.6.5, then add the python program main.py to the docker container, then installing all the necessary libraries to the container, and finally executing the main.py program as the entry point when the container start running.
2) Build Docker image with command below, note that we need a dot at the end of the command.
% docker build -t python-imdb .
The result should be like this.
+] Building 12.4s (9/9) FINISHED => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 197B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:3.6.5 4.7s => [auth] library/python:pull token for registry-1.docker.io 0.0s => CACHED [1/3] FROM docker.io/library/python:3.6.5@sha256:c49ab7d5121521de57653c7209e68102d057ed77aff9859e8a9603b36105911a 0.0s => [internal] load build context 0.0s => => transferring context: 1.17kB 0.0s => [2/3] ADD main.py . 0.1s => [3/3] RUN pip3 install requests beautifulsoup4 6.9s => exporting to image 0.4s => => exporting layers 0.4s => => writing image sha256:acc61d2df3d4d8c113a5ac7fa8856eb0aeef8a988e5fe56a6dccb103acf8c003 0.0s => => naming to docker.io/library/python-imdb
3) Run the python program by starting the container.
% docker run -t -i python-imdb
We should receive the result like this.
Green Book (2018), rating: 8.2, starring:Peter Farrelly (dir.), Viggo Mortensen, Mahershala Ali Do you want another movie?(y/[n])
Push the docker container to container hub
1) Open a new terminal and login into docker with your docker account. After entering your account password, the Login Succeeded message will show up.
% docker login -u huynhthviet Password: Login Succeeded
2) Now we need to tag the container before push it to docker hub.
% docker tag python-imdb:latest huynhthviet/dockerhub:python-imdb
Just a quick note here: python-imdb is my docker image’s name, latest is its default tag (yours should be the same), huynhthviet/dockerhub is my hub location (you can use the same as youraccount/dockerhub for your hub location)
3) Then push the docker container to the cloud docker hub with command:
% docker push huynhthviet/dockerhub:python-imdb
The successful result should be like this.
The push refers to repository [docker.io/huynhthviet/dockerhub] ce21f7be808e: Pushed 258cd3f39bba: Pushed 73fc8bce5073: Pushed 54d95b300b59: Pushed b1638a94bc26: Pushed 0f093e2a0b2a: Pushed 9df2ff4714f2: Pushed c30dae2762bd: Pushed 43701cc70351: Pushed e14378b596fb: Pushed a2e66f6c6f5f: Pushed python-imdb: digest: sha256:461a64711ae3d89973d9a09c410760994fb33c0ad563e7dbdd5763cf7fd959bb size: 2636
Great! We've uploaded our docker container to cloud successfully. Now let run it in another machine.
Run the docker container in another machine
1)Access this webpage:
https://labs.play-with-docker.com/
Login with your docker account and then press + ADD NEW INSTANCE button.
2) A new terminal will popup in our virtual Linux machine, let type command python, the result should be like below which shows that our Linux machine doesn't have any Python environment installed yet. But since we now run our Python program inside a docker container, we don't need our Linux machine to be installed anything.
python bash: python: command not found [node1] (local) root@192.168.0.18 ~
3) Let check the docker version of the Linux machine with command docker -v. The result should be like this.
Docker version 20.10.0, build 7287ab3
4) Now we pull our docker container to the Linux machine with command.
docker pull huynhthviet/dockerhub:python-imdb
The result should be like this.
python-imdb: Pulling from huynhthviet/dockerhub 0bd44ff9c2cf: Pull complete 047670ddbd2a: Pull complete ea7d5dc89438: Pull complete ae7ad5906a75: Pull complete 0f2ddfdfc7d1: Pull complete d055f4d7ae62: Pull complete c501289d05b9: Pull complete 211aaca0a156: Pull complete a2d4f20d1579: Pull complete 8190b4a4037a: Pull complete bac17f218e6d: Pull complete Digest: sha256:461a64711ae3d89973d9a09c410760994fb33c0ad563e7dbdd5763cf7fd959bb Status: Downloaded newer image for huynhthviet/dockerhub:python-imdb docker.io/huynhthviet/dockerhub:python-imdb
5) Run our docker container in the virtual Linux machine.
docker run -t -i huynhthviet/dockerhub:python-imdb
The result should be like as below.
City Lights (1931), rating: 8.5, starring:Charles Chaplin (dir.), Charles Chaplin, Virginia Cherrill Do you want another movie?(y/[n])
Congratulations! We now run our Python program in another machine with the Docker container.
Summary
In this article we have done 3 things: build a Python program and run it in the local environment, then build a Docker container for the python program and push it to the cloud, then pull the container into another machine and run it in this new machine.
I hope you have gained a lot of skills from this post. If you have anything to discuss, feel free to feedback in the comment section.