In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Docker as the development environment of Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Docker as the development environment of Python".
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:
$git clone https://github.com/mrmrcoleman/python_webapp $docker build-t python_webapp.
Now, we need to add some code to the container and write it in detail. We intend to create a new project that only points to the Docker image to do this, rather than directly modifying the previous project.
Create a new project with the following structure:
├── Dockerfile ├── example_app │ ├── app │ │ ├── _ _ init__.py │ │ └── views.py │ └── _ _ init__.py ├── example_app.wsgi
Or clone the sample project of this address: https://github.com/amouat/example_app.git
Write in example_app/app/_init_.py:
From flask import Flask app = Flask (_ _ name__) from app import views
Leave the other _ init_.py empty. Write in views.py:
From app import app @ app.route ('/') @ app.route ('/ index') def index (): return "Hello, World!"
This is the smallest flask version of one of our hello world apps. I've used similar code in this tutorial, so if you're new to Flask or Python, you can follow the above tutorial and use Docker instead of virtualenv to continue.
In order for it to run inside the Docker container, we need to do something else. In our example Apache server, the example_app.wsgi file contains instructions to connect the Python code to the web server. The file should contain the following:
Import site site.addsitedir ('/ opt/example_app/') from app import app as application
Finally, we need a Dockerfile to build the container and run it. In our example, it looks like this:
FROM python_webapp MAINTAINER amouat ADD example_app.wsgi / var/www/flaskapp/flaskapp.wsgi CMD service apache2 start & & tail-F / var/log/apache2/error.log
ADD's behavior starts WSGI by injecting some code. The CMD line gets any possible error messages when starting the container and the apache web server, and sends it to stdout.
If you do the following:
$docker build-t example_app. $docker run-p 5000 example_app 5000-v $(pwd) / example_app:/opt/example_app/-I-t
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:
! flask/bin/python from app import app app.run (debug = True, host='0.0.0.0')
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 * yet. 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.
Thank you for reading, the above is the content of "how to use Docker as the development environment of Python". After the study of this article, I believe you have a deeper understanding of how to use Docker as the development environment of Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.