{"id":3594,"date":"2023-04-12T15:39:33","date_gmt":"2023-04-12T15:39:33","guid":{"rendered":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/"},"modified":"2026-01-15T18:06:14","modified_gmt":"2026-01-15T18:06:14","slug":"creating-a-docker-application","status":"publish","type":"post","link":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/","title":{"rendered":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile?"},"content":{"rendered":"<p><strong>Create a project<\/strong><\/p>\n<p>To create a <a href=\"https:\/\/www.docker.com\/resources\/what-container\">Docker application<\/a>, you need an application and a (Docker) image file. In this example we\u2019ll use a very simple Python script as the application, so we have to create 2 files:<\/p>\n<ul>\n<li>A \u201c<em>py<\/em>\u201d file (the python script that will contain the code to be executed)<\/li>\n<li>A \u2018<em>Dockerfile<\/em>\u2019 file (the Docker file that will contain the necessary commands to create the environment and launch the application).<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2040 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC1-min.jpg\" alt=\"\" width=\"900\" height=\"259\" \/><\/p>\n<p>Python files are called\u00a0modules\u00a0and they are identified by the . py file extension. A module can define functions, classes, and variables. In this case, there is going to be only one Python script and it will be contained in the \u201cmain\u201d module, in the main.py file.<\/p>\n<p>The Dockerfile is simply a text-based script of commands that are used to create a container image. And launch the Python application. To create this file, you can use a text-editor (like notepad) but you need to save the file as \u201cDockerfile\u201d (include the quotes) and no extension:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2041 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC2-min.jpg\" alt=\"\" width=\"897\" height=\"77\" \/><\/p>\n<p><strong>Writing the Python Script<\/strong><\/p>\n<p>Once the files are created, you can add the Python script to the \u201c<em>main.py\u201d<\/em> file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2042 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC3-min.jpg\" alt=\"\" width=\"803\" height=\"225\" \/><\/p>\n<p>The simple script will display a message and then list of all files and directories in the current directory using the listdir and Print Python functions.<\/p>\n<p><strong>Adding Docker Commands<\/strong><\/p>\n<p>Now you have to update the Dockerfile. What we want to do is to launch the Python application (run the Python script). To accomplish this, the Dockerfile must contain all the dependencies necessary to launch Python.<\/p>\n<p>The first step to take when you create a Docker file is to access the\u00a0<a href=\"https:\/\/hub.docker.com\/\">DockerHub<\/a>\u00a0website. This site contains many pre-designed images to save your time (for example: all images for linux or code languages). In our case, we will type \u2018Python\u2019 in the search bar. The first result is\u00a0<a href=\"https:\/\/hub.docker.com\/_\/python\">the official image<\/a>\u00a0created to execute Python. That\u2019s what we need.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2043 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC4-min.jpg\" alt=\"\" width=\"478\" height=\"259\" \/><\/p>\n<p><strong>Creating a Docker image<\/strong><\/p>\n<p>Once the Python script and the Dockerfile are both ready, all you have to do is create an image to contain the application. You do this with the <a href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/build\/\">docker build command<\/a>. The command uses the -t parameter to set the name of the image (jimsdockerfile). Take note that the dot \u201c.\u201d Is required to use the Dockerfile in the local directory<\/p>\n<p>docker build -t jimsdockerfile .<\/p>\n<p>To run this command (and other docker commands) you need to open a terminal session using Windows PowerShell (or similar). Notice I had to change the directory to the folder where my files reside (KBDocker):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2043 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC4-min.jpg\" alt=\"\" width=\"478\" height=\"259\" \/><\/p>\n<p><strong>Running the Docker image<\/strong><\/p>\n<p>Once the image has been created, the application is ready to be initiated. This is done with the <a href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/run\/\">docker run command<\/a>,\u00a0 stating the name of the image after \u201c<em>docker run<\/em>\u201d:<\/p>\n<p>docker run jimsdockerfile<\/p>\n<p>You should see the following displayed in the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2044 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC6-min.jpg\" alt=\"\" width=\"900\" height=\"398\" \/><\/p>\n<p><strong>Something Different<\/strong><\/p>\n<p>The Python script (application) I used isn\u2019t very interesting especially since its terminal based. To make it a little more fun, we can edit the main.py file changing it to the following sample script that uses <a href=\"https:\/\/github.com\/mkaz\/termgraph\">Termgrah<\/a> (a command-line tool that draws basic graphs in the terminal, written in Python):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2045 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC7-min.jpg\" alt=\"\" width=\"867\" height=\"373\" \/><\/p>\n<p><strong>Rebuilding and Rerunning<\/strong><\/p>\n<p>For <em>this<\/em> script to work, we need to add 1 new command to our Dockerfile. That is shown below:<\/p>\n<p>RUN pip install requests termgraph<\/p>\n<p>This uses pip (the package manager for\u00a0<em>Python<\/em>\u00a0packages) to install the termgraph Python library which the script requires. Once both files are saved, I can again use the docker build command (to recreate the docker image) and the docker run command (to run the Python application). The following output should be generated:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2046 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC8-min.jpg\" alt=\"\" width=\"900\" height=\"257\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a project To create a Docker application, you need an application and a (Docker) image file. In this example we\u2019ll use a very simple Python script as the application, so we have to create 2 files: A \u201cpy\u201d file (the python script that will contain the code to be executed) A \u2018Dockerfile\u2019 file (the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[34],"class_list":["post-3594","post","type-post","status-publish","format-standard","hentry","category-quebit-value","tag-data-management"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT\" \/>\n<meta property=\"og:description\" content=\"Create a project To create a Docker application, you need an application and a (Docker) image file. In this example we\u2019ll use a very simple Python script as the application, so we have to create 2 files: A \u201cpy\u201d file (the python script that will contain the code to be executed) A \u2018Dockerfile\u2019 file (the&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/\" \/>\n<meta property=\"og:site_name\" content=\"QueBIT\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-12T15:39:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-15T18:06:14+00:00\" \/>\n<meta name=\"author\" content=\"agoddard\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"agoddard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/\"},\"author\":{\"name\":\"agoddard\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"headline\":\"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile?\",\"datePublished\":\"2023-04-12T15:39:33+00:00\",\"dateModified\":\"2026-01-15T18:06:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/\"},\"wordCount\":612,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/DOC1-min.jpg\",\"keywords\":[\"Data Management\"],\"articleSection\":[\"QueBIT Value\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/\",\"name\":\"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/DOC1-min.jpg\",\"datePublished\":\"2023-04-12T15:39:33+00:00\",\"dateModified\":\"2026-01-15T18:06:14+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#primaryimage\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/DOC1-min.jpg\",\"contentUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/DOC1-min.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/creating-a-docker-application\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\",\"name\":\"QueBIT\",\"description\":\"QueBIT\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\",\"name\":\"agoddard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"caption\":\"agoddard\"},\"sameAs\":[\"https:\\\/\\\/quebit.com\\\/askquebit\"],\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/author\\\/agoddard\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/","og_locale":"en_US","og_type":"article","og_title":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT","og_description":"Create a project To create a Docker application, you need an application and a (Docker) image file. In this example we\u2019ll use a very simple Python script as the application, so we have to create 2 files: A \u201cpy\u201d file (the python script that will contain the code to be executed) A \u2018Dockerfile\u2019 file (the&hellip;","og_url":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/","og_site_name":"QueBIT","article_published_time":"2023-04-12T15:39:33+00:00","article_modified_time":"2026-01-15T18:06:14+00:00","author":"agoddard","twitter_card":"summary_large_image","twitter_misc":{"Written by":"agoddard","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#article","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/"},"author":{"name":"agoddard","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"headline":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile?","datePublished":"2023-04-12T15:39:33+00:00","dateModified":"2026-01-15T18:06:14+00:00","mainEntityOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/"},"wordCount":612,"commentCount":0,"image":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC1-min.jpg","keywords":["Data Management"],"articleSection":["QueBIT Value"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/","url":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/","name":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile? - QueBIT","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/#website"},"primaryImageOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#primaryimage"},"image":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC1-min.jpg","datePublished":"2023-04-12T15:39:33+00:00","dateModified":"2026-01-15T18:06:14+00:00","author":{"@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"breadcrumb":{"@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#primaryimage","url":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC1-min.jpg","contentUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2022\/02\/DOC1-min.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/quebit.com\/askquebit\/creating-a-docker-application\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/quebit.com\/askquebit\/"},{"@type":"ListItem","position":2,"name":"How Do I Create and Run a Docker Application Using a Python Script and Dockerfile?"}]},{"@type":"WebSite","@id":"https:\/\/quebit.com\/askquebit\/#website","url":"https:\/\/quebit.com\/askquebit\/","name":"QueBIT","description":"QueBIT","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/quebit.com\/askquebit\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084","name":"agoddard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","caption":"agoddard"},"sameAs":["https:\/\/quebit.com\/askquebit"],"url":"https:\/\/quebit.com\/askquebit\/author\/agoddard\/"}]}},"_links":{"self":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/comments?post=3594"}],"version-history":[{"count":1,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3594\/revisions"}],"predecessor-version":[{"id":4907,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3594\/revisions\/4907"}],"wp:attachment":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/media?parent=3594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/categories?post=3594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/tags?post=3594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}