Environment: python2.7
docker:
1. A simple python program
Since it is a simple python program, we can implement a simple addition function.
#coding=utf-8 import random def add(aStr,bStr): map={} try: a=float(aStr) b=float(bStr) sum=a+b file_name=str(int(random.random()*1000000))+"" file=open("data/"+file_name+'.txt',"w") file.write(str(a)+"+"+str(b)+"="+str(sum)) file.close() map["result"]="OK" map["id"]=str(file_name) except: map["result"]="wrong" return map if __name__=="__main__": print "run run, bro, not this" map=add("4.5",5.6) print map
The function implemented by this method can be understood clearly, that is to add a and b, and output the result in a file, and return whether the operation is successful, and if it is successful, add the number of the file (regardless of the latter paragraph = =)
2. Provide an external calling interface
In addition to this code, we also need to provide a call interface to the outside world. I use tornado to open a web service to provide the interface. This method is named run method = =
#coding=utf-8 import tornado.web import tornado.ioloop from add import add #define processing class class Add(tornado.web.RequestHandler): #Add a post request method def post(self): #Add data to the response a=self.get_argument('a', '') b=self.get_argument('b', '') map=add(a,b) self.write(map) def make_app(): return tornado.web.Application( [(r'/add', Add)], ) if __name__ == '__main__': # create an application object app = make_app() #Bind a listening port app.listen(8888) #Start the web program and start listening for connections on the port tornado.ioloop.IOLoop.current().start()
When we run run.py, enter the command in the terminal curl -d 'a=3&b=9' 'localhost:8888/add'
The information returned by the terminal is: {"result": "OK", "id": "808992"} (the ID here is not necessarily the same, just a random number)
You can find the corresponding data file in the data directory below the same file
So far, we have implemented a simple python program and made it provide an interface call to the outside world. Now we need to package it into the docker image and encrypt the key code (add)
3. Run an ubuntu image, install python, vim, etc. and necessary python packages such as tornado (not to mention the installation and use of docker = =)
Obviously what we need is the first image, pull it locally, i.e. run docker pull ubuntu
View the local image docker images to see the image just now
Activate it! and install some necessary stuff!
docker run -it c9d990395902 /bin/bash where c9d990395902 is the image ID of ubuntu. If it has been updated when you pull, the ID may change. /bin/bash is used to enter the newly running container.
First execute apt update to update ubuntu system
Then use apt install python to install python2
You will be asked if you have a step to continue, enter y and press Enter.
Enter python to enter the following page and the installation is successful, enter exit() and press Enter to exit the editing
Use apt-get install python-pip python-dev build-essential to install pip, note that y is also required to enter
After the installation is complete, enter pip -V to see if the installation is successful
Install vim, the command is apt-get install vim
Import some python packages, such as tornado, use the command pip install tornado
Another example is cython, the command is pip install cython
So far, we have installed python, pip, vim and imported two necessary packages for this container
4. Create a new folder and copy the code to the container
Run mkdir add_uncleyiba in the root directory and create a new folder add_uncleyiba
Enter the add_uncleyiba folder, create a new directory data, and create a new file test.py
Open test.py with vim
Just press any key such as a, enter the edit mode, enter print 123, then press Esc, enter: wq (preceded by a colon), then press Enter, that is, save
Run python test.py, test it without problems
Now create three new files, called add.py, run.py, setup.py
Use vim to open add.py and copy the previous code in, and copy the code of run.py in the same way (note, first enter the edit mode and then copy)
After copying and saving, you can use the cat command to see if there is any problem with the code
cat add.py
We can try to run add.py in the container with the command python add.py
It doesn't seem to be a problem
Now vim open the setup.py file and enter the following
from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize(["add.py"]))
then save and exit
Run the command python setup.py build_ext to generate the so file of the add.py file to ensure source code security
We can find that the newly created build folder
As shown in the figure add.so is the so file we need!
Use mv add.so ../../add.so to put it in the outer folder
Now we can delete some useless things, except add.so, data, run.py, setup.py, everything else can be deleted
Friendly reminder: use the delete command carefully = = you must have seen the animation
The picture doesn't seem to move - you can go to some water groups to find pictures. . .
5. Submit the image and run the test
Reopen a terminal and make the container an image docker commit 248224b7067e add_uncleyiba:1.0
id is obtained by looking at your own container id, followed by the name of the image you want to generate + colon + version number
Later, when we look at the local mirror, we will find that the mirror add_uncleyiba:1.0 is sanctified.
Go back to an arbitrary directory, of course, preferably the directory where you saved the python code before, and the name of the new folder is data
then run the command
docker run -itd -p 8899:8888 -v #{local_abs_path}:/add_uncleyiba/data add_uncleyiba:1.0 python /add_uncleyiba/run.py
Where #{local_abs_path} is changed to the local absolute path of the data folder (use pwd to view after cd into the directory)
After the new container ID appears, use curl -d 'a=5&b=7' 'localhost:8888/add' to test the access~
At this time, we can enter the local data folder and find that there is a file name in it which is the id value just returned
Open is our test content just now