Use Docker as the Python Development Environment

Source: Internet
Author: User
Tags docker hosting virtualenv

Use Docker as the Python Development Environment

In this article, I will try to demonstrate how to use Docker to develop python applications, mainly Web applications. Although I personally focus on the Python Flask microframework, this article aims to demonstrate how to better develop and share applications, developed by any language or framework, through Docker ). By encapsulating dependencies, Docker significantly reduces the gap between the development environment and formal products.

Most Python developers use virtualenv during development. It provides an easy-to-use mechanism for applications to use their own dependencies, which may conflict with other applications or operating systems, especially different Pyhton versions, there are also different library versions ). Personally, I have never been very interested in virtualenv for the following reasons:

  • I often forget to enable it, or forget to switch it when I switch the project. This may lead to vague error information, which is quite confusing.

  • It cannot provide "pure" isolation. It can only be a Python-level Isolation System Library and non-python dependencies will still have problems ).

  • I usually don't want to run it in a formal product, which means that the development environment is different from the formal product.

  • It makes people feel a little "hacker": it is implemented by modifying scripts and setting new paths.

View the pythonrants article to learn more why you may not want to use virtualenv)

So how can we make Docker better? Docker essentially provides a very lightweight VMs, which can be called a "Container"). We can use it to create a highly isolated development and product environment that greatly reduces the mismatch. If you are not familiar with Docker, but want to learn more, you can refer to my talk about Docker at the Edinburgh Technology Forum ).

When we build a small visual Web APP, I myself and Mark Coleman use this document here ). The following figure shows how to install Python 2.7 using a basic image, Flask management, and PostgreSQL. I will develop a hello world Web Application Based on this image. I suppose you are developing on Linux and you already have git and installed Docker. The MacOS command should be very similar. Start by cloning and creating a basic image:

 
 
  1. $ git clone https://github.com/mrmrcoleman/python_webapp  
  2. $ docker build -t python_webapp . 

Now, we need to add some code to the container and specify it in detail. We plan to create a project that only points to the Docker image to do this, instead of directly modifying the previous project.

Create a new project with the following structure:

 
 
  1. ├── Dockerfile  
  2.  ├── example_app  
  3.  │   ├── app  
  4.  │   │   ├── __init__.py  
  5.  │   │   └── views.py  
  6.  │   └── __init__.py  
  7.  ├── example_app.wsgi 

Or clone the sample project for this address: https://github.com/amouat/example_app.git

Write in example_app/app/_ init _. py:

 
 
  1. from flask import Flask  
  2.    
  3. app = Flask(__name__)  
  4. from app import views 

Leave another _ init _. py empty. Write in views. py:

 
 
  1. from app import app  
  2.    
  3. @app.route('/')  
  4. @app.route('/index')  
  5. def index():  
  6.     return "Hello, World!" 

The above is the minimum flask version of a hello world application. I have also used similar code in this tutorial, so if you are new to Flask or Python, you can continue learning using Docker instead of virtualenv Based on the tutorial mentioned above.

To make it run inside the Docker container, we still need to do some operations. On the Apache server of our instance, The example_app.wsgi file contains commands to connect Python code to the web server. This file should contain the following content:

 
 
  1. import site  
  2. site.addsitedir('/opt/example_app/')  
  3. from app import app as application 

Finally, we need a Dockerfile to build the container and run the container. In our instance, it looks like this:

 
 
  1. FROM python_webapp  
  2.    
  3. MAINTAINER amouat  
  4.    
  5. ADD example_app.wsgi /var/www/flaskapp/flaskapp.wsgi  
  6. CMD service apache2 start && tail -F /var/log/apache2/error.log 

ADD: Start WSGI and inject some code. CMD obtains any possible error information when starting the container, apache web server, and sends it to stdout.

If you perform the following operations:

 
 
  1. $ docker build -t example_app .  
  2. $ docker run -p 5000:5000 -v $(pwd)/example_app:/opt/example_app/ -i -t example_app 

You should get a feedback like this: Open the address localhost: 5000 in a browser and you will see that your website is running. If you are running in a VM or vagrant, open port 5000.

Now we have run the web server, which is very close to what we use in the product (I intentionally use Apache to do this, rather than the Python default web server ). We can inject code into the container by ing from the host to the container. We can also use ADD to ADD code in the Dockerfile command line, but when the Code of our team is changed, you need to re-build the container every time.

However, this is still not very good; in development, we really want to use the Python web server that helps us debug a lot. We are glad that we do not need to modify the Dockerfile. Start from creating a run. py file in the example_app file and follow the following content:

 
 
  1. !flask/bin/python  
  2. from app import app  
  3. app.run(debug = True, host='0.0.0.0') 

This will start the Python web server with debugging and listen to all connections, and we can also access it from outside the container. Run the following command to restart the container:

 
 
  1. $ docker run -p 5000:5000 -v $(pwd)/example_app:/opt/example_app/ -i -t example_app python /opt/example_app/run.py 

You can see that the webpage is running again. This time, we explicitly provide the running command "python/opt/example_app/ryn. py"), which overwrites the settings 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 look at our gains:

  • A web application running in an isolated container completely encapsulates the Python dependencies and system dependencies of the application.

  • You can use an existing editor or IDE To develop code and view changes directly, just like editing locally.

  • It is closer to the running environment of formal products than before.

  • Virtualenv is not used.

If you want to know how to create a program release path in this way, you can refer to the visual Web application article mentioned above written by Mark Coleman.

Unfortunately, this is not perfect yet. There are also the following questions:

  • You may still need to use virtualenv or its equivalent solution, such as conflicts between the operating system version of the library and the version required by your program.

  • We have not completely solved the problem of data hosting, and we still need to perform some tests.

  • I assume that the "product" is a Docker container, but this is often not the case and Docker hosting is just getting started.

Even so, I still think this is a huge step towards a better future for software development, greatly reducing the pain of deploying software and managing dependencies.

English: Using Docker as a Python Development Environment

By http://www.oschina.net/translate/using-docker-as-a-python-development-environment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.