In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the use of Docker to develop python Web applications case, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
In this article, I will try to demonstrate a feasible way to develop python applications (mainly Web applications) with Docker. Although I personally focus on Python's Flask microframework, the purpose of this article is to demonstrate how to better develop and share applications (applications developed in any language and framework) through Docker. Docker greatly reduces the gap between the development environment and the formal product by encapsulating dependencies.
Most Python developers use virtualenv in their development. It provides an easy-to-use mechanism for applications to use their own proprietary dependencies that may conflict with other applications or operating systems (especially different Pyhton versions, different library versions, and so on). Personally, I haven't been very interested in virtualenv for the following reasons:
I often forget to enable it, or forget to switch it when switching projects, which will encounter vague error messages, which is very confusing.
It does not provide "pure" isolation, only at the Python level (system libraries and non-python dependencies can still go wrong).
I usually don't want to run it in a formal product, which means that there is an inconsistency between the development environment and the formal product.
It feels like a bit of a "hacker": it depends on modifying scripts and setting new paths.
(see pythonrants's article to learn more about why you may not want to use virtualenv.)
So, what can Docker do to get better? Docker essentially provides a very lightweight VMs (in terms of "containers") that we can use to create a development and production environment with high standards of isolation and significant reduction of mismatches. If you are not familiar with Docker but want to learn more, you can check out my conversation about Docker at the Edinburgh technical symposium.
When we build a small visual Web APP, Mark Coleman and I use this method (the documentation is here). This outlines a basic image installation of Python 2.7, as well as some Flask management and PostgreSQL content. I will use this image to develop a Web application for hello world. I assume that you are developing on Linux and that you already have git and that the instructions for installing Docker,MacOS should be very similar. Start by cloning and establishing a basic mirror:
$docker build-t example _ app.
$docker run-p 5000: 5000-v $(pwd) / example_app: / opt / example_app /-I-t example_app
You should get this feedback: open the address localhost:5000 through your browser and you will see that your website is running. If you are running in VM or vagrant, remember to open port 5000.
Now that we are running the web server, we are very close to what we use in the product (I intentionally use Apache to do this instead of Python's default web server). We inject code into the container by mapping from the host to the container; we can also use ADD to add code on the Dockerfile command line, but then we need to rebuild the container every time our team code changes.
However, this is still not very good; in development we really want to use the Python web server that greatly helps us debug. We should be glad that we don't have to make any changes to Dockerfile. In the example_app file, start by creating a run.py file, as follows:
This will start Python's web server with debugging and listen for all connections, which we can also access from outside the container. Now restart the container with the following command:
$docker run-p 5000 example_app python 5000-v $(pwd) / example_app:/opt/example_app/-I-t example_app python / opt/example_app/run.py
You can see that the web page is running again. This time we explicitly provide the running command ("python / opt/example_app/ryn.py"), which overrides the setting of the CMD line in Dockerfile. Now if you edit the source program on the host, you can immediately see the changes on the web page.
Let's take a moment to see what we've got:
A web application runs in an isolated container, which completely encapsulates the application's Python dependencies and system dependencies.
You can use an existing editor or IDE to develop code and view changes directly, just like editing locally.
Closer to the operating environment of the formal product than ever before.
Virtualenv is not used.
If you want to know how to set up a way to distribute a program in this way, take a look at Mark Coleman's article about the previously mentioned visual Web application.
Unfortunately, all this is not perfect. There are also the following questions:
You may still encounter situations where you need to use virtualenv or its equivalent solution, such as a conflict between the operating system version of the library and the required version of your program.
We haven't completely solved the problem of data hosting yet, and we still need to do some tests.
My hypothetical "product" is a Docker container, but this is often not the case and Docker hosting itself is in its infancy.
Still, I think this is a big step towards a better future for software development, greatly reducing the pain of deploying software and managing dependencies.
The above is all the contents of the article "developing python Web applications with Docker". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.